- 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.
推薦閱讀
- C++面向對象程序設計(第三版)
- Python 3.7網絡爬蟲快速入門
- 自己動手實現Lua:虛擬機、編譯器和標準庫
- OpenCV for Secret Agents
- C語言程序設計案例精粹
- Microsoft System Center Orchestrator 2012 R2 Essentials
- Building Wireless Sensor Networks Using Arduino
- HTML5開發精要與實例詳解
- Java EE企業級應用開發教程(Spring+Spring MVC+MyBatis)
- Everyday Data Structures
- Cinder:Begin Creative Coding
- Isomorphic JavaScript Web Development
- JavaScript前端開發程序設計教程(微課版)
- 計算機信息技術實踐教程
- 寫給所有人的編程思維