HTML+CSS:如何移动不关闭的div来创建右列?

发布于 2024-12-07 07:21:46 字数 1933 浏览 1 评论 0原文

我有一个我认为很简单的问题,但花了我很多时间来解决它。如果有一个简单的解决方案(并且我不太了解CSS),请指出我,我会立即关闭这个问题。

我的问题如下。

我有 6 个 div 一个在另一个之上,类似于:

<div id="header">header content</div>
<div id="sidebar1">sidebar1 content</div>
<div id="maincontent">maincontent content</div>
<div id="maincontent2">maincontent2 content</div>
<div id="sidebar2">sidebar2 content</div>
<div id="footer">footer content</div>

我想要的是类似于:

-----------------------------------------------------------
| header content                                          |
-----------------------------------------------------------
--------------------------------------  -------------------
| maincontent content                |  | sidebar1 content|
|                                    |  -------------------
|                                    |  -------------------
|                                    |  | sidebar2 content|
|                                    |  -------------------
--------------------------------------
--------------------------------------
| maincontent2content                |
|                                    |
|                                    |
|                                    |
|                                    |
--------------------------------------
-----------------------------------------------------------
| footer content                                          |
-----------------------------------------------------------

换句话说,有没有办法创建一个右列,即使这些 div 不是一个接一个仅使用 CSS 并且不移动代码中的div?

我尝试对 sidebar1 使用以下 css

 position:absolute;
 top:3em;
 right:0;

,对 sidebar2 使用以下 css,

 position:absolute;
 top:9.5em;
 right:0;

但我的印象是这不是正确的方法。

一个简单的方法可能是将 top 属性大小定义为 Heather 高度的函数,但我找不到任何仅使用 CSS 来实现的方法。

谢谢!

I have a problem that I thought was pretty simple but that is taking me a lot of time to solve it. If there is a simple solution (and I don't know CSS enough), please point me there and I'll close this question immediately.

My problem is the following.

I have 6 divs one on top of another, something like:

<div id="header">header content</div>
<div id="sidebar1">sidebar1 content</div>
<div id="maincontent">maincontent content</div>
<div id="maincontent2">maincontent2 content</div>
<div id="sidebar2">sidebar2 content</div>
<div id="footer">footer content</div>

What I want to have is something like:

-----------------------------------------------------------
| header content                                          |
-----------------------------------------------------------
--------------------------------------  -------------------
| maincontent content                |  | sidebar1 content|
|                                    |  -------------------
|                                    |  -------------------
|                                    |  | sidebar2 content|
|                                    |  -------------------
--------------------------------------
--------------------------------------
| maincontent2content                |
|                                    |
|                                    |
|                                    |
|                                    |
--------------------------------------
-----------------------------------------------------------
| footer content                                          |
-----------------------------------------------------------

In other words, is there a way to create a right column even if the divs are not one after another using only CSS and without moving the divs in the code?

I tried to use for sidebar1 the following css

 position:absolute;
 top:3em;
 right:0;

and for sidebar2 the following css

 position:absolute;
 top:9.5em;
 right:0;

but I have the impression that this is not the right way to do it.

A simple way could be to define the top attribute size as a function of the height of heather, but I couldn't find any way to do it only with CSS.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟火散人牵绊 2024-12-14 07:21:46

嗯...作为您问题的直接答案,您可以使用浮点数和%宽度的组合来完成此任务。您还可以使用固定宽度,但您必须确定这些尺寸。这是一个简单的实现:

演示


CSS

#maincontent
{
    float:left;
    width:70%;
    background:#ee5;
}

#sidebar2,#sidebar1
{
    float:right;
    width:30%;
    background:#5e5;
}

#footer
{
    clear:both;
    background:#5ee;
}

#header
{
    background:#55e;
}

HTML

<div id="header">header content</div>
<div id="sidebar1" class="side">sidebar1 content</div>
<div id="maincontent">maincontent content</div>
<div id="sidebar2" class="side">sidebar2 content</div>
<div id="footer">footer content</div>

Well... as a direct answer to your question, you can use a combination of floats and % widths to accomplish this. You can also use fixed widths, but you'll have to determine what those sizes are. Here's a simple implementation:

Demo


CSS

#maincontent
{
    float:left;
    width:70%;
    background:#ee5;
}

#sidebar2,#sidebar1
{
    float:right;
    width:30%;
    background:#5e5;
}

#footer
{
    clear:both;
    background:#5ee;
}

#header
{
    background:#55e;
}

HTML

<div id="header">header content</div>
<div id="sidebar1" class="side">sidebar1 content</div>
<div id="maincontent">maincontent content</div>
<div id="sidebar2" class="side">sidebar2 content</div>
<div id="footer">footer content</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文