拉伸内部 div 以匹配可变高度的容器。不可能的?
我喜欢认为我是一个相当不错的……嗯,还过得去的……网络开发人员。我已经建立了大概... IDK,50 个网站,使用了大量的 Flash 和 Javascript,以及一些数据库支持的应用程序,并做了足够的图形设计,能够有效地伪造它:)
我遇到过这个问题在我的职业生涯中出现过六次:当容器的高度可变时,如何将内部 div 设置为包含 div 的 100% 高度?我知道你可以用 Javascript 或使用人造列来做到这一点,但我向狗发誓,如果没有办法用纯 HTML/CSS 来做到这一点,我将对生活和 W3C 感到非常失望:(
这里是模型伙伴: http://random.siliconrockstar.com/heightTest/
看,内部div #content 将 #container 向下推。我希望粉红色部分 #shadow 与 #content 的高度相同。遇到此问题的常见情况是那些对 Web 开发一无所知并使用 a 的设计师。很多阴影;只是假装粉色部分是一个阴影,我需要向下延伸 #content 的右边缘:
是的,我知道你现在可以用 CSS 做阴影,即使在 IE 中也可以进行一些修改,但在这种情况下。我的设计问题阻止我这样做如果
没有 JavaScript,这真的不可能吗?
I like to think I'm a pretty good... well, passable... web developer. I've built probably... IDK, 50 web sites, with a good amount of Flash and Javascript, and a few database-backed applications, and done enough graphic design to be able to competently fake it :)
I've run across this problem half a dozen times in my career: How do you set an inner div to 100% height of the containing div when the container's height is variable? I know you can do it with Javascript or use faux columns, but I swear to dog, if there is no way to do it with pure HTML/CSS I'm going to be very disappointed in life and the W3C :(
Here be the mockup matey: http://random.siliconrockstar.com/heightTest/
See, the inner div #content pushes #container down. I want the pink part, #shadow, to be the same height as #content. The usual case when I run into this problem is with designers who don't know anything about web development and use a lot of drop shadows; just pretend the pink part is a dropshadow I need to extend down the right edge of #content.
Aside: yes, I know you can do dropshadows with CSS now, even in IE with some hacks, but in this case I have design issues preventing me from doing that.
Is this really impossible without Javascript? :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果IE6不是主题,你可以这样做:
If IE6 is no topic, you can do:
我认为没有 JavaScript 是不可能的,但根据你的需要,你可以使用这种东西: http://matthewjamestaylor.com/blog/equal-height-columns-cross-browser-css-no-hacks
I think it's impossible without JavaScript but according what you need you can use this kind of stuff: http://matthewjamestaylor.com/blog/equal-height-columns-cross-browser-css-no-hacks
我想也许新的 css3 东西会开始帮助解决这个问题:
例如
(您可能需要
-moz-
和-webkit-
前缀)I'm thinking that maybe the new css3 stuff will start to help with this:
e.g.
(you may need the
-moz-
and-webkit-
prefixes for this)对于粉色的那一款
For the pink one
出色地。有多种方法可以做到这一点。最古老的一种是使用大底部填充和大负底部边距,如下所述: http://www.alphapapahotel.com/equal-height-div-columns-100-percent/css/
但这种方式有它的缺点:你需要设置父母的元素溢出隐藏以使其工作。因此,如果您将绝对定位元素置于内部以使其溢出,则不能。另外,如果区域位于“overflow:hidden;”内div 具有锚点(例如 ),div 内的区域将滚动,而不是页面。
另一种方法是使用表格单元格属性: http://jsfiddle.net/8UPeR/ 。在旧版浏览器中不起作用。
但要将 #shadow 拉伸到 #content 的高度,有更简单的方法,例如使用伪元素 ( http: //jsfiddle.net/PWMsz/1/ )或“位置:绝对”,如topek的示例中所示。
Well. there is a number of ways to do it. Oldest one is to use big bottom padding and big negative bottom margin as as explained there: http://www.alphapapahotel.com/equal-height-div-columns-100-percent/css/
But this way have it's drawbacks: You need to set parent's element overflow to hidden to make it work. So if you inner absolute positioned elements to overflow it, you can't. Also if area inside "overflow: hidden;" div have anchor (<a name="first-paragraph"></a> for example) the area inside the div will be scrolled, not the page.
Another way is to use table-cell property: http://jsfiddle.net/8UPeR/ . Didn't work in older browsers through.
But to just stretch #shadow to the height of the #content there are easier ways, like using pseudo-elemens ( http://jsfiddle.net/PWMsz/1/ ) or "position: absolute" as in topek's example.