HTML/CSS: Clear floating elements in the middle without adding unneeded tags

妖精的绣舞 提交于 2019-12-03 15:48:42

Just use clear: both; on the 3rd element, this way you don't have to use <div style="clear: both;"></div> after the floated elements.

.child:nth-child(3) {
    background: blue;
    color: white;
    clear: both;
}

Demo


Also, if whenever you are looking to clear a parent element without adding an extra element or using overflow: hidden; which is a dirty way to clear floats, you can call a class on the parent element, with the properties below

.clear:after {
   content: "";
   display: table;
   clear: both;
}

For example

<div class="wrapper clear">
    <div class="left_floated_div"></div>
    <div class="right_floated_div"></div>
</div>

adding a clear:both; in the 3rd child helps:

.child:nth-child(3) {
    background: blue;
    color: white;
    clear:both;
}

You have to use the clear property.

For your example:

.child:nth-child(3) {
    clear: both;
}

In jsfiddle.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!