css float浮动布局是目前主流布局方式,float有4种方式,常用的有两种left、right;浮动样式代码怎么写,在使用浮动后如何清除浮动
目录
1css浮动右浮动float: right
2css浮动左浮动float: left
3css浮动清除属性clear
css浮动右浮动float: right
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=yes">
<title>通高科技</title>
<style>
.div {
background-color: yellowgreen;
width: 100px;
height: 100px;
border: 1px dotted black;
margin-left: 40px;
float: left;
margin-top: 10px;
}
.div2{
float: right;
}
</style>
</head>
<body>
<div >
<div class="div">左浮动1</div>
<div class="div div2">右浮动2</div>
<div class="div div2">右浮动3</div>
</div>
</body>
</html>
运行结果:
css浮动左浮动float: left
向左浮动代码表示:float: left;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=yes">
<title>通高科技</title>
<style>
.div {
background-color: yellowgreen;
width: 100px;
height: 100px;
border: 1px dotted black;
margin-left: 40px;
float: left;
margin-top: 10px;
}
</style>
</head>
<body>
<div >
<div class="div">左浮动1</div>
<div class="div">左浮动2</div>
<div class="div">左浮动3</div>
</div>
</body>
</html>
运行结果:
css浮动清除属性clear
clear 属性规定哪些元素可以在清除的元素旁边以及在哪一侧浮动。(left、right、both、none、inherit)
实例:5个左浮动
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=yes">
<title>通高科技</title>
<style>
.div {
background-color: yellowgreen;
width: 100px;
height: 100px;
border: 1px dotted black;
margin-left: 40px;
float: left;
}
.div1{
float: left;
background-color: yellowgreen;
width: 100px;
height: 100px;
border: 1px dotted black;
margin-left: 40px;
margin-top: 10px;
clear: left;
}
</style>
</head>
<body>
<div >
<div class="div">左浮动1</div>
<div class="div">左浮动2</div>
<div class="div ">左浮动3</div>
<div class="div">左浮动4</div>
<div class="div">左浮动5</div>
</div>
</body>
</html>
运行效果:
需求:对3号块取消左浮动
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=yes">
<title>通高科技</title>
<style>
.div {
background-color: yellowgreen;
width: 100px;
height: 100px;
border: 1px dotted black;
margin-left: 40px;
float: left;
margin-top: 10px;
}
.div1{
float: left;
background-color: yellowgreen;
width: 100px;
height: 100px;
border: 1px dotted black;
margin-left: 40px;
margin-top: 10px;
clear: left;
}
</style>
</head>
<body>
<div >
<div class="div">左浮动1</div>
<div class="div">左浮动2</div>
<div class="div div1">左浮动3</div>
<div class="div">左浮动4</div>
<div class="div">左浮动5</div>
</div>
</body>
</html>
运行结果:
对3号块取消浮动后。3号块没有浮动,4号.5号块继续左浮动。