<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>南芝 - 我才12岁（飞鸟尽，良弓藏，2011，背包去旅行！） &#187; margin</title>
	<atom:link href="http://www.12sui.cn/tag/margin/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.12sui.cn</link>
	<description>南芝的前端技术学习经验</description>
	<lastBuildDate>Sun, 08 Jan 2012 09:22:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>IE6、IE7 下的浮动bug</title>
		<link>http://www.12sui.cn/develop/ie6%e3%80%81ie7-%e4%b8%8b%e7%9a%84%e6%b5%ae%e5%8a%a8bug/</link>
		<comments>http://www.12sui.cn/develop/ie6%e3%80%81ie7-%e4%b8%8b%e7%9a%84%e6%b5%ae%e5%8a%a8bug/#comments</comments>
		<pubDate>Sat, 04 Oct 2008 04:13:36 +0000</pubDate>
		<dc:creator>南芝</dc:creator>
				<category><![CDATA[编码]]></category>
		<category><![CDATA[IE6]]></category>
		<category><![CDATA[IE7]]></category>
		<category><![CDATA[margin]]></category>

		<guid isPermaLink="false">http://www.12sui.cn/?p=168</guid>
		<description><![CDATA[开门见山，我们首先看一下代码： &#60;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&#62; &#60;html xmlns="http://www.w3.org/1999/xhtml"&#62; &#60;head&#62; &#60;meta http-equiv="Content-Type" content="text/html; charset=gb2312" /&#62; &#60;title&#62;无标题文档&#60;/title&#62; &#60;style&#62; test { background:#0099FF; overflow:hidden; padding:5px; width:300px;} .testblock { float:left; height:100px; width:100px; background:#000; margin-right:20px; margin-bottom:100px;} &#60;/style&#62; &#60;/head&#62; &#60;body&#62; &#60;div id="test"&#62; &#60;div class="testblock"&#62;&#60;/div&#62; &#60;div class="testblock"&#62;&#60;/div&#62; &#60;/div&#62; &#60;/body&#62; &#60;/html&#62; 大家在心中勾画一下应该是什么结果呢？看一下在 IE6、IE7 下的显示把： .testblock 的左 margin 起效，而下 margin 未起效，再来看看该段代码在 IE8、Firefox、Opera、Safari [...]]]></description>
			<content:encoded><![CDATA[<p>开门见山，我们首先看一下代码：</p>

<p><pre>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=gb2312" /&gt;
&lt;title&gt;无标题文档&lt;/title&gt;
&lt;style&gt;</p>

<h1>test { background:#0099FF; overflow:hidden; padding:5px; width:300px;}</h1>

<p>.testblock { float:left; height:100px; width:100px; background:#000; margin-right:20px; margin-bottom:100px;}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="test"&gt;
    &lt;div class="testblock"&gt;&lt;/div&gt;
    &lt;div class="testblock"&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></p>

<p>大家在心中勾画一下应该是什么结果呢？看一下在 IE6、IE7 下的显示把：</p>

<p><img src="http://www.12sui.cn/wp-content/uploads/2008/10/1041.gif" alt="" /></p>

<p>.testblock 的左 margin 起效，而下 margin 未起效，再来看看该段代码在 IE8、Firefox、Opera、Safari 以及 Google Chrome 下的显示：</p>

<p><img src="http://www.12sui.cn/wp-content/uploads/2008/10/1042.gif" alt="" /></p>

<p>两个 margin 都起效了，您可以通过各种浏览器打开<a href="http://www.12sui.cn/test/13/1.html">这个页面</a>查看效果。IE6、IE7 的下 margin 真的没有起效吗？我们再为 #test 添加两个内 div，看看会是什么效果：</p>

<p><span id="more-168"></span>
<pre>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=gb2312" /&gt;
&lt;title&gt;无标题文档&lt;/title&gt;
&lt;style&gt;</p>

<h1>test { background:#0099FF; overflow:hidden; padding:5px; width:300px;}</h1>

<p>.testblock { float:left; height:100px; width:100px; background:#000; margin-right:20px; margin-bottom:100px;}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="test"&gt;
    &lt;div class="testblock"&gt;&lt;/div&gt;
    &lt;div class="testblock"&gt;&lt;/div&gt;
    &lt;div class="testblock"&gt;&lt;/div&gt;
    &lt;div class="testblock"&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></p>

<p>先来看看 IE6、IE7下的显示把：</p>

<p><img src="http://www.12sui.cn/wp-content/uploads/2008/10/1043.gif" alt="" /></p>

<p>上边两个 .testblock 的下 margin 起了作用，然后最下边的依然无效，再来看看 IE8、Firefox、Opera、Safari 以及 Google Chrome 下的显示：</p>

<p><img src="http://www.12sui.cn/wp-content/uploads/2008/10/1044.gif" alt="" /></p>

<p>显示依然正常，所有的 margin 均起了作用。您可以打开各种浏览器测试<a href="http://www.12sui.cn/test/13/2.html">这个页面</a>，观看效果。</p>

<p>那么现在我们在浮动元素下添加一个固定的元素，看看会是什么效果呢？</p>

<p><pre>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=gb2312" /&gt;
&lt;title&gt;无标题文档&lt;/title&gt;
&lt;style&gt;</p>

<h1>test { background:#0099FF; overflow:hidden; padding:5px; width:300px;}</h1>

<p>.testblock { float:left; height:100px; width:100px; background:#000; margin-right:20px; margin-bottom:100px;}</p>

<h1>test_2 { clear:both; height:20px; background:#00CC33; width:100%; margin-bottom:100px;}</h1>

<p>&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="test"&gt;
    &lt;div class="testblock"&gt;&lt;/div&gt;
    &lt;div class="testblock"&gt;&lt;/div&gt;
    &lt;div class="testblock"&gt;&lt;/div&gt;
    &lt;div class="testblock"&gt;&lt;/div&gt;
    &lt;div id="test_2"&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></p>

<p>大家可以看到，在所有浏览器下，这一段的显示是一样的，所有 .testblock 的 margin 都表现了出来。您可以在<a href="http://www.12sui.cn/test/13/3.html">这个页面</a>进行测试查看。</p>

<p><img src="http://www.12sui.cn/wp-content/uploads/2008/10/1045.gif" alt="" /></p>

<p>现在再对代码就行修改，我们将固定元素放到 #test 外部，不与 .testblock 同级，看看会如何：</p>

<p><pre>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=gb2312" /&gt;
&lt;title&gt;无标题文档&lt;/title&gt;
&lt;style&gt;</p>

<h1>test { background:#0099FF; overflow:hidden; padding:5px; width:300px;}</h1>

<p>.testblock { float:left; height:100px; width:100px; background:#000; margin-right:20px; margin-bottom:100px;}</p>

<h1>test_2 { clear:both; height:20px; background:#00CC33; width:100%;}</h1>

<p>&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="test"&gt;
    &lt;div class="testblock"&gt;&lt;/div&gt;
    &lt;div class="testblock"&gt;&lt;/div&gt;
    &lt;div class="testblock"&gt;&lt;/div&gt;
    &lt;div class="testblock"&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div id="test_2"&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></p>

<p>先来看看 IE6、IE7 下的显示把：</p>

<p><img src="http://www.12sui.cn/wp-content/uploads/2008/10/1046.gif" alt="" /></p>

<p>下边两个 .testblock 的下 margin 失效，再来看看 IE8、Firefox、Opera、Safari 以及 Google Chrome 下的显示：</p>

<p><img src="http://www.12sui.cn/wp-content/uploads/2008/10/1047.gif" alt="" /></p>

<p>.testblock 的所有 margin 都有效，您可以在<a href="http://www.12sui.cn/test/13/4.html">这个页面</a>查看。</p>

<p>至于最后这段代码中的 #test_2 的下 margin 是否起效，相信大家都知道，不知道的可以自己测试一下。</p>

<p>那么我们最后得到了什么结论呢？在 IE6、IE7 下的浮动元素，如果该浮动元素下方有同级的元素，不管是浮动还是固定的，该浮动元素的下 margin 有效，倘若下方无元素，该浮动元素的下 margin 将不起效，即为 0。这就造成了一个问题，当我们拍一大串浮动的元素时，倘若换行了，改如何去消除下 margin 这个 bug 呢？</p>

<p>方法一：放弃使用浮动，改用 display:inline-block，不清楚的可以参照<a href="http://www.12sui.cn/blog/138.html">http://www.12sui.cn/blog/138.html</a>。</p>

<p>方法二：为所有的浮动元素的父级固定一个高度，并设置 overflow:hidden。</p>

<p>方法三：可以在浮动元素末尾添加一个空的 div 以消除影响。</p>

<p>方法四：首先定义在现代浏览器下的正常显示，然后使用 *+html 定义在 IE7 下的显示，然后通过条件注释单独定义 IE6 的 CSS。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.12sui.cn/develop/ie6%e3%80%81ie7-%e4%b8%8b%e7%9a%84%e6%b5%ae%e5%8a%a8bug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

