分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

CSS深入理解之float(HTML/CSS)

发布时间:2023-09-06 01:33责任编辑:彭小芳关键词:CSSHTMLfloat

float的设计初衷仅仅是:为了文字环绕效果

float的包裹与破坏

包裹:收缩、坚挺、隔绝(BFC)

破坏:父元素高度塌陷

<!DOCTYPE html><html> ?<head> ???<meta charset="utf-8" /> ???<title>Layout</title> ???<style> ?????.box{ ?????????border: 1px solid red; ?????????width: 300px; ?????????margin: 20px auto; ?????} ?????.left{ ?????????float: left; ?????????height: 100px; ?????????width: 50px; ?????????border: 1px solid black; ?????} ?????.right{ ?????????float: right; ?????????height: 100px; ?????????width: 50px; ?????????border: 1px solid green; ?????} ???</style> ?</head> ?<body> ???<div class="box"> ???????<div class="left"></div> ???????<div class="right"></div> ???</div> ?</body></html>
View Code

如何降低float破坏性的影响(清除浮动)?

1、底部插入clear:both;

<!DOCTYPE html><html> ?<head> ???<meta charset="utf-8" /> ???<title>Layout</title> ???<style> ?????.box { ???????border: 1px solid red; ???????width: 300px; ???????margin: 20px auto; ?????} ?????.clearfix { ???????clear: both; ?????} ?????.left { ???????float: left; ???????height: 100px; ???????width: 50px; ???????border: 1px solid black; ?????} ?????.right { ???????float: right; ???????height: 100px; ???????width: 50px; ???????border: 1px solid green; ?????} ???</style> ?</head> ?<body> ???<div class="box"> ?????<div class="left"></div> ?????<div class="right"></div> ?????<div class="clearfix"></div> ???</div> ?</body></html>
View Code
<!DOCTYPE html><html> ?<head> ???<meta charset="utf-8" /> ???<title>Layout</title> ???<style> ?????.box { ???????border: 1px solid red; ???????width: 300px; ???????margin: 20px auto; ?????} ?????.box:after { ???????content: ‘‘; ???????display: block; ???????height: 0; ???????overflow: hidden; ???????clear: both; ?????} ?????.left { ???????float: left; ???????height: 100px; ???????width: 50px; ???????border: 1px solid black; ?????} ?????.right { ???????float: right; ???????height: 100px; ???????width: 50px; ???????border: 1px solid green; ?????} ???</style> ?</head> ?<body> ???<div class="box"> ?????<div class="left"></div> ?????<div class="right"></div> ???</div> ?</body></html>
View Code
<!DOCTYPE html><html> ?<head> ???<meta charset="utf-8" /> ???<title>Layout</title> ???<style> ?????.box { ???????border: 1px solid red; ???????width: 300px; ???????margin: 20px auto; ?????} ?????.box:after { ???????content: ‘‘; ???????display: table; ???????clear: both; ?????} ?????.left { ???????float: left; ???????height: 100px; ???????width: 50px; ???????border: 1px solid black; ?????} ?????.right { ???????float: right; ???????height: 100px; ???????width: 50px; ???????border: 1px solid green; ?????} ???</style> ?</head> ?<body> ???<div class="box"> ?????<div class="left"></div> ?????<div class="right"></div> ???</div> ?</body></html>
View Code

2、父元素BFC/haslayout

<!DOCTYPE html><html> ?<head> ???<meta charset="utf-8" /> ???<title>Layout</title> ???<style> ?????.box { ???????border: 1px solid red; ???????width: 300px; ???????margin: 20px auto; ???????overflow: hidden; ?????} ?????.left { ???????float: left; ???????height: 100px; ???????width: 50px; ???????border: 1px solid black; ?????} ?????.right { ???????float: right; ???????height: 100px; ???????width: 50px; ???????border: 1px solid green; ?????} ???</style> ?</head> ?<body> ???<div class="box"> ?????<div class="left"></div> ?????<div class="right"></div> ???</div> ?</body></html>
View Code

float的特性

1、元素block块状化(砖头化);

2、破坏性造成的紧密排列特性(去空格化)。

CSS深入理解之float(HTML/CSS)

原文地址:https://www.cnblogs.com/fengxiongZz/p/8148673.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved