How to make child divs always fit inside parent div?

后端 未结 11 703
北海茫月
北海茫月 2020-12-08 09:13

My question is if there is a way, without using JavaScript, to cause child divs to extend to the borders of their parent, without exceeding those borders, when you cannot kn

11条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 09:31

    If I've understood you correctly, the easiest method is to float the children. For example:

    #one { width: 500px; height: 1%; overflow: hidden; background: red; }
    #two { float: left; width: 250px; height: 400px; background: aqua; }
    #two { float: left; width: 250px; height: 200px; background: lime; }
    

    Setting a dimension (height/width) and overflow to auto or hidden on the parent element causes it to contain any floated child elements.

    Note that overflow:hidden; can occasionally cause problems with content getting cut off, in which case you might want to try this alternative method:

    http://www.positioniseverything.net/easyclearing.html

提交回复
热议问题