Floating elements within a div, floats outside of div. Why?

前端 未结 10 973
灰色年华
灰色年华 2020-11-22 05:10

Say you have a div, give it a definite width and put elements in it, in my case an img and another div.

The idea

10条回答
  •  再見小時候
    2020-11-22 05:32

    The easiest is to put overflow:hidden on the parent div and don't specify a height:

    #parent { overflow: hidden }
    

    Another way is to also float the parent div:

    #parent { float: left; width: 100% }
    

    Another way uses a clear element:

    CSS

    span.clear { clear: left; display: block; }
    

提交回复
热议问题