我们从最开始讨论如何浮动,以及他们在基本层级是如何工作的,然后是如何设置元素的浮动来影响元素所涉及的盒子边框,让你找出怎么样工作才能根据你想的那样获得边距。
什么是浮动?
<img src="http://lorempixum.com/200/200/" />
<p>Lorem ipsum...</p>
img {
float: right;
margin: 20px;
}
这种盒子是怎么工作的
p {margin: 20px;}
img {margin: 20px;}
p {
border: solid 1px black;
}
img {
float: right;
margin: 20px;
}
p {
float: left;
width: 220px;
margin: 20px;
}
疯狂的浮动规则
<ul>
<li><img src="http://placehold.it/100x100&text=1"/></li>
<li><img src="http://placehold.it/100x150&text=2"/></li>
<li><img src="http://placehold.it/100x100&text=3"/></li>
<li><img src="http://placehold.it/100x100&text=4"/></li>
<li><img src="http://placehold.it/100x100&text=5"/></li>
<li><img src="http://placehold.it/100x150&text=6"/></li>
<li><img src="http://placehold.it/100x100&text=7"/></li>
</ul>
li {
float: left;
margin: 4px;
}
li {
display: inline;
}
img{
vertical-align: top;
}
翻译要求
-
-
-
-
-
-
-
-
-
浮动顺序
<ul>
<li><img src="http://placehold.it/100x100&text=1"/></li>
<li><img src="http://placehold.it/100x100&text=2"/></li>
<li><img src="http://placehold.it/100x100&text=3"/></li>
<li><img src="http://placehold.it/100x100&text=4"/></li>
<li><img src="http://placehold.it/100x100&text=5"/></li>
<li><img src="http://placehold.it/100x100&text=6"/></li>
</ul>
清除浮动
ul li:nth-child(2) {
clear: left;
}
p {
clear: both;
}
浮动的怪癖和清除浮动
ul {
background: gray;
}
ul {
height: 300px;
}
为了解决而清除浮动
.clearfix {
clear: both;
}
ul {
overflow: auto;
}
/* For modern browsers */
.cf:before,
.cf:after {
content:"";
display:table;
}
.cf:after {
clear:both;
}
/* For IE 6/7 (trigger hasLayout) */
.cf {
zoom:1;
}