浮动元素排列规则、贴靠与字围现象

本文深入解析浮动元素在网页布局中的行为特性,包括排列规则、贴靠现象及字围效应,通过实例代码展示不同方向浮动元素如何影响页面布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

浮动元素排列规则:

  • 相同方向上的浮动元素,先浮动的会显示在前面,后浮动的会显示在后面;
  • 元素浮动后的位置,由元素在标准流中的位置决定。
<!doctype html>
<html>
	<head>
	<meta charset="UTF-8">
	<title>标题</title>
	<style>
.cc {
	height: 800px;
	width: 800px;
	border: 1px solid;
}
.c1 {
	height: 100px;
	width: 100px;
	background-color: red;
	float: left;
}
.c2 {
	height: 200px;
	width: 200px;
	background-color: blue;
	float: left;
}
.c3 {
	height: 50px;
	width: 100px;
	background-color: yellow;
	float: left;
}
.c4 {
	height: 100px;
	width: 100px;
	background-color: red;
	float: right;
}
.c5 {
	height: 200px;
	width: 200px;
	background-color: blue;
	float: right;
}
.c6 {
	height: 50px;
	width: 50px;
	background-color: yellow;
	float: right;
}
</style>
	</head>
	<body>
    <div  class="cc">
      <div class="c1"></div>
      <div class="c2"></div>
      <div class="c3"></div>
      <div class="c4"></div>
      <div class="c5"></div>
      <div class="c6"></div>
    </div>
</body>
</html>

 

浮动元素贴靠现象:

当同一方向上的浮动元素超过父元素的宽度时,浮动元素会自动与上一元素贴靠在同一个元素上。

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>标题</title>
<style>
.cc {
	height: 400px;
	width: 300px;
	border: 1px solid;
}
.c1 {
	height: 300px;
	width: 100px;
	background-color: red;
	float: left;
}
.c2 {
	height: 200px;
	width: 200px;
	background-color: blue;
	float: left;
}
.c3 {
	height: 50px;
	width: 100px;
	background-color: yellow;
	float: left;
}
</style>
</head>
<body>
<div  class="cc">
  <div class="c1"></div>
  <div class="c2"></div>
  <div class="c3"></div>
</div>
</body>
</html>

浮动元素字围现象:

当存在两个元素,一个元素为浮动元素,而另一个元素为标准元素时,标准元素中的内容会自动围绕在浮动元素四周。

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>标题</title>
<style>
.c1 {
	height: 50px;
	width: 50px;
	background-color: red;
	float: left;
}
.c2 {
	height: 200px;
	width: 200px;
	background-color: blue;
}
</style>
</head>
<body>
<div class="c1"></div>
<div class="c2">我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容我是内容</div>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值