<?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>一起CSS &#187; CSS HACK</title>
	<atom:link href="http://www.17css.com/tag/css-hack/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.17css.com</link>
	<description>一起CSS，专注、关注CSS等前端技术。</description>
	<lastBuildDate>Tue, 27 Jul 2010 00:58:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>说说CSS Hack和向后兼容</title>
		<link>http://www.17css.com/css-hack-and-compatibility/</link>
		<comments>http://www.17css.com/css-hack-and-compatibility/#comments</comments>
		<pubDate>Tue, 18 May 2010 16:46:46 +0000</pubDate>
		<dc:creator>青色</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[CSS HACK]]></category>

		<guid isPermaLink="false">http://www.17css.com/?p=580</guid>
		<description><![CDATA[人一旦习惯了某些东西就很难去改，以及各种各样的原因，新的浏览器越来越多，而老的总淘汰不了。增长总是快于消亡导致了浏览器兼容是成了谈不完的话题。说 到浏览器兼容，CSS HACK自然... ]]></description>
			<content:encoded><![CDATA[<p>人一旦习惯了某些东西就很难去改，以及各种各样的原因，新的浏览器越来越多，而老的总淘汰不了。增长总是快于消亡导致了浏览器兼容是成了谈不完的话题。说 到浏览器兼容，CSS HACK自然而然地被我们想起。今天，我们通常都有一个团队或者将有一个团队的人在一个公司里面做相同的事，需要我们有统一的规范来进行Coding，以 方便维护。而解决兼容的方法就是（必须是，因为这才最容易有问题的）其中一个最重要的、要解决的规范之一。</p>
<p><img src="http://17css.com/works/10/B/images/css-hack-and-compatibility.png" alt="CSS Hack" /></p>
<p>在解决兼容方法上，想定出一个统一的规范，个人认为应该以下面3点为基本原则：</p>
<ol>
<li>权衡成本：在浏览器被淘汰后，如何快速清理掉无用代码</li>
<li>可维护：在资源成本和完美间平衡的向后兼容</li>
<li>可读：省力、易记</li>
</ol>
<p>这里把成本放在了第一位，并不是说我们不愿意追求完美，而只是，太刻意追求完美有时候可能会阻碍我们前进；在成本后，应该是可维护和可读，这点对于团队的合作来说至关重要，而最终结果也是为了减少成本。</p>
<p>先把这三个原则存起来，来看看我们平时解决兼容的写法（后面会附详细的Hack方法列表）：</p>
<p><span id="more-580"></span></p>
<h3>一、CSS 选择器 Hack</h3>
<pre class="prettyprint">
/* Opera */
@media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0)
{head~body .sofish{display:block;}}
</pre>
<p><strong>这种写法的优缺点是：</strong></p>
<ul>
<li>优点：全面，各种HACK都有；清理无用代码里易认</li>
<li>缺点：选择器名称不易记；代码量多（要重复写选择器）</li>
</ul>
<h3>二、CSS 属性 Hack</h3>
<pre class="prettyprint">
.sofish {
	padding:10px;
	padding:9px\9; /* all ie */
	padding:8px\0; /* ie8-9 */
	*padding:5px; /* ie6-7 */
	+padding:7px; /* ie7 */
	_padding:6px; /* ie6 */
}
</pre>
<p><strong>这种写法的优缺点是：</strong></p>
<ul>
<li>优点：易记；代码少</li>
<li>缺点：不全面</li>
</ul>
<h3>三、IE 注释</h3>
<pre class="prettyprint">
&lt;!--[if IE]&gt;IE only&lt;![endif]--&gt;
&lt;!--[if !IE]&gt;NOT IE&lt;![endif]--&gt;
</pre>
<p><strong>这种写法的优缺点是：</strong></p>
<ul>
<li>优点：安全；向后兼容好；易维护</li>
<li>缺点：用不好会增加HTTP请求；用得好代码又多</li>
</ul>
<h3>四、浏览器探测：JS/后端程序判断</h3>
<pre class="prettyprint">
// 以jQuery为例，检测是否是IE6，是则加上class=&quot;ie6&quot;
if ($.browser.msie &amp;&amp; $.browser.version = 6 ){
	$('div').addClass('ie6');
}
</pre>
<p><strong>这种写法的优缺点是：</strong></p>
<ul>
<li>优点：全面；易维护；可读性高</li>
<li>缺点：占资源；代码量大（要重写选择器）</li>
</ul>
<p>上面4种是我们最常用的方法。现在，让我们抽出心里存着的那3个原则，看看如何选择。要时间思考一下么？这里简单地说一下我的选择：</p>
<ol>
<li>尽量使用单独HACK
<p>这样维护起来成本比较低，改动不会影响其他的浏览器，而一旦有浏览器淘汰，只要搜索关键字，就可以批量去掉这些代码。比如，ie6的单独hack：<code>_padding:6px;；</code></p>
</li>
<li>向后兼容的目标：1年
<p>你想现在的网站兼容IE10么，谁不想，但这可预见性太低了，也可以说，成本太高了。暂时没必要。不过，IE9可能要发布了，所以，选择像<code>padding:8px\0;</code>这样的IE8+的hack，在删掉其他代码不影响向后兼容上，会更好；并且，如果IE10出来，一旦支持这个hack，而又没有这个bug，可能删掉只影响2个浏览器，也会更方便；</p>
</li>
<li>尽可能省资源
<p>你要是不考虑页面加载速度，不考虑服务器承受能力的话，那在向后兼容和淘汰的处理上可以做得很完美（从代码上），但这从某种程度上，不如不做。</p>
</li>
</ol>
<h3>五、个人推荐写法</h3>
<p>其实可以纠结的还真多，这里结合A-Grade浏览器的种类和HACK的种类，写两种个人认为比较合理的HACK和向后兼容相兼顾的写法，仅供大家参考的。</p>
<p><strong>经济实惠型写法：</strong>注重单独的HACK。 IE的HACK比较多，选择省力易记的属性HACK；其他浏览器HACK少，选择块状的选择器HACK(推荐)</p>
<pre class="prettyprint">
.sofish {
	padding:10px;
	padding:9px\9; /* all ie */
	padding:8px\0; /* ie8-9 目前应用于IE8的单独hack，情况比较少 */
	*padding:5px; /* ie6-7 */
	+padding:7px; /* ie7 */
	_padding:6px; /* ie6 */
}

/* webkit and opera */
@media all and (min-width: 0px){ .sofish{padding:11px;} }

/* webkit */
@media screen and (-webkit-min-device-pixel-ratio:0){ .sofish{padding:11px;} }

/* opera */
@media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0) { .sofish{padding:11px;} }

/* firefox * /
@-moz-document url-prefix(){ .sofish{padding:11px;}} /* all firefox */
html&gt;/**/body .sofish, x:-moz-any-link, x:default { padding:11px; } /* newest firefox */
</pre>
<p><strong>准完美主义写法：</strong>配合IE注释，一律采用选择器HACK（选择性推荐）</p>
<pre class="prettyprint">
HTML: 添加body class
&lt;!--[if IE6]--&gt;&lt;body class=&quot;ie6&quot;&gt;&lt;![endif]--&gt;
&lt;!--[if IE7]--&gt;&lt;body class=&quot;ie7&quot;&gt;&lt;![endif]--&gt;
&lt;!--[if IE8]--&gt;&lt;body class=&quot;ie8&quot;&gt;&lt;![endif]--&gt;
&lt;!--[if IE9]--&gt;&lt;body class=&quot;ie9&quot;&gt;&lt;![endif]--&gt;
&lt;!--[if !IE]--&gt;&lt;body class=&quot;non-ie&quot;&gt;&lt;![endif]--&gt;
</pre>
<pre class="prettyprint">
.sofish { padding:10px;}
.non-ie .sofish { padding:12px;}
.ie9 .sofish { padding:9px;}
.ie8 .sofish { padding:8px;}
.ie7 .sofish { padding:7px;}
.ie6 .sofish { padding:6px;}

/* webkit and opera */
@media all and (min-width: 0px){ .sofish{padding:11px;} }

/* webkit */
@media screen and (-webkit-min-device-pixel-ratio:0){ .sofish{padding:11px;} }

/* opera */
@media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0) { .sofish{padding:11px;} }

/* firefox */
@-moz-document url-prefix(){ .sofish{padding:11px;}} /* all firefox */
html&gt;/**/body .sofish, x:-moz-any-link, x:default { padding:11px; } /* newest firefox */
</pre>
<p>然后，从第二种方式我们也可以发现。把IE注释用在body class上，而不是添加单独的<code>&lt;link    /&gt;</code>或者<code>@import</code>会是更好的选择。虽然分文件也是一种不错的选择，但了为页面加载速度，HTTP请求一个都不能浪费。</p>
<p>至于利用JS或者后端程序来判断，除非你有足够的资源，除非你解决不了（90%不会发生），不然，并不推荐用。附上一个表（<a href="http://paulirish.com/2009/browser-specific-css-hacks/" rel="nofollow">via</a>），可以参考参考：</p>
<h3>六：全面的IE6+ / Firefox / Webkit / Opera CSS HACK列表：</h3>
<pre class="prettyprint">
/***** Selector Hacks ******/
/* IE6 and below */
* html #uno  { color: red }

/* IE7 */
*:first-child+html #dos { color: red }

/* IE7, FF, Saf, Opera  */
html&gt;body #tres { color: red }

/* IE8, FF, Saf, Opera (Everything but IE 6,7) */
html&gt;/**/body #cuatro { color: red }

/* Opera 9.27 and below, safari 2 */
html:first-child #cinco { color: red }

/* Safari 2-3 */
html[xmlns*=&quot;&quot;] body:last-child #seis { color: red }

/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
body:nth-of-type(1) #siete { color: red }

/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
body:first-of-type #ocho { color: red }

/* saf3+, chrome1+ */
@media screen and (-webkit-min-device-pixel-ratio:0) {
	#diez  { color: red }
}

/* iPhone / mobile webkit */
@media screen and (max-device-width: 480px) {
	#veintiseis { color: red }
}

/* Safari 2 - 3.1 */
html[xmlns*=&quot;&quot;]:root #trece  { color: red }

/* Safari 2 - 3.1, Opera 9.25 */
*|html[xmlns*=&quot;&quot;] #catorce { color: red } 

/* Everything but IE6-8 */
:root *&gt; #quince { color: red }

/* IE7 */
*+html #dieciocho { color: red }

/* Firefox only. 1+ */
#veinticuatro,  x:-moz-any-link  { color: red }

/* Firefox 3.0+ */
#veinticinco,  x:-moz-any-link, x:default  { color: red }

/***** Attribute Hacks ******/
/* IE6 */
#once { _color: blue }

/* IE6, IE7 */
#doce { *color: blue; /* or #color: blue */ }

/* Everything but IE6 */
#diecisiete { color/**/: blue }

/* IE6, IE7, IE8 */
#diecinueve { color: blue\9; }

/* IE7, IE8 */
#veinte { color/*\**/: blue\9; }

/* IE6, IE7 -- acts as an !important */
#veintesiete { color: blue !ie; } /* string after ! can be anything */
</pre>
<p>其他的就不多说了。不过，还是要提醒一下: 注释也是很重要的。虽然是HACK了，但现实中情况有时候比想象中的复杂得多，给代码一个注释，好过千言万语。</p>
<p>最后，还是那句，希望看到你更好的方法！</p>
<p>文章转自：<a href="http://www.happinesz.cn/" title="链接到 幸福收藏夹">幸福收藏夹</a></p>
<p>原文地址：<a href="http://www.happinesz.cn/archives/1331/" title="说说CSS Hack和向后兼容 的原文地址">http://www.happinesz.cn/archives/1331/</a></p>
<p>&copy;2010 <a href="http://www.17css.com">一起CSS</a>. All Rights Reserved.</p>.<h2  class="related_post_title">相关日志</h2><ul class="related_post"><li><a href="http://www.17css.com/css-hack/" title="css hack">css hack</a> (19)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.17css.com/css-hack-and-compatibility/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>css hack</title>
		<link>http://www.17css.com/css-hack/</link>
		<comments>http://www.17css.com/css-hack/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 06:43:25 +0000</pubDate>
		<dc:creator>青色</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[CSS HACK]]></category>

		<guid isPermaLink="false">http://www.17css.com/?p=509</guid>
		<description><![CDATA[css hack 是个很有争议的东西，一开始我也很讨厌，因为我觉得可以饶过 css hack，通过另外的方法解决问题。但是，随着工作中的不断实践，改变了我的观点，css hack 虽然不能通过 w3c 标准认证，... ]]></description>
			<content:encoded><![CDATA[<p>css hack 是个很有争议的东西，一开始我也很讨厌，因为我觉得可以饶过 css hack，通过另外的方法解决问题。但是，随着工作中的不断实践，改变了我的观点，css hack 虽然不能通过 w3c 标准认证，但适当是使用很有可能会使你的 HTML 结构更紧凑、有效的减少无语义标签或带来其他好处。</p>
<p>在公司，电脑里的浏览器是 ie6，除了开发部和技术部外，其他部门的同事很少装其他浏览器（不过有小部分可能会升级到 ie7/ie8）。而开发部和技术虽然会装 ff，但装好之后一般不会升级，因为他们不一定是前端开发人员，他们装 ff 的目的也只是看看自己做的东西在 ff 下是否正常，所以这就导致了公司里有各个版本的 ie 和 ff 都有。其实这不是最严重的，因为我做的东西除了在 op 下可能会有些差异外，在上面所有浏览器和 sa 、ch 下都表现的很好。最严重的是之前的有些东西他们做的时候可能只考虑到 ie6，所以如果要修复的话，会把我给郁闷坏，因为我不仅要针对浏览器修复，还有可能要针对浏览器版本修复。如果是你的话，你会头疼吗？所以我收集了一些我认为是比较简单方便的 css hack，一来是自己在工作可能需要，二来算是分享吧。这些 css hack 注意顺序一起使用，能区分不同的浏览器和版本。当然，如果你没必要考虑这么复杂的情况，就挑选需要的用咯。通过实例来体现吧。</p>
<p>html 代码</p>
<pre class="prettyprint">
&lt;body&gt;
	&lt;p&gt;您的浏览器是&lt;/p&gt;
&lt;/body&gt;</pre>
<p><span id="more-509"></span></p>
<p>css hack 代码</p>
<pre class="prettyprint">
p { margin:0; padding:0 55px 0 0; height:30xp; line-height:30px; font-size:14px;}
p { background:url(llq.gif) 90px -170px no-repeat;} /* all */
p,x:-moz-any-link { background:url(llq.gif) 90px -80px no-repeat;} /* for ff */
p,x:-moz-any-link,x:default { background:url(llq.gif) 90px -140px no-repeat;} /* for ff2+ */
p {[;background:url(llq.gif) 90px -260px no-repeat;]}  /* for sa/ch */
p { background:url(llq.gif) 90px -50px no-repeat\9;}  /* for ie */
*+html p { background:url(llq.gif) 90px -20px no-repeat;} /* only for ie7 */
p { _background:url(llq.gif) 90px 10px no-repeat;} /* only for ie6 */</pre>
<p><a title="css hack" href="http://17css.com/works/09/03/css%20hack.html">查看Demo</a></p>
<p>因为没有找到 op10 的 css hack，所以标准的写法是给 op10 的，然后针对其他浏览器写 css hack。</p>
<p>另外，在修复过程中，我发现了网上流传的一个 css hack 有问题，这个 css hack 也许有很多人在用，就是[属性：值\0]，有的人说这是 ie8 专用的，但我在测试过程中发现这个 css hack 除了 ie8 识别外，ff3 和 op10 也能识别（ff2 和 ff3.5 不能识别）。你可以使用对应的浏览器（如果你有的话）点击<a title="ie8 ff3 op10 css hack" href="http://17css.com/works/09/03/css%20hack2.html">这个例子</a>查看。</p>
<p>所以有些 css hack 还是尽量在多个浏览器里测试测试，以免误导别人。</p>
<p>注：op 代表 opera，sa 代码 safari，ch 代表 chrome。</p>
<p>&copy;2010 <a href="http://www.17css.com">一起CSS</a>. All Rights Reserved.</p>.<h2  class="related_post_title">相关日志</h2><ul class="related_post"><li><a href="http://www.17css.com/css-hack-and-compatibility/" title="说说CSS Hack和向后兼容">说说CSS Hack和向后兼容</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.17css.com/css-hack/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>
