CSS补充2

浮动是css里面布局最多的一个属性
效果:两个元素并排了,并且两个元素都能够设置宽度和高度

四个特性:
1.浮动的元素脱标
2.浮动的元素互相贴靠
3.浮动的元素有“字围”效果
4.收缩的效果

脱标: 脱离了标准文档流
span标签不需要转成块级元素,也能够设置宽高。
所有的标签一旦设置浮动,能够并排,都不区分行内、块状元素,设置宽高

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>浮动</title>
<style type="text/css">
*{
padding: 0;
margin: 0;
}

.box1{
width: 200px;
height: 200px;
background-color: red;
float: left;

}
.box2{
width: 400px;
height: 400px;
background-color: yellow;

}
span{
background-color: green;
float: left;
width: 300px;
height: 50px;
}
</style>
</head>
<body>
<div class="box1">小红</div>
<div class="box2">小黄</div>
<span>span标签</span>
<span>span标签</span>
</body>
</html>
小红盒子浮动了,脱离了标准流,此时小黄这个盒子就是标准文档流中的第一个盒子。
所以就渲染到了左上方。 浮动元素 “飘起来了”


浮动元素当有足够的空间时,会紧贴在一起,没有没有足够的空间,会挨着边靠

所谓字围效果:
当div浮动,p不浮动
div挡住了p,div的层级提高,但是p中的文字不会被遮盖,此时就形成了字围效果
关于浮动我们强调一点,浮动这个元素,我们初期一定要遵循一个原则
永远不是一个盒子单独浮动,要浮动就要一起浮动。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
div{
float: left;
}
p{
background-color: #666;
}
</style>
</head>
<body>
<div>
<img src="./images/picture.png" alt="">
</div>
<p>
123文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字
文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字
文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字
</p>

</body>
</html>

收缩:一个浮动元素。如果没有设置width,那么就自动收缩为文字的宽度(这点跟行内元素很像)


清除浮动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">

*{
padding: 0;
margin: 0;

}
.father{
width: 1126px;
/*子元素浮动 父盒子一般不设置高度*/

/*出现这种问题,我们要清除浮动带来影响*/
/*height: 300px;*/

}
.box1{
width: 200px;

height: 500px;
float: left;
background-color: red;
}
.box2{
width: 300px;
height: 200px;
float: left;
background-color: green;
}
.box3{
width: 400px;
float: left;
height: 100px;
background-color: blue;
}
.father2{
width: 1126px;
height: 600px;
background-color: purple;
}
</style>
</head>
<body>

<div class="father">
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</div>

<div class="father2"></div>


</body>
</html>


给父盒子设置高度,这种方式不灵活。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">

*{
padding: 0;
margin: 0;
}
div{
width: 400px;

/*给父盒子设置高度,这种方式不灵活。
/*固定导航栏*/
/*height: 40px;*/
}
ul{
list-style: none;
height: 40px;
}

div ul li {
float: left;
width: 100px;
height: 40px;
background-color: red;
}
.box{
width: 200px;
height: 100px;
background-color: yellow;
}
</style>
</head>
<body>

<div>
<ul>
<li>Python</li>
<li>web</li>
<li>linux</li>
</ul>
</div>
<div class="box">

</div>
</body>
</html>


给浮动元素最后面加一个空的div 并且该元素不浮动,然后设置clear:both 清除别人对我的浮动影响
内墙法
无缘无故加了div元素 结构冗余


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">

*{
padding: 0;
margin: 0;
}
ul{
list-style: none;

}
div{
width: 400px;

}
div ul li {
float: left;
width: 100px;
height: 40px;
background-color: red;
}
.box{
width: 200px;
height: 100px;
background-color: yellow;
}
.clear{
clear: both;
}
</style>
</head>
<body>

<div>
<ul>
<li>Python</li>
<li>web</li>
<li>linux</li>
</ul>
<div class="clear"></div>
</div>
<div class="box">

</div>
</body>
</html>


清除浮动伪元素方法
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>伪元素清除法(常用)</title>
<style type="text/css">

*{
padding: 0;
margin: 0;
}
ul{
list-style: none;

}


div{
width: 400px;

}

div ul li {
float: left;
width: 100px;
height: 40px;
background-color: red;
}
.box{
width: 200px;
height: 100px;
background-color: yellow;
}
/*伪元素选择器*/
.clearfix:after{
/*必须要写这三句话*/
content: '.';
clear: both;
display: block;
height: 0;
visibility: hidden;

/*
新浪首页清除浮动伪元素方法
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden

*/
}

</style>
</head>
<body>

<div class="clearfix">
<ul>
<li>Python</li>
<li>web</li>
<li>linux</li>

</ul>

</div>
<div class="box">

</div>
</body>
</html>

清除浮动
overflow:hidden

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>overflow除法</title>
<style type="text/css">

*{
padding: 0;
margin: 0;
}
ul{
list-style: none;

}


.box{
width: 400px;
overflow: hidden;
}

.box ul li {
float: left;
width: 100px;
height: 40px;
background-color: red;
}
.box2{
width: 200px;
height: 100px;
background-color: yellow;
}


</style>
</head>
<body>

<div class="box">
<ul>
<li>Python</li>
<li>web</li>
<li>linux</li>

</ul>

</div>
<div class="box2">

</div>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/xfxing/p/9388142.html