如何使浮动div的高度达到其父级的100%?
这是 HTML:
<div id="outer">
<div id="inner"></div>
Test
</div>
这是 CSS:
#inner {
float: left;
height: 100%;
}
使用 Chrome 开发者工具检查后,内部 div 的高度为 0px。
如何强制它为父 div 高度的 100%?
Here is the HTML:
<div id="outer">
<div id="inner"></div>
Test
</div>
And here is the CSS:
#inner {
float: left;
height: 100%;
}
Upon inspection with the Chrome developer tools, the inner div is getting a height of 0px.
How can I force it to be 100% of the height of the parent div?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
对于父级:
您应该添加一些前缀 http://css-tricks.com/using-flexbox/< /a>
编辑:
唯一的缺点是IE一如既往,IE9不支持flex。
http://caniuse.com/flexbox
编辑2:
正如 @toddsby 所指出的,对齐项目是针对父项的,其默认值实际上是 stretch。如果你想要子元素有不同的值,可以使用align-self属性。
编辑3:
jsFiddle: https://jsfiddle.net/bv71tms5/2/
For the parent:
You should add some prefixes http://css-tricks.com/using-flexbox/
Edit:
Only drawback is IE as usual, IE9 does not support flex.
http://caniuse.com/flexbox
Edit 2:
As @toddsby noted, align items is for parent, and its default value actually is stretch. If you want a different value for child, there is align-self property.
Edit 3:
jsFiddle: https://jsfiddle.net/bv71tms5/2/
要使
#outer
高度基于其内容,并让#inner
以其高度为基础,使两个元素绝对定位。更多详细信息可以在 css height 属性,但本质上,如果
#outer
的高度为auto
,#inner
必须忽略#outer
高度, 除非#outer
是绝对定位的。那么#inner
高度将为0,除非#inner
本身是绝对定位的。但是...通过绝对定位
#inner
,float
设置将被忽略,因此您需要显式选择#inner
的宽度,并在#outer
中添加填充以伪造我怀疑您想要的文本换行。例如,下面的#outer
的填充是#inner
的宽度+3。方便的是(因为重点是让#inner
高度达到 100%),不需要在#inner
下面包裹文本,所以这看起来就像#内部
是浮动的。我删除了之前的答案,因为它基于对您的目标的太多错误假设。
For
#outer
height to be based on its content, and have#inner
base its height on that, make both elements absolutely positioned.More details can be found in the spec for the css height property, but essentially,
#inner
must ignore#outer
height if#outer
's height isauto
, unless#outer
is positioned absolutely. Then#inner
height will be 0, unless#inner
itself is positioned absolutely.However... By positioning
#inner
absolutely, afloat
setting will be ignored, so you will need to choose a width for#inner
explicitly, and add padding in#outer
to fake the text wrapping I suspect you want. For example, below, the padding of#outer
is the width of#inner
+3. Conveniently (as the whole point was to get#inner
height to 100%) there's no need to wrap text beneath#inner
, so this will look just like#inner
is floated.I deleted my previous answer, as it was based on too many wrong assumptions about your goal.
其实只要父元素定位好,就可以将子元素的高度设置为100%。也就是说,如果您不希望父级处于绝对定位。让我进一步解释一下:
Actually, as long as the parent element is positioned, you can set the child's height to 100%. Namely, in case you don't want the parent to be absolutely positioned. Let me explain further:
只要您不需要支持 IE8 之前的 Internet Explorer 版本,就可以使用
display: table-cell
来完成此操作:HTML:
CSS :
这将强制具有
.inner
类的每个元素占据其父元素的整个高度。As long as you don't need to support versions of Internet Explorer earlier than IE8, you can use
display: table-cell
to accomplish this:HTML:
CSS:
This will force each element with the
.inner
class to occupy the full height of its parent element.我做了一个示例来解决您的问题。
你必须制作一个包装器,将其浮动,然后绝对定位你的 div 并赋予它 100% 的高度。
HTML
CSS:
说明: .right div 是绝对定位的。这意味着只有在 CSS 中显式声明宽度或高度属性时,才会根据绝对或相对定位的第一个父 div 来计算其宽度和高度以及顶部和左侧位置;如果没有显式声明,这些属性将根据父容器 (.right-wrapper) 计算。
因此,DIV的100%高度将根据.container最终高度计算,.right位置的最终位置将根据父容器计算。
I made an example resolving your problem.
You have to make a wrapper, float it, then position absolute your div and give to it 100% height.
HTML
CSS:
Explanation: The .right div is absolutely positioned. That means that its width and height, and top and left positiones will be calculed based on the first parent div absolutely or relative positioned ONLY if width or height properties are explicitly declared in CSS; if they aren't explicty declared, those properties will be calculed based on the parent container (.right-wrapper).
So, the 100% height of the DIV will be calculed based on .container final height, and the final position of .right position will be calculed based on the parent container.
这里有一个更简单的方法来实现这一点:
感谢 浮动 div,100% 高度中的 @Itay
Here it is a simpler way to achieve that:
Thanks to @Itay in Floated div, 100% height
如果您准备使用一点 jQuery,答案很简单!
这对于将单个元素浮动到其父元素高度为 100% 的一侧非常有效,而通常会环绕的其他浮动元素则保留在一侧。
希望这对 jQuery 粉丝有所帮助。
If you're prepared to use a little jQuery, the answer is simple!
This works well for floating a single element to a side with 100% height of it's parent while other floated elements which would normally wrap around are kept to one side.
Hope this helps fellow jQuery fans.
这是等高网格系统的代码示例。
上面是外部 div 的 CSS
上面是内部 div
希望这对你有帮助
This is a code sample for grid system with equal height.
Above is the CSS for outer div
Above is the inner div
Hope this help you
这对我有帮助。
更改 right: 和 left: 以设置首选的 #inner 宽度。
This helped me.
Change right: and left: to set preferable #inner width.
当您需要多个子元素具有相同高度时,可以使用 flexbox 解决类似的情况:
https:// css-tricks.com/using-flexbox/
为父元素设置
display: flex;
,为子元素设置flex: 1;
,它们都将具有相同的高度。A similar case when you need several child elements have the same height can be solved with flexbox:
https://css-tricks.com/using-flexbox/
Set
display: flex;
for parent andflex: 1;
for child elements, they all will have the same height.适用于新绿色浏览器的简单解决方案。
答案是-webkit-fill-available
Simple solution for the new green browsers.
Answer is -webkit-fill-available
尝试
在以下位置显示更多选项:
如何防止浮动元素的父元素折叠?< /a>
try
show more options in:
How do you keep parents of floated elements from collapsing?