- 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.
推薦閱讀
- Learning Docker
- DevOps入門與實踐
- 我的第一本算法書
- JavaScript+jQuery開發實戰
- Java Web程序設計
- C語言實驗指導及習題解析
- Building Minecraft Server Modifications
- Python:Master the Art of Design Patterns
- Visual Basic程序設計(第三版)
- Web前端開發技術:HTML、CSS、JavaScript
- JavaScript Mobile Application Development
- jQuery Essentials
- Sony Vegas Pro 11 Beginner’s Guide
- Learning ClojureScript
- jQuery Mobile從入門到精通