<code style="font-family: Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; padding: 0px; color: inherit; background-color: transparent;">浮动元素引起的问题: (1)父元素的高度无法被撑开,影响与父元素同级的元素 (2)与浮动元素同级的非浮动元素会跟随其后 (3)若非第一个元素浮动,则该元素之前的元素也需要浮动,否则会影响页面显示的结构 </code>
解决方法:
使用CSS
中的clear:both
;属性来清除元素的浮动可解决2、3问题,对于问题1,添加如下样式,给父元素添加clearfix
样式:
<code style="font-family: Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; padding: 0px; color: inherit; background-color: transparent;"><span class="hljs-class" style="color: rgb(155, 112, 63);">.clearfix</span><span class="hljs-pseudo" style="color: rgb(203, 75, 22);">:after</span><span class="hljs-rules">{<span class="hljs-rule"><span class="hljs-attribute" style="color: rgb(181, 137, 0);">content</span>:<span class="hljs-value" style="color: rgb(42, 161, 152);"> <span class="hljs-string" style="color: rgb(0, 136, 0);">"."</span></span></span>;<span class="hljs-rule"><span class="hljs-attribute" style="color: rgb(181, 137, 0);">display</span>:<span class="hljs-value" style="color: rgb(42, 161, 152);"> block</span></span>;<span class="hljs-rule"><span class="hljs-attribute" style="color: rgb(181, 137, 0);">height</span>:<span class="hljs-value" style="color: rgb(42, 161, 152);"> <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span></span></span>;<span class="hljs-rule"><span class="hljs-attribute" style="color: rgb(181, 137, 0);">clear</span>:<span class="hljs-value" style="color: rgb(42, 161, 152);"> both</span></span>;<span class="hljs-rule"><span class="hljs-attribute" style="color: rgb(181, 137, 0);">visibility</span>:<span class="hljs-value" style="color: rgb(42, 161, 152);"> hidden</span></span>;<span class="hljs-rule">}</span></span>
<span class="hljs-class" style="color: rgb(155, 112, 63);">.clearfix</span><span class="hljs-rules">{<span class="hljs-rule"><span class="hljs-attribute" style="color: rgb(181, 137, 0);">display</span>:<span class="hljs-value" style="color: rgb(42, 161, 152);"> inline-block</span></span>;<span class="hljs-rule">}</span></span> <span class="hljs-comment" style="color: rgb(147, 161, 161);">/* for IE/Mac */</span>
</code>
清除浮动的几种方法:
<code style="font-family: Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; padding: 0px; color: inherit; background-color: transparent;"><span class="hljs-number" style="color: rgb(42, 161, 152);">1</span>,额外标签法,<div style=<span class="hljs-string" style="color: rgb(42, 161, 152);">"clear:both;"</span>></div>(缺点:不过这个办法会增加额外的标签使HTML结构看起来不够简洁。) <span class="hljs-number" style="color: rgb(42, 161, 152);">2</span>,使用after伪类 <span class="hljs-comment" style="color: rgb(147, 161, 161);">#parent:after{</span> <span class="hljs-attribute" style="color: rgb(181, 137, 0);">content</span>:<span class="hljs-string" style="color: rgb(42, 161, 152);">"."</span>; <span class="hljs-attribute" style="color: rgb(181, 137, 0);">height</span>:<span class="hljs-number" style="color: rgb(42, 161, 152);">0</span>; <span class="hljs-attribute" style="color: rgb(181, 137, 0);">visibility</span>:hidden; <span class="hljs-attribute" style="color: rgb(181, 137, 0);">display</span>:block; <span class="hljs-attribute" style="color: rgb(181, 137, 0);">clear</span>:both; } <span class="hljs-number" style="color: rgb(42, 161, 152);">3</span>,浮动外部元素 <span class="hljs-number" style="color: rgb(42, 161, 152);">4</span>,设置`<span class="javascript">overflow</span>`为`<span class="javascript">hidden</span>`或者auto</code>