浮动
浮动流:块级元素无法识别浮动流元素的位置
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.left {
width: 100px;
height: 100px;
float: left;
background-color: green;
}
.right {
height: 200px;
width: 200px;
background-color: orange;
}
</style>
</head>
<body>
<div class="left">
</div>
<div class="right"></div>
</body>
</html
两个位置层叠到了一起
内联,内联块,浮动,溢出隐藏,纯文本都可以是被浮动元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.left {
width: 100px;
height: 100px;
float: left;
background-color: green;
}
.right {
height: 200px;
width: 200px;
background-color: orange;
}
</style>
</head>
<body>
<div class="left">
</div>
<span>123</span>
</body>
</html>
文本显示在旁边
清除浮动,使用伪元素是一种较好的解决方案。
没使用伪元素 ,可看下面效果图。
使用伪元素后。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
ul::after,
div::after {
content: "";
display: block;
clear: both;
}
.box {
width: 200px;
border: 10px solid black;
}
.box1 {
float: left;
width: 100px;
height: 100px;
background-color: green;
}
.box2 {
float: left;
width: 100px;
height: 100px;
background-color: orange;
}
</style>
</head>
<body>
<div class="box">
<div class="box1"></div>
<div class="box2"></div>
</div>
</body>
</html>
效果如下:
伪元素::before
清除浮动小案例
```css
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.slider{
width: 590px;
height: 470px;
position: relative;
}
.slider .indicator{
width: 152px;
height: 18px;
position: absolute;
bottom: 36px;
left: 18px;
}
.slider .indicator .indicator-dot{
display: block;
float: left;
width: 18px;
height: 18px;
position: relative;
/* border: 1px solid black; */
box-sizing: border-box;
}
.slider .indicator .indicator-dot::after{
content: "";
display: block;
width: 6px;
height: 6px;
border: 2px solid white;
position: absolute;
top: 4px;
left: 4px;
border-radius: 50%;
opacity: .4;
}
.slider .indicator .indicator-dot.active::before{
display: block;
content: "";
width: 18px;
height: 18px;
background-color: white;
opacity: .2;
border-radius: 50%;
}
.slider .indicator .indicator-dot.active::after{
opacity: 1;
background-color: #fff;
}
</style>
</head>
<body>
<div class="slider">
<div class="indicator">
<i class="indicator-dot active"></i>
<i class="indicator-dot"></i>
<i class="indicator-dot"></i>
<i class="indicator-dot"></i>
<i class="indicator-dot"></i>
<i class="indicator-dot"></i>
<i class="indicator-dot"></i>
<i class="indicator-dot"></i>
</div>
<img src="img/q.jpg" alt=""/>
</div>
</body>
</html>
实现如下效果: