并排对齐两个 div
我有一个小问题。我正在尝试使用 CSS 并排对齐两个 div,但是,我希望将中心 div 水平放置在页面中央,我通过使用实现了这一点:
#page-wrap { margin 0 auto; }
效果很好。我想将第二个 div 放置在中央换行的左侧,但我无法使用浮动来做到这一点,尽管我确信这是可能的。
我想将红色 div 推到白色 div 旁边。
这是我当前关于这两个 div 的 CSS,侧边栏是红色 div,换行是白色 div:
#sidebar {
width: 200px;
height: 400px;
background: red;
float: left;
}
#page-wrap {
margin: 0 auto;
width: 600px;
background: #ffffff;
height: 400px;
}
I have a small problem. I am trying to align two divs side by side using CSS, however, I would like the center div to be positioned horizontally central in the page, I achieved this by using:
#page-wrap { margin 0 auto; }
That's worked fine. The second div I would like positioned to the left side of the central page wrap but I can't manage to do this using floats although I'm sure it is possible.
I would like to push the red div up alongside the white div.
Here is my current CSS concerning these two divs, sidebar being the red div and page-wrap being the white div:
#sidebar {
width: 200px;
height: 400px;
background: red;
float: left;
}
#page-wrap {
margin: 0 auto;
width: 600px;
background: #ffffff;
height: 400px;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果你包装你的 div,像这样:
你可以使用这个样式:
不过,这是一个稍微不同的外观,所以我不确定这是否是你想要的。这会将所有
800px
作为一个单位居中,而不是将600px
与左侧的200px
居中。基本方法是你的侧边栏向左浮动,但在主 div 内部,并且#page-wrap
具有侧边栏的宽度,因为它的左边距可以移动那么远。根据评论进行更新:对于这种偏心的外观,您可以执行以下操作:
使用以下样式:
If you wrapped your divs, like this:
You could use this styling:
This is a slightly different look though, so I'm not sure it's what you're after. This would center all
800px
as a unit, not the600px
centered with the200px
on the left side. The basic approach is your sidebar floats left, but inside the main div, and the#page-wrap
has the width of your sidebar as it's left margin to move that far over.Update based on comments: For this off-centered look, you can do this:
With this styling:
我不明白为什么 Nick 使用
margin-left: 200px;
而不是将另一个div
浮动到left
或right
,我刚刚调整了他的标记,您可以对两个元素使用float
而不是使用margin-left
。演示
另外,我使用了正在调用的
.clear:after
父元素,只是为了自我清除父元素。I don't understand why Nick is using
margin-left: 200px;
instead off floating the otherdiv
to theleft
orright
, I've just tweaked his markup, you can usefloat
for both elements instead of usingmargin-left
.Demo
Also, I've used
.clear:after
which am calling on the parent element, just to self clear the parent.这可以通过样式属性来完成。
其结果将是:
享受...
请注意:这适用于更高版本的 CSS (>3.0)。
This Can be Done by Style Property.
Its Result will be :
Enjoy...
Please Note: This works in Higher version of CSS (>3.0).
HTML 代码用于三个 div 并排对齐,也可以通过一些更改用于两个 div
CSS 将是
此代码将针对响应式布局进行工作,因为它将
根据设备宽度调整大小。
一个人可以让任何人保持沉默
即使
,也可以使用两个人
并排休息。
The HTML code is for three div align side by side and can be used for two also by some changes
The CSS will be
This code will workup towards responsive layout as it will resize the
according to device width.
Even one can silent anyone
as
and can use rest two for two
side by side.
也可以在没有包装器的情况下执行此操作 - div#main。您可以使用 margin: 0 auto; 将#page-wrap 居中;方法,然后使用 left:-n;方法来定位#sidebar并添加#page-wrap的宽度。
但是,如果窗口小于内容,侧边栏将消失在浏览器视口之外。
尼克的第二个答案是最好的,因为它也更易于维护,因为如果您想调整#page-wrap 的大小,则不必调整#sidebar。
It's also possible to to do this without the wrapper - div#main. You can center the #page-wrap using the margin: 0 auto; method and then use the left:-n; method to position the #sidebar and adding the width of #page-wrap.
However, the sidebar would disappear beyond the browser viewport if the window was smaller than the content.
Nick's second answer is best though, because it's also more maintainable as you don't have to adjust #sidebar if you want to resize #page-wrap.
最简单的方法是将它们包装在
container div
中,并将margin: 0 auto;
应用于容器。这将使#page-wrap
和#sidebar
div 在页面上居中。但是,如果您想要偏离中心的外观,则可以将container
200px
向左移动,以考虑#sidebar
的宽度代码>分区。The easiest method would be to wrap them both in a
container div
and applymargin: 0 auto;
to the container. This will center both the#page-wrap
and the#sidebar
divs on the page. However, if you want that off-center look, you could then shift thecontainer
200px
to the left, to account for the width of the#sidebar
div.