给父元素设置宽高:
由于浮动后的元素脱离文档流,无法撑起父元素的宽高,可以设置父元素的宽高。
<style>
*{
margin: 0;
padding: 0;
}
.box1{
background-color: red;
/*这里*/
height: 50px;
}
.box2{
background-color: lightblue;
}
ul{
list-style: none;
}
.ul1 li{
background-color: blue;
}
.ul2 li{
background-color: green;
}
ul li{
float: left;
}
</style>
<div class="box1">
<ul class="ul1">
<li>张三</li>
<li>李四</li>
<li>王麻子</li>
</ul>
</div>
<div class="box2">
<ul class="ul2">
<li>天朗</li>
<li>气清</li>
<li>风和</li>
</ul>
</div>
CSS的clear属性:
给后面的盒子元素添加clear属性:
clear属性取值:
none: 默认取值, 按照浮动元素的排序规则来排序(左浮动找左浮动, 右浮动找右浮动);
left: 不要找前面的左浮动元素;
right: 不要找前面的右浮动元素;
both: 不要找前面的左浮动元素和右浮动元素。
注意点:
当我们给某个元素添加clear属性之后, 那么这个属性的margin属性就会失效。
<style>
*{
margin: 0;
padding: 0;
}
.box1{
background-color: red;
}
.box2{
background-color: lightblue;
clear: both;
}
ul{
list-style: none;
}
.ul1 li{
background-color: blue;
}
.ul2 li{
background-color: green;
}
ul li{
float: left;
}
</style>
<div class="box1">
<ul class="ul1">
<li>张三</li>
<li>李四</li>
<li>王麻子</li>
</ul>
</div>
<div class="box2">
<ul class="ul2">
<li>天朗</li>
<li>气清</li>
<li>风和</li>
</ul>
</div>
隔墙法:
- 外墙法:
- 在两个盒子中间添加一个额外的块级元素;
- 给这个额外添加的块级元素设置clear: both;属性。
<style>
*{
margin: 0;
padding: 0;
}
.box1{
background-color: red;
}
.box2{
background-color: lightblue;
clear: both;
}
ul{
list-style: none;
}
.ul1 li{
background-color: blue;
}
.ul2 li{
background-color: green;
}
ul li{
float: left;
}
/*这里*/
.wall{
clear: both;
}
.h2{
height:20px;
background-color: deepskyblue;
</style>
<div class="box1">
<ul class="ul1">
<li>张三</li>
<li>李四</li>
<li>王麻子</li>
</ul>
</div>
<div class="box2">
<ul class="ul2">
<li>天朗</li>
<li>气清</li>
<li>风和</li>
</ul>
</div>
注意点:
外墙法它可以让第二个盒子使用margin-top属性;
外墙法不可以让第一个盒子使用margin-bottom属性。
- 内墙法
- 在第一个盒子中所有子元素最后添加一个额外的块级元素
- 给这个额外添加的块级元素设置clear: both;属性
<style>
*{
margin: 0;
padding: 0;
}
.box1{
background-color: red;
}
.box2{
background-color: lightblue;
clear: both;
}
ul{
list-style: none;
}
.ul1 li{
background-color: blue;
}
.ul2 li{
background-color: green;
}
ul li{
float: left;
}
/*这里*/
.wall{
clear: both;
}
</style>
<div class="box1">
<ul class="ul1">
<li>张三</li>
<li>李四</li>
<li>王麻子</li>
</ul>
<div class="wall"></div>
</div>
<div class="box2">
<ul class="ul2">
<li>天朗</li>
<li>气清</li>
<li>风和</li>
</ul>
</div>
注意点:
内墙法它可以让第二个盒子使用margin-top属性;
内墙法它可以让第一个盒子使用margin-bottom属性。
3.外墙法和内墙法区别?
外墙法不能撑起第一个盒子的高度, 而内墙法可以撑起第一个盒子的高度。
伪元素选择器清除
伪元素选择器作用就是给指定标签的内容前面添加一个子元素或者给指定标签的内容后面添加一个子元素。
格式:
标签名称::before{
属性名称:值;
}
给指定标签的内容前面添加一个子元素;
标签名称::after{
属性名称:值;
}
给指定标签的内容后面添加一个子元素。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>68-伪元素选择器</title>
<style>
* {
margin: 0;
padding: 0;
}
div{
width: 200px;
height: 200px;
background-color: red;
}
/*
p{
width: 50px;
height: 50px;
background-color: pink;
}
*/
div::before{
content: "爱你";
width: 50px;
height: 50px;
background-color: pink;
display: block;
}
div::after{
/*指定添加的子元素中存储的内容*/
content: "么么哒";
/*指定添加的子元素的宽度和高度*/
width: 50px;
/*height: 50px;*/
/*内容是可以超出标签的范围的, 所以高度为0依然可以看见内容*/
height:0;
background-color: pink;
/*指定添加的子元素的显示模式*/
display: block;
/*隐藏添加的子元素*/
visibility: hidden;
}
</style>
</head>
<body>
<div>
<!--<p>爱你</p>-->
我是文字
<!--<p>么么哒</p>-->
</div>
</body>
</html>
利用伪元素选择器清除浮动本质上就是内墙法, 只不过是直接通过CSS代码添加了内墙, 其它特性和内墙法都一样。
注意点:
IE6中不支持这种方式, 为了兼容IE6必须给前面的盒子添加*zoom:1;属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>69-清除浮动方式四</title>
<style>
*{
margin: 0;
padding: 0;
}
.box1{
background-color: red;
/*margin-bottom: 10px;*/
}
.box2{
background-color: green;
/*margin-top: 10px;*/
}
.box1 p{
width: 100px;
background-color: blue;
}
.box2 p{
width: 100px;
background-color: yellow;
}
p{
float: left;
}
.box1::after{
/*设置添加的子元素的内容为空*/
content: "";
/*设置添加的子元素为块级元素*/
display: block;
/*设置添加的子元素的高度为0*/
height: 0;
/*设置添加的子元素看不见*/
visibility: hidden;
/*给添加的子元素设置clear: both;*/
clear: both;
}
.box1{
/*兼容IE6*/
*zoom:1;
}
</style>
</head>
<body>
<div class="box1">
<p>我是文字1</p>
<p>我是文字1</p>
<p>我是文字1</p>
</div>
<div class="box2">
<p>我是文字2</p>
<p>我是文字2</p>
<p>我是文字2</p>
</div>
</body>
</html>
overflow:hidden
- 可以将超出标签范围的内容裁剪掉;
- 清除浮动;
- 可以通过overflow: hidden;让里面的盒子设置margin-top之后, 外面的盒子不被顶下来。
网友评论