Make outer div be automatically the same height as its floating content

后端 未结 7 876
夕颜
夕颜 2020-11-29 00:03

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

7条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 00:28

    You can set the outerdiv's CSS to this

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

    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).

    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.

提交回复
热议问题