css_float_浮动的认识和使用
浮动
reference
- Floats - Learn web development | MDN (mozilla.org)
- 浮动 - 学习 Web 开发 | MDN (mozilla.org)
- All About Floats | CSS-Tricks - CSS-Tricks
Table of contents
float 主要作用
- 浮动可以实现文字环绕图片(包括但不限于于图片)的效果
- 对于不兼容flex的旧浏览器实现多列布局
- 注意浮动的清除(浮动使得浮动元素脱离正常文本流,导致和正常文本流的元素见的关系变得难以控制,因此要及时清除浮动)
- 除此之外,使用flex可以更好的实现多列布局
浮动实例
- 对于列高的不同,有多中可能的处理方案能使得列高看起来是相同的
- flex在这方面会有更好的自适应性,对于flex而言,创建可控的多列布局轻而易举.
试验代码
float · xuchaoxin1375/webLearn - 码云 - 开源中国 (gitee.com)
float背景介绍
- Originally for floating images inside blocks of text, the
float
property became one of the most commonly used tools for creating multiple column layouts on webpages. - With the advent of flexbox and grid it’s now
returned to its original purpose
. - The
float
property was introduced to allow web developers to implement simple layoutsinvolving an image floating inside a column of text
(正如上图效果所示,在html中,位于文本盒子<p>
外的img元素的在渲染出来的效果,犹如图片是<p>
的一个子元素), with the text wrapping around the left or right of it. - The kind of thing you might get in a newspaper layout.
float 的基本工作方式
- float元素将浮动脱离正常文本流,同时影响到附近的元素
- float元素会吸附到父元素边缘(方向根据float的取值而定)
- 我们以
float:left
情况为例: - Any content that would come below the floated element in the normal layout flow will now wrap around it instead
- filling up the space to the right-hand side of it
as far up as the top of the floated element
. There, it will stop.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了