官术网_书友最值得收藏!

  • Mastering CSS
  • Rich Finelli
  • 135字
  • 2021-07-08 09:45:59

The float method

We can prevent elements from collapsing by floating the container to the left or to the right. Let's do this; let's add float: left or float: right to our secondary-section. Either will work:

.secondary-section { 
  margin-bottom: 50px; 
  background-color: #7EEEAF; 
  float: left; 
} 

Once we save this, we will see that we have the green background, so the collapse is no longer taking place, but the obvious problem is we've floated to the left. We wanted this div to be centered:

This method has an obvious drawback. There are some circumstances and some situations where it would be a perfect solution, but in this one, there's an obvious problem: we are no longer centered. Remove float: left from your CSS and explore my favorite, and I think the best, solution: the clearfix hack.

主站蜘蛛池模板: 武清区| 三亚市| 淅川县| 漯河市| 平陆县| 海南省| 安远县| 湾仔区| 黄石市| 天镇县| 延津县| 阿图什市| 杭锦旗| 九龙县| 东宁县| 公主岭市| 林周县| 嘉鱼县| 昭通市| 安国市| 荃湾区| 融水| 正定县| 永清县| 庆城县| 孟村| 湾仔区| 山阳县| 普格县| 襄樊市| 普宁市| 临澧县| 长乐市| 卓资县| 武鸣县| 盖州市| 广汉市| 黎川县| 清徐县| 仁化县| 特克斯县|