- 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.
推薦閱讀
- 零基礎玩轉區塊鏈
- VSTO開發入門教程
- Servlet/JSP深入詳解
- Java程序設計與計算思維
- Mastering LibGDX Game Development
- Building Minecraft Server Modifications
- Learning Salesforce Einstein
- The Complete Coding Interview Guide in Java
- Flutter跨平臺開發入門與實戰
- Go語言精進之路:從新手到高手的編程思想、方法和技巧(2)
- Android Game Programming by Example
- Learning GraphQL and Relay
- Building Microservices with .NET Core 2.0(Second Edition)
- Python量子計算實踐:基于Qiskit和IBM Quantum Experience平臺
- Computer Vision with Python 3