简单的 CSS 布局
我需要找到一种跨浏览器的方式,将两个 div 包含在父 div 中,其中第一个子 div 具有固定高度,第二个 div 向下流动以包含在父 div 中。
height: 500px
_____________________
| _________________ |
| | height: 20px | |
| |_________________| |
| _________________ |
| | | |
| | | |
| | height: 100% | |
| | | |
| | | |
| |_________________| |
|_____________________|
因此,父级的固定高度为 500px,标题 div 的固定高度为 20px,但主体 div 的高度必须向下流动以适合父级。将其高度设置为 100% 会使它的高度溢出 20px,因为 height:100% 使用父母的高度,而不考虑孩子的高度。我看到了一个使用绝对位置的解决方案,但这在 IE7 或 6 上不起作用。是否有跨浏览器解决方案,因为它是一个相对简单的布局?
编辑:忘记提及父级将可调整大小,因此并不总是 500px。使用 jquery 可调整大小,父维度可以是任何内容,并且会经常更改。
编辑2:我希望我能将这个解决方案归功于多个人,因为我听取了每个人的建议。基本上,我给 #body
的高度为 480px,并使用 resizing 中的alsoResize 选项来调整父级和主体的大小。
I need to find a cross browser way of having two divs contained in a parent div where the first child has a fixed height and the second flows down to be contained in the parent.
<div id="parent"><div id="header"></div><div id="body"></div></div>
height: 500px
_____________________
| _________________ |
| | height: 20px | |
| |_________________| |
| _________________ |
| | | |
| | | |
| | height: 100% | |
| | | |
| | | |
| |_________________| |
|_____________________|
So the parent has a fixed height of 500px, the header div has a fixed height of 20px but the body div's height must flow down to fit inside the parent. Giving it a height of 100% overflows it's height by 20px because height:100% uses the parents height not taking into account the children. I saw a solution using position absolute but this does not work on IE7 or 6. Is there a cross browser solution for this as it is a relatively simple layout?
Edit: Forgot to mention that the parent will be resizable so will not always be 500px. Using jquery resizable, the parent dimensions could be anything and will change often.
Edit 2: I wish I could credit multiple people for this solution as I took advice from everyone. Basically I gave #body
a height of 480px and used the alsoResize option in resizable to resize the parent and the body.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
控制 child2 的高度,而不是动态设置父级的大小。
希望这就是您所要求的。
编辑:根据您的更新进行编辑!
Instead of setting the size of the parent dynamically, control child2's height.
Hope that is what your asking for.
Edit: Edited as per your update!
如果您已经知道外部容器的高度以及内部分区的高度,我认为您可以知道剩余 div 的高度。
尝试这样的
height: 480px;
。保持简单易行If you already know the height of outer container, and the one of height of the divisions inside, you can know the height of the remaining div I think.
try something like this
height: 480px;
. Keep it simple and easy不幸的是,这个问题并不像您想象的那么简单。您可以使用一些 JavaScript 来根据
parent
的计算高度更改body
的高度。但最简单的事情可能是将
body
的高度设置为 100%,并在parent
div 上设置overflow: hide
。这样,主体的高度将等于父级,但底部 20 像素将被剪掉。这可能是可接受的,也可能是不可接受的,具体取决于您对
body
执行的操作,以及其内容是否可能到达底部。另一种方法是使用:
并让
body
成为由脚本调整大小的主体,而不是parent
。parent
将自动调整大小以包围header
+body
的总高度。Unfortunately, this isn't as simple a problem as you think it ought to be. You can use some javascript to change the height of
body
based onparent
's computed height.But the simplest thing may be to just set
body
s height to 100%, and setoverflow: hidden
on theparent
div.This way the body will have a height equal to the parent, but the bottom 20px will just be clipped. This may or may not be acceptable, depending on what you're doing with the
body
, and whether its content will potentially reach the bottom.Another approach would be to use:
and have
body
be the one that gets resized by the script, rather thanparent
.parent
will automatically be sized to surround the total height ofheader
+body
.我对父级具有动态高度的页面的解决方案:
My solution for a page where parent has a dynamic height:
如果#parent 的高度始终为 500 像素,而 #header 的高度始终为 500 像素,那么为什么不直接将 480 像素 (1) 的高度设置为 #body 呢?
(1) 当然,您必须忽略任何垂直边距或填充。
PS:现在在 IE6 中测试可能不值得
If #parent always have 500px height, and #header, why not just putting 480px (1) to #body?
(1) Of course you have to discount any vertical margin or padding.
PS: Testing in IE6 may not be worth the time nowadays