使外部div与其浮动内容自动具有相同的高度

编程入门 行业动态 更新时间:2024-10-22 20:41:35
本文介绍了使外部div与其浮动内容自动具有相同的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想要外部 div ,它是黑色的,包装它的 div 浮动。我不想在 div 中使用 outerdiv 使用 style ='height:200px / code> id,因为我想让它自动的是其内容的高度(例如,浮动 div s)。

I want the outer div, which is black to wrap its divs floating within it. I dont want to use style='height: 200px in the div with the outerdiv id as I want it to be automatically the height of its content (eg, the floating divs).

<div id='outerdiv' style='border: 1px solid black;background-color: black;'> <div style='width=300px;border: red 1px dashed;float: left;'> <p>xxxxxxxxxxxxxxxxxxxxxxxxxxxxx</p> </div> <div style='width=300px;border: red 1px dashed;float: right;'> <p>zzzzzzzzzzzzzzzzzzzzzzzzzzzzz</p> </div>

如何实现? / p>

How to achieve this?

推荐答案

您可以将 outerdiv 的CSS设置为

You can set the outerdiv's CSS to this

#outerdiv { overflow: hidden; /* make sure this doesn't cause unexpected behaviour */ }

通过在 clear:both 的末尾添加一个元素来做到这一点。这可以正常添加JS(不是一个好的解决方案)或:之后 CSS伪元素(在旧IE不广泛支持)。

You can also do this by adding an element at the end with clear: both. This can be added normally, with JS (not a good solution) or with :after CSS pseudo element (not widely supported in older IEs).

问题是容器不会自然扩展以包括浮动的子项。使用第一个示例警告,如果父元素之外有任何子元素,它们将被隐藏。您还可以使用auto作为属性值,但如果任何元素出现在外面,这将调用滚动条。

The problem is that containers won't naturally expand to include floated children. Be warned with using the first example, if you have any children elements outside the parent element, they will be hidden. You can also use 'auto' as the property value, but this will invoke scrollbars if any element appears outside.

您也可以尝试浮动父容器,您的设计,这可能是不可能/困难的。

You can also try floating the parent container, but depending on your design, this may be impossible/difficult.

更多推荐

使外部div与其浮动内容自动具有相同的高度

本文发布于:2023-10-08 01:44:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471150.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:高度   内容   div

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!