父浮动元素不会随子元素的百分比宽度调整大小
我有一个浮动父 div,其中包含一个使用 width: 30%
的子元素。
如果使用百分比作为宽度单位,这个父 div 似乎不会调整大小,但在使用 px 或 ems 时效果很好。
因为我想要一个流畅的布局,所以在使用 width: 30%
时有什么方法可以让父 div 调整其宽度吗?
请查看此示例以了解更多详细信息: http://jsfiddle.net/sBzYH/
<div class='percent container'>
<div>
<h3>width: 30%</h3>
<p>
When using the child div
with percent width, the floating
parent div keeps expading 100%.
</p>
</div>
</div>
<div class='pixel container'>
<div>
<h3>width: 100px</h3>
<p>
When using the child div
with pixel width, the floating
parent div resizes accordingly.
</p>
</div>
</div>
I have a floating parent div containing a child using width: 30%
.
It seems this parent div won't resize if using percent as the width unit, but it works well when using px or ems.
Is there any way to make the parent div adjust its width when using width: 30%
since I would like to have a fluid layout?
Please have a look at this example for more details:
http://jsfiddle.net/sBzYH/
<div class='percent container'>
<div>
<h3>width: 30%</h3>
<p>
When using the child div
with percent width, the floating
parent div keeps expading 100%.
</p>
</div>
</div>
<div class='pixel container'>
<div>
<h3>width: 100px</h3>
<p>
When using the child div
with pixel width, the floating
parent div resizes accordingly.
</p>
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
父容器已经是流体宽度,因为它是一个
div
,它将默认为完整可用宽度,但您可以明确限制它。 此 CSS 将使您的两个示例大小大致相同:如果可用的大小超过(略低于)500px对于容器,它将扩展以占据 66% 的可用空间,但它将保持足够的宽度,使得 30% 宽度的子元素始终至少为 100px 宽。
The parent container will already be a fluid width, because it's a
div
it will default to the full available width but you can explicitly limit that. This CSS will make your two examples approximately the same size:If there's more than (just under) 500px available for the container then it will expand to take up 66% of the available space, but it will stay wide enough that the 30% width child element is always at least 100px wide.