How to make child divs always fit inside parent div?

后端 未结 11 697
北海茫月
北海茫月 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:38

    In your example, you can't: the 5px margin is added to the bounding box of div#two and div#three effectively making their width and height 100% of parent + 5px, which will overflow.

    You can use padding on the parent Element to ensure there's 5px of space inside its border:

    
    

    EDIT: In testing, removing the width:100% from div#two will actually let it work properly as divs are block-level and will always fill their parents' widths by default. That should clear your first case if you'd like to use margin.

提交回复
热议问题