CSS关于两列布局
我从来没有想过使用css编写一个简单的两列布局是如此复杂....哈哈
我想做的是以下内容:
当内容div的高度超过屏幕尺寸的高度时,滚动条存在仅在内容 div 中。用户只能滚动内容 div,但侧边栏保持静态
两列应具有相同的高度
我的布局是:
<----------------容器--------------------> ;
<-------------------标题------------------>
<-----侧边栏-----><---------内容--->
<------------------页脚-------------------->
<---容器末端------------------------>
这是我的 css 文件: http://137.189.145.40/c2dm/css/main.css
I have never thought that writing a simple two column layout is so complicated using css....haha
What I want to do is the following:
When the height of the content div exceed the height of screen size, scroll bar exist only in the content div. The users can only scroll the content div but the sidebar keeps static
The two columns should have the same height
My layout is:
<---------------container------------------->
<-------------------header------------------>
<-----sidebar-------><---------content--->
<------------------footer------------------->
<---End of container------------------------->
Here is my css file:
http://137.189.145.40/c2dm/css/main.css
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将为您提供一个页面,其中主 div 无法滚动,但两个 div 列可以滚动。他们将肩并肩。您的问题不太清楚,所以希望这就是您所追求的。
编辑:响应您显示示例站点。
你的问题其实很简单。
所有 div 的高度规则为
height: 100%;
当您使用百分比高度时,您将使其成为其所在容器(即其父容器)的百分比。 它不是整个窗口的百分比高度。
每个容器都指定一个百分比高度,因此结果是高度 0。
给你最外面的div一个固定的高度,问题就解决了。
附加编辑:
如果您担心确保最外面的 div 始终拉伸到窗口的底部,那么这是使用绝对定位的 css 解决方案:
即使外部 div 没有硬性,使用此方法仍然会导致计算出的高度编码高度。这将允许您在内部 div 上使用百分比高度,并维护一个从可见窗口的顶部延伸到底部的外部 div。
That will give you a page where the main div cannot scroll but the two div columns can. They will be side by side. You question wasn't exactly clear so hopefully this is what you were after.
EDIT: In response to you showing the example site.
Your problem is really simple.
All of your divs have a height rule of
height: 100%;
When you use percentage height, you are making it a percent of the container it is within, i.e Its parent container. It is NOT a percentage height of the entire window.
Every container is specifying a percentage height so the result is a height of 0.
Give your outermost div a fixed height and the problem will be resolved.
Additional Edit:
If you are concerned with making sure the outermost div always stretches to the bottom of the window then this is a css solution using absolute positioning:
Using this approach still causes a calculated height even though the outer div doesn't have a hard coded height. This will allow you to use percentage heights on your inner divs and maintain a outer div that stretches from top to the bottom of the visible window.
您必须将容器元素设置为
overflow:hidden;
,并将内容 div 设置为overflow:scroll;
(并且可能会执行overflow-x:hidden ;
隐藏水平滚动条)。这样做的问题是,如果你的侧边栏和内容将具有相同的高度,那么您将必须有两个滚动条 - 一个用于内容,一个用于侧边栏。您可能可以通过在侧边栏和侧边栏周围使用另一个容器元素来解决此问题。内容,并设置溢出:滚动条; overflox-x:hidden; 在上面而不是侧边栏/内容上。
You'd have to set your container element to
overflow:hidden;
, and your content div tooverflow:scroll;
(and possibly dooverflow-x:hidden;
to hide the horizontal scrollbar). The problem with this is that if your sidebar & content are going to be the same height, then you would have to have TWO scrollbars - one for content, and one for sidebar.You could probably solve this by using another container element around just sidebar & content, and setting the
overflow: scrollbar; overflox-x:hidden;
on it instead of sidebar/content.如果您在使用
float
时遇到困难,还可以使用display:table
和display:table-cell
创建列。 CSS 如下:HTML 为:
希望这能解决您的问题。但是
display:table
在某些旧浏览器中不起作用。You can also use
display:table
anddisplay:table-cell
to create columns if you're facing difficulties withfloat
. Here's the CSS:and the HTML is:
Hope this solves your problem. But
display:table
doesn't work in some old browsers.