欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 锐评 > 浮动元素详解

浮动元素详解

2024/10/24 22:01:03 来源:https://blog.csdn.net/qq_36324341/article/details/142282202  浏览:    关键词:浮动元素详解

浮动元素

代码实现:

<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>浮动元素</title><style>#container1 {width: 400px;height: 50px;background-color: lightgrey;border: 1px solid;}#container1 div {width: 100px;height: 30px;border: 1px solid;background-color: #fff;}.lft {float: left;}.rgh {float: right;}#container2 {width: 400px;height: 100px;background-color: lightgrey;border: 1px solid;}#container2 .lft {width: 300px;height: 20px;border: 1px solid;background-color: #fff;}#container2 .rgh {width: 150px;height: 50px;border: 1px solid;background-color: #fff;}#container3 {width: 400px;height: 150px;background-color: lightgrey;border: 1px solid;}#container3 div {border: 1px solid;background-color: #fff;}#container3 div:nth-child(-n+2) {width: 100px;height: 30px;}.clr {clear: both;}#container3 div:nth-child(4) {width: 190px;height: 40px;}#container3 div:nth-child(5) {width: 120px;height: 20px;}#container3 div:nth-child(n+7) {width: 120px;height: 20px;}</style>
</head><body><h2>只要宽度之和小于父元素宽度,块级元素就在同一行</h2><div id="container1"><div class="lft">float left;</div><div class="lft">float left;</div><div class="rgh">float right;</div></div><h2>两个浮动元素宽度之和大于父元素宽度,其中一个换行</h2><div id="container2"><div class="lft">float:left</div><div class="rgh">float:right</div></div><h2>使用clear:both清除浮动并创建新的浮动行</h2><div id="container3"><div class="lft">float left;</div><div class="lft">float left;</div><div class="clr"></div><div class="lft">float left;</div><div class="rgh">float:right</div><div class="clr"></div><div class="rgh">float:right</div><div class="rgh">float:right</div><div class="rgh">float:right</div></div>
</body></html>

页面效果

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com