<?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>浮山狼de博客 &#187; 网页设计</title>
	<atom:link href="https://www.fushanlang.com/category/programming-skill/web-design/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.fushanlang.com</link>
	<description>next station - 下一站，活在当下，且行且思</description>
	<lastBuildDate>Sat, 29 Nov 2014 15:14:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.2.5</generator>
	<item>
		<title>css超链接下划线</title>
		<link>https://www.fushanlang.com/css-underline-hyperlinks-1026/</link>
		<comments>https://www.fushanlang.com/css-underline-hyperlinks-1026/#comments</comments>
		<pubDate>Sun, 26 Sep 2010 03:15:53 +0000</pubDate>
		<dc:creator><![CDATA[fushanlang]]></dc:creator>
				<category><![CDATA[css]]></category>
		<category><![CDATA[网页设计]]></category>
		<category><![CDATA[超链接]]></category>

		<guid isPermaLink="false">http://www.fushanlang.com/blog/?p=1026</guid>
		<description><![CDATA[css超链接下划线往下隔开一段距离的两种实现方法 css underline style  without text-decoration <p>经常使用的超链接的CSS样式，带下划线通常采用&#8221;text-decoration:underline&#8221; 普通情况下，很好用，但是当字体加粗或者对h1~h3比较大的字体加下划线时，会发现下划线与超链接连在一起，很不雅观。于是便有了将超链接下划线与文本链接往下隔开一段距离的要求。这里采用的是用首先设置text-decoration:none，然后设置border-bottom,只需设置为solid 即可，最后padding-bottom往下隔开一段像素距离如3px，可以完美实现将下划线往下隔开一段距离的要求。</p> <p>a.cls-underline-1{</p> <p></p> <p>text-decoration:none;</p> <p>border-bottom:solid /*设置为solid就可以了*/</p> <p>padding-bottom:3px; /*设置下方隔开距离此处为3像素*/ }</p> a.cls-underline-1{ text-decoration:none; border-bottom:solid; /*设置为solid就可以了*/ padding-bottom:3px; /*设置下方隔开距离此处为3像素*/ margin:5px; } a.cls-underline-2{ text-decoration:none; border-bottom:dotted #C90; /*设置为solid就可以了*/ padding-bottom:3px; /*设置下方隔开距离此处为3像素*/ margin:5px; } a.cls-underline-3{ text-decoration:none; border-bottom:double black; /*设置为solid就可以了*/ padding-bottom:3px; /*设置下方隔开距离此处为3像素*/ margin:5px; } a.cls-underline-4{ text-decoration:none; border-bottom:dashed; /*设置为solid就可以了*/ padding-bottom:3px; /*设置下方隔开距离此处为3像素*/ margin:5px; } a.cls-bgunderline-1{ text-decoration:none; padding-bottom:3px; <span style="color:#777"> . . . &#8594; Read More: <a href="https://www.fushanlang.com/css-underline-hyperlinks-1026/">css超链接下划线</a></span>]]></description>
				<content:encoded><![CDATA[<h3>css超链接下划线往下隔开一段距离的两种实现方法</h3>
<h2>css underline style  without text-decoration</h2>
<p>经常使用的超链接的CSS样式，带下划线通常采用&#8221;text-decoration:underline&#8221; 普通情况下，很好用，但是当字体加粗或者对h1~h3比较大的字体加下划线时，会发现下划线与超链接连在一起，很不雅观。于是便有了将超链接下划线与文本链接往下隔开一段距离的要求。这里采用的是用首先设置text-decoration:none，然后设置border-bottom,只需设置为solid 即可，最后padding-bottom往下隔开一段像素距离如3px，可以完美实现将下划线往下隔开一段距离的要求。</p>
<p>a.cls-underline-1{</p>
<p><span id="more-1026"></span></p>
<p>text-decoration:none;</p>
<p>border-bottom:solid /*设置为solid就可以了*/</p>
<p>padding-bottom:3px; /*设置下方隔开距离此处为3像素*/<br />
}</p>
<style type="text/css"> a.cls-underline-1{ text-decoration:none; border-bottom:solid; /*设置为solid就可以了*/ padding-bottom:3px; /*设置下方隔开距离此处为3像素*/ margin:5px; } a.cls-underline-2{ text-decoration:none; border-bottom:dotted  #C90; /*设置为solid就可以了*/ padding-bottom:3px; /*设置下方隔开距离此处为3像素*/ margin:5px; } a.cls-underline-3{ text-decoration:none; border-bottom:double black; /*设置为solid就可以了*/ padding-bottom:3px; /*设置下方隔开距离此处为3像素*/ margin:5px; } a.cls-underline-4{ text-decoration:none; border-bottom:dashed; /*设置为solid就可以了*/ padding-bottom:3px; /*设置下方隔开距离此处为3像素*/ margin:5px; } a.cls-bgunderline-1{ text-decoration:none; padding-bottom:3px; /*设置下方隔开距离此处为3像素*/ background:url('http://www.fushanlang.com/blog/wp-content/uploads/2010/09/underline.gif')  repeat-x 100% 100%; margin:5px; } </style>
<p>Examples:<br />
<a class="cls-underline-1" href="#">css underline style </a><br />
<a class="cls-underline-2" href="#">css underline style </a><br />
<a class="cls-underline-3" href="#">css underline style </a><br />
<a class="cls-underline-4" href="#">css underline style </a></p>
<p>当然，还有一种方式更为简单一些，就是直接设置一个图片作为超链接的背景，背景图片里定制好下划线的位置。也可以采用background-position来辅助调整一下。采用此种方式，可以通过替换背景图片为动态的gif图片制作更炫的效果，而且图片的下划线可以制作的更丰富一些。</p>
<p>a.cls-underline-2{</p>
<p>text-decoration:none;</p>
<p>background:url(alink-background.jpg);</p>
<p>background-position: left+px top+px;/*当背景图片设计的不够好时，可以采用此设置来调整位置*/</p>
<p>pading-bottom:3px;/*设置下方隔开距离此处为3像素*/</p>
<p>}<br />
Examples:<br />
<a class="cls-bgunderline-1" href="#">css underline style </a></p>
]]></content:encoded>
			<wfw:commentRss>https://www.fushanlang.com/css-underline-hyperlinks-1026/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>CSS的优先级特性Specificity</title>
		<link>https://www.fushanlang.com/the-priority-of-features-css-specificity-582/</link>
		<comments>https://www.fushanlang.com/the-priority-of-features-css-specificity-582/#comments</comments>
		<pubDate>Wed, 15 Sep 2010 01:10:10 +0000</pubDate>
		<dc:creator><![CDATA[fushanlang]]></dc:creator>
				<category><![CDATA[css]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[网页设计]]></category>

		<guid isPermaLink="false">http://www.fushanlang.com/blog/?p=582</guid>
		<description><![CDATA[<p>如果同个元素有两个或以上冲突的CSS规则，浏览器有一些基本的规则来决定哪一个非常特殊而胜出。 它可能不像其它那么重要，大部分案例你不需要担心冲突，但大型而且复杂的CSS文件，或有很多CSS文件组成的，可能产生冲突。 选择器一样的情况下后面的会覆盖前面的属性。比如：</p> p { color: red; } p { color: blue; } p元素的元素将是蓝色，因为遵循后面的规则。 然而，你不可能经常用相同的选择器冲突达到目的，当你使用嵌套选择器，合理的冲突来了。比如： div p { color: red; } p { color: blue; } 也许你看起来p元素在div元素里面的颜色是蓝色，就像后面p元素的规则，但是第一个选择器的特性却是红色。基本上，一个选择器越多特性，样式冲突的时候将显示它的样式。 一组嵌套选择器的实际特性可以计算出来。基本的，使用ID选择器的值是100，使用class选择器的值是10，每个html选择器的值是1。它们加起来就可以计算出特性的值。 <p> p的特性是1（一个html选择器） div p的特性是2（两个html选择器） .tree的特性是10（1个class选择器） div p.tree的特性是1+1+10=12，（两个html选择器，一个class选择器） #baobab的特性是100（1个ID选择器） body #content .alternative p的特性是112（两个html选择器，一个ID选择器，一个类选择器） 按照上面的规则，div p.tree的特性比div p高，body #content .alternative p又比它们两个都高。</p> <p>css优先级的四大原则：</p> <p>原则一： 继承不如指定</p> <p>如果某样式是继承来的永远不如具体指定的优先级高。</p> <p>例子1：</p> <p>CODE:</p> <p>&#60;style type=&#8221;text/css&#8221;&#62;</p> <span style="color:#777"> . . . &#8594; Read More: <a href="https://www.fushanlang.com/the-priority-of-features-css-specificity-582/">CSS的优先级特性Specificity</a></span>]]></description>
				<content:encoded><![CDATA[<p><span style="font-size: small;">如果同个元素有两个或以上冲突的CSS规则，浏览器有一些基本的规则来决定哪一个非常特殊而胜出。<br />
它可能不像其它那么重要，大部分案例你不需要担心冲突，但大型而且复杂的CSS文件，或有很多CSS文件组成的，可能产生冲突。<br />
选择器一样的情况下后面的会覆盖前面的属性。比如：</span></p>
<div><strong><span style="font-size: small;">p { color: red; }<br />
p { color: blue; }</span></strong></div>
<div><span style="font-size: small;">p元素的元素将是蓝色，<strong><span style="color: #ff0000;">因为遵循后面的规则</span></strong>。<br />
然而，你不可能经常用相同的选择器冲突达到目的，当你使用嵌套选择器，合理的冲突来了。比如：<span id="more-582"></span><br />
</span></div>
<div><span style="font-size: small;"><br />
</span></div>
<div><strong><span style="font-size: small;">div p { color: red; }<br />
p { color: blue; }</span></strong></div>
<div><span style="font-size: small;">也许你看起来p元素在div元素里面的颜色是蓝色，就像后面p元素的规则，但是第一个选择器的特性却是红色。基本上，一个选择器越多特性，样式冲突的时候将显示它的样式。<br />
一组嵌套选择器的实际特性可以计算出来。基本的，使用ID选择器的值是100，使用class选择器的值是10，每个html选择器的值是1。它们加起来就可以计算出特性的值。<br />
</span></div>
<div>
<p><span style="font-size: small;"> p的特性是1（一个html选择器）<br />
div p的特性是2（两个html选择器）<br />
.tree的特性是10（1个class选择器）<br />
div p.tree的特性是1+1+10=12，（两个html选择器，一个class选择器）<br />
#baobab的特性是100（1个ID选择器）<br />
body #content .alternative p的特性是112（两个html选择器，一个ID选择器，一个类选择器）<br />
按照上面的规则，div p.tree的特性比div p高，body #content .alternative p又比它们两个都高。</span></p>
<p><span style="font-size: small;">css优先级的四大原则：</span></p>
<p>原则一： 继承不如指定</p>
<p>如果某样式是继承来的永远不如具体指定的优先级高。</p>
<p>例子1：</p>
<p>CODE:</p>
<p>&lt;style type=&#8221;text/css&#8221;&gt;</p>
<p>&lt;!&#8211;</p>
<p>*{font-size:20px}</p>
<p>.class3{ font-size: 12px; }</p>
<p>&#8211;&gt;</p>
<p>&lt;/style&gt;</p>
<p>&lt;span&gt;我是多大字号？&lt;/span&gt;</p>
<p>运行结果：.class3{ font-size: 12px; }</p>
<p>例子2：</p>
<p>CODE:</p>
<p>&lt;style type=&#8221;text/css&#8221;&gt;</p>
<p>&lt;!&#8211;</p>
<p>#id1 #id2{font-size:20px}</p>
<p>.class3{font-size:12px}</p>
<p>&#8211;&gt;</p>
<p>&lt;/style&gt;</p>
<p>&lt;div id=&#8221;id1&#8243;&gt;</p>
<p>&lt;p id=&#8221;id2&#8243;&gt; &lt;span id=&#8221;id3&#8243;&gt;我是多大字号？&lt;/span&gt; &lt;/p&gt;</p>
<p>&lt;/div&gt;</p>
<p>运行结果：.class3{ font-size: 12px; }</p>
<p>注意：后面的几大原则都是建立在“指定”的基础上的。</p>
<p>原则二： #ID &gt;.class &gt;标签选择符</p>
<p>例子：</p>
<p>CODE:</p>
<p>&lt;style type=&#8221;text/css&#8221;&gt;</p>
<p>&lt;!&#8211;</p>
<p>#id3 { font-size: 25px; }</p>
<p>.class3{ font-size: 18px; }</p>
<p>span{font-size:12px}</p>
<p>&#8211;&gt;</p>
<p>&lt;/style&gt;</p>
<p>&lt;span id=&#8221;id3&#8243;&gt;我是多大字号？&lt;/span&gt;</p>
<p>运行结果：#id3 { font-size: 25px; }</p>
<p>原则三：越具体越强大。</p>
<p>解释：当对某个元素的CSS选择符样式定义的越具体，层级越明确，该定义的优先级就越高。</p>
<p>CODE:</p>
<p>&lt;style type=&#8221;text/css&#8221;&gt;</p>
<p>&lt;!&#8211;</p>
<p>.class1 .class2 .class3{font-size: 25px;}</p>
<p>.class2 .class3{font-size:18px}</p>
<p>.class3 { font-size: 12px; }</p>
<p>&#8211;&gt;</p>
<p>&lt;/style&gt;</p>
<p>&lt;div&gt;</p>
<p>&lt;p&gt; &lt;span&gt;我是多大字号？&lt;/span&gt; &lt;/p&gt;</p>
<p>&lt;/div&gt;</p>
<p>运行结果：.class1 .class2 .class3{font-size: 25px;}</p>
<p>原则四：标签#id &gt;#id ; 标签.class &gt;.class</p>
<p>上面这条原则大家应该也都知道，看例子</p>
<p>CODE:</p>
<p>&lt;style type=&#8221;text/css&#8221;&gt;</p>
<p>&lt;!&#8211;</p>
<p>span#id3{font-size:18px}</p>
<p>#id3{font-size:12px}</p>
<p>span.class3{font-size:18px}</p>
<p>.class3{font-size:12px}</p>
<p>&#8211;&gt;</p>
<p>&lt;/style&gt;</p>
<p>&lt;span id=&#8221;id3&#8243;&gt;我是多大字号？&lt;/span&gt;</p>
<p>&lt;span&gt;我是多大字号？&lt;/span&gt;</p>
<p>运行结果：span#id3{font-size:18px} | span.class3{font-size:18px}</p>
<p>很多人会有这样的疑问，为什么不把这个原则四归入原则一形成：</p>
<p>【 标签#ID &gt;#ID &gt;标签.class &gt;.class &gt;标签选择符 &gt;通配符 】 呢？或者将 “标签.class” 看作多更为具体的 “.class” 从而归入原则二呢？后面我将解答各位的疑惑，这就涉及到CSS的解析规律&#8212;&#8212;&#8212;这四大原则间也是有优先级的，是不是有些糊涂了？别急，继续看。</p>
<p>*四大原则的权重</p>
<p>相信很多人都知道上面的四大原则，不要以为知道了这四大原则就能分辨css中那条代码是起作用的，不信？那你5秒内能肯定的知道下面这段代码，测试中的文字的字号吗？</p>
<p>CODE:</p>
<p>&lt;style type=&#8221;text/css&#8221;&gt;</p>
<p>&lt;!&#8211;</p>
<p>.class1 p#id2 .class3{font-size:25px}</p>
<p>div .class2 span#id3{font-size:18px}</p>
<p>#id1 .class3{font-size:14px}</p>
<p>.class1 #id2 .class3{font-size:12px}</p>
<p>#id1 #id2{font-size:10px}</p>
<p>&#8211;&gt;</p>
<p>&lt;/style&gt;</p>
<p>&lt;div id=&#8221;id1&#8243;&gt;</p>
<p>&lt;p id=&#8221;id2&#8243;&gt; &lt;span id=&#8221;id3&#8243;&gt;我是多大字号？&lt;/span&gt; &lt;/p&gt;</p>
<p>&lt;/div&gt;</p>
<p>为了大家方便阅读，我去掉了一些代码。</p>
<p>四大原则的权重就是: 原则一 &gt;原则二 &gt;原则三 &gt;原则四</p>
<p>解释：</p>
<p>首先遵循原则一</p>
<p>有指定开始使用下面的原则，无指定则继承离他最近的定义。</p>
<p>然后开始原则二</p>
<p>1、比较最高优先级的选择符</p>
<p>例子：</p>
<p>CODE:</p>
<p>&lt;style type=&#8221;text/css&#8221;&gt;</p>
<p>&lt;!&#8211;</p>
<p>#id3{font-size:18px}</p>
<p>.class1 .class2 .class3{font-size:12px} /* 描述的再具体也不起作用 &#8212; 原则二 */</p>
<p>.class3{font-size:18px}</p>
<p>div p span{font-size:12px}</p>
<p>&#8211;&gt;</p>
<p>&lt;/style&gt;</p>
<p>&lt;div id=&#8221;id1&#8243;&gt;</p>
<p>&lt;p id=&#8221;id2&#8243;&gt; &lt;span id=&#8221;id3&#8243;&gt;我是多大字号？&lt;/span&gt; &lt;/p&gt;</p>
<p>&lt;/div&gt;</p>
<p>运行结果：#id3{font-size:18px}</p>
<p>删掉上面CSS中的前两行可以得出，如果没有最高级别的#ID会寻找.class 即使后面的CSS按照“原则二” 描述的再具体也无法突破原则一。</p>
<p>2、如果两条CSS的如果最高选择符优先级一样，则比较他们的数量</p>
<p>例子：</p>
<p>CODE:</p>
<p>&lt;style type=&#8221;text/css&#8221;&gt;</p>
<p>&lt;!&#8211;</p>
<p>.class1 #id3{font-size:12px}</p>
<p>.class1 .class2 #id3{font-size:14px}</p>
<p>&#8211;&gt;</p>
<p>&lt;/style&gt;</p>
<p>&lt;div id=&#8221;id1&#8243;&gt;</p>
<p>&lt;p id=&#8221;id2&#8243;&gt; &lt;span id=&#8221;id3&#8243;&gt;我是多大字号？&lt;/span&gt; &lt;/p&gt;</p>
<p>&lt;/div&gt;</p>
<p>运行结果:.class1 .class2 #id3{font-size:14px}</p>
<p>3、如果最高选择符级别和数量都一样，则按照原则二比较他们下一级，以此类推。</p>
<p>例子1：</p>
<p>CODE:</p>
<p>&lt;style type=&#8221;text/css&#8221;&gt;</p>
<p>&lt;!&#8211;</p>
<p>#id1 .class2 .class3{font-size:14px}</p>
<p>div .class2 #id3{font-size:12px}</p>
<p>&#8211;&gt;</p>
<p>&lt;/style&gt;</p>
<p>&lt;div id=&#8221;id1&#8243;&gt;</p>
<p>&lt;p id=&#8221;id2&#8243;&gt; &lt;span id=&#8221;id3&#8243;&gt;我是多大字号？&lt;/span&gt; &lt;/p&gt;</p>
<p>[color=Orange]</p>
<p>运行结果:#id1 .class2 .class3{font-size:14px}</p>
<p>[/color]</p>
<p>*最高级选择符的位置没有高下之分，论证：[code]CODE:</p>
<p>&lt;style type=&#8221;text/css&#8221;&gt;</p>
<p>&lt;!&#8211;</p>
<p>#id1 .class2 .class3{font-size:18px}</p>
<p>.class1 #id2 .class3{font-size:14px}</p>
<p>.class1 .class2 #id3{font-size:12px}</p>
<p>&#8211;&gt;</p>
<p>&lt;/style&gt;</p>
<p>&lt;div id=&#8221;id1&#8243;&gt;</p>
<p>&lt;p id=&#8221;id2&#8243;&gt; &lt;span id=&#8221;id3&#8243;&gt;我是多大字号？&lt;/span&gt; &lt;/p&gt;</p>
<p>&lt;/div&gt;</p>
<p>上例中更换3条CSS的先后可以得出，哪条位于最后，哪条起作用。说明他们的级别一样，后面的将覆盖前面的。</p>
<p>*将原则四归入原则二的不合理性，论证：</p>
<p>CODE:</p>
<p>&lt;style type=&#8221;text/css&#8221;&gt;</p>
<p>&lt;!&#8211;</p>
<p>.class1 span#id3{font-size:14px}</p>
<p>#id1 .class2 .class3{font-size:12px}</p>
<p>&#8211;&gt;</p>
<p>&lt;/style&gt;</p>
<p>&lt;div id=&#8221;id1&#8243;&gt;</p>
<p>&lt;p id=&#8221;id2&#8243;&gt; &lt;span id=&#8221;id3&#8243;&gt;我是多大字号？&lt;/span&gt; &lt;/p&gt;</p>
<p>&lt;/div&gt;</p>
<p>#id1 .class2 .class3{font-size:12px}</p>
<p>可以看到span#id3并不比#id1高出一个级别。</p>
<p>无结果开始原则三</p>
<p>如果比较结果，选择符从最高级开始都对应，级别上的数量也相同，则开始比较谁更具体。</p>
<p>例子：</p>
<p>CODE:</p>
<p>&lt;style type=&#8221;text/css&#8221;&gt;</p>
<p>&lt;!&#8211;</p>
<p>#id1 .class2 span{font-size:14px}</p>
<p>.class1 #id3{font-size:12px}</p>
<p>&#8211;&gt;</p>
<p>&lt;/style&gt;</p>
<p>&lt;div id=&#8221;id1&#8243;&gt;</p>
<p>&lt;p id=&#8221;id2&#8243;&gt; &lt;span id=&#8221;id3&#8243;&gt;我是多大字号？&lt;/span&gt; &lt;/p&gt;</p>
<p>&lt;/div&gt;</p>
<p>#id1 .class2 span{font-size:14px}</p>
<p>当然也可以理解为在原则二层层比较中“少一个层级的样式”，缺少的那个层级没有“层级较多的样式”多出的那个层级的级别高。（绕口令）</p>
<p>*将原则四归入原则三的不合理性，论证：</p>
<p>CODE:</p>
<p>&lt;style type=&#8221;text/css&#8221;&gt;</p>
<p>&lt;!&#8211;</p>
<p>.class2 .class3{font-size:14px}</p>
<p>span.class3{font-size:12px}</p>
<p>&#8211;&gt;</p>
<p>&lt;/style&gt;</p>
<p>&lt;div id=&#8221;id1&#8243;&gt;</p>
<p>&lt;p id=&#8221;id2&#8243;&gt; &lt;span id=&#8221;id3&#8243;&gt;我是多大字号？&lt;/span&gt; &lt;/p&gt;</p>
<p>&lt;/div&gt;</p>
<p>上例中可以看出，如果将原则四并入原则三，将span.class3看作两层，那么应该和.class2 .class3层级一样多，那么应该显示12px,而事实不是这样。</p>
<p>最终对决原则四</p>
<p>如果还分不出结果，则开始原则四的比较：</p>
<p>例子1：</p>
<p>CODE:</p>
<p>&lt;style type=&#8221;text/css&#8221;&gt;</p>
<p>&lt;!&#8211;</p>
<p>.class1 p.class2 .class3{font-size:14px}</p>
<p>.class1 .class2 .class3{font-size:12px}</p>
<p>&#8211;&gt;</p>
<p>&lt;/style&gt;</p>
<p>&lt;div id=&#8221;id1&#8243;&gt;</p>
<p>&lt;p id=&#8221;id2&#8243;&gt; &lt;span id=&#8221;id3&#8243;&gt;我是多大字号？&lt;/span&gt; &lt;/p&gt;</p>
<p>&lt;/div&gt;</p>
<p>.class1 p.class2 .class3{font-size:14px}</p>
</div>
]]></content:encoded>
			<wfw:commentRss>https://www.fushanlang.com/the-priority-of-features-css-specificity-582/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CSS specificity优先级规则</title>
		<link>https://www.fushanlang.com/css-specificity-rules-priority-563/</link>
		<comments>https://www.fushanlang.com/css-specificity-rules-priority-563/#comments</comments>
		<pubDate>Wed, 15 Sep 2010 00:57:16 +0000</pubDate>
		<dc:creator><![CDATA[fushanlang]]></dc:creator>
				<category><![CDATA[css]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[网页设计]]></category>

		<guid isPermaLink="false">http://www.fushanlang.com/blog/?p=563</guid>
		<description><![CDATA[<p>关于CSS specificity CSS specificity 特性或称非凡性，它是衡量个衡量CSS值优先级个标准，既然作为标准，就具套相关判定规定及计算方式，specificity用个位数字串(CSS2是位)来表示，更像个级别，值从左到右，左面最，级于级，数位之间没进制，级别之间可超越。 在多个选择符应用于同个元素上那么Specificity值高最终获得优先级。 选择符Specificity值列表： 规则： 1. 行内样式优先级Specificity值为1,0,0,0，高于外部定义。 如：&#60;div style=”color: red”&#62;sjweb&#60;/div&#62; 外部定义指经由&#60;link&#62;或&#60;style&#62;标签定义规则； 2.!important声明Specificity值最高； 3.Specificity值样情况下，按CSS代码中出现顺序决定，后者CSS样式居上； 4.由继续而得到样式没specificity计算，它低于切其他规则(比如全局选择符*定义规则)。</p> <p>算法： 当遇到多个选择符同时出现时候 按选择符得到Specificity值逐位相加， {数位之间没进制 比如说： 0,0,0,5 + 0,0,0,5 =0,0,0,10 而是 0,0, 1, 0} 就得到最终计算得specificity， 然后在比较取舍时按照从左到右顺序逐位比较。</p> <p>实例分析： 1.div { font-size:12px;} 分析： 1个元素{ div}，Specificity值为0,0,0,1</p> <p>2.body div p{color: green;} 分析： 3个元素{ body div p }，Specificity值为0,0,0,3</p> <p>3.div .sjweb{ font-size:12px;} 分析： 1个元素{ div <span style="color:#777"> . . . &#8594; Read More: <a href="https://www.fushanlang.com/css-specificity-rules-priority-563/">CSS specificity优先级规则</a></span>]]></description>
				<content:encoded><![CDATA[<p>关于CSS specificity<br />
CSS <img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/0857174Nr.png" border="0" alt="" />specificity 特性或称非凡性，它是衡量<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/085718AME.png" border="0" alt="" />个衡量CSS值优先级<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/0857174Nr.png" border="0" alt="" /><img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/085718AME.png" border="0" alt="" />个标准，既然作为标准，就具<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/085718MXN.png" border="0" alt="" /><img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/085718AME.png" border="0" alt="" />套相关<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/0857174Nr.png" border="0" alt="" />判定规定及计算方式，specificity用<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/085718AME.png" border="0" alt="" />个<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/085718R1u.png" border="0" alt="" />位<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/0857174Nr.png" border="0" alt="" />数字串(CSS2是<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/085719cR2.png" border="0" alt="" />位)来表示，更像<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/085718R1u.png" border="0" alt="" />个级别，值从左到右，左面<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/0857174Nr.png" border="0" alt="" />最<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/085719GGO.png" border="0" alt="" />，<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/085718AME.png" border="0" alt="" />级<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/085719GGO.png" border="0" alt="" />于<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/085718AME.png" border="0" alt="" />级，数位之间没<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/085718MXN.png" border="0" alt="" />进制，级别之间<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/085719Kj9.png" border="0" alt="" />可超越。<br />
<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/0857201H2.gif" border="0" alt="" /><span id="more-563"></span><br />
在多个选择符应用于同<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/085718AME.png" border="0" alt="" />个元素上那么Specificity值高<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/0857174Nr.png" border="0" alt="" />最终获得优先级。<br />
选择符Specificity值列表：<br />
<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/085722phD.jpg" border="0" alt="" /><br />
规则：<br />
1. 行内样式优先级Specificity值为1,0,0,0，高于外部定义。<br />
如：&lt;div style=”color: red”&gt;sjweb&lt;/div&gt;<br />
外部定义指经由&lt;link&gt;或&lt;style&gt;标签定义<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/0857174Nr.png" border="0" alt="" />规则；<br />
2.!important声明<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/0857174Nr.png" border="0" alt="" />Specificity值最高；<br />
3.Specificity值<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/085718AME.png" border="0" alt="" />样<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/0857174Nr.png" border="0" alt="" />情况下，按CSS代码中出现<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/0857174Nr.png" border="0" alt="" />顺序决定，后者CSS样式居上；<br />
4.由继续而得到<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/0857174Nr.png" border="0" alt="" />样式没<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/085718MXN.png" border="0" alt="" />specificity<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/0857174Nr.png" border="0" alt="" />计算，它低于<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/085718AME.png" border="0" alt="" />切其他规则(比如全局选择符*定义<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/0857174Nr.png" border="0" alt="" />规则)。</p>
<p>算法：<br />
当遇到多个选择符同时出现时候<br />
按选择符得到<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/0857174Nr.png" border="0" alt="" />Specificity值逐位相加，<br />
{数位之间没<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/085718MXN.png" border="0" alt="" />进制 比如说： 0,0,0,5 + 0,0,0,5 =0,0,0,10 而<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/085719Kj9.png" border="0" alt="" />是 0,0, 1, 0}<br />
就得到最终计算得<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/0857174Nr.png" border="0" alt="" />specificity，<br />
然后在比较取舍时按照从左到右<img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/0857174Nr.png" border="0" alt="" />顺序逐位比较。</p>
<p>实例分析：<br />
1.div { font-size:12px;}<br />
分析：<br />
1个元素{ div}，Specificity值为0,0,0,1</p>
<p>2.body div p{color: green;}<br />
分析：<br />
3个元素{ body div p }，Specificity值为0,0,0,3</p>
<p>3.div .sjweb{ font-size:12px;}<br />
分析：<br />
1个元素{ div }，Specificity值为0,0,0,1<br />
1个类选择符{.sjweb}，Specificity值为0,0,1, 0<br />
最终：Specificity值为 0,0,1,1</p>
<p>4.Div # sjweb { font-size:12px;}<br />
分析：<br />
1个元素{ div }，Specificity值为0,0,0,1<br />
1个类选择符{.sjweb}，Specificity值为0,1,0, 0<br />
最终：Specificity值为 0,1,0,1</p>
<p>5.html &gt; body div [id=”totals”] ul li &gt; p {color:red;}<br />
分析：<br />
6个元素{ html body div ul li p}  Specificity值为0,0,0,6<br />
1个属性选择符{ [id=”totals”] }      Specificity值为0,0,1,0<br />
2个其他选择符{ &gt;  &gt; }            Specificity值为0,0,0,0<br />
最终：Specificity值为 0,0,1,6</p>
<p>声明:<br />
本文作者：孙佳 转载需连接并注明来自www.sjweb.cn</p>
]]></content:encoded>
			<wfw:commentRss>https://www.fushanlang.com/css-specificity-rules-priority-563/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CSS Specificity: Things You Should Know(转)</title>
		<link>https://www.fushanlang.com/css-specificity-things-you-should-know-change-560/</link>
		<comments>https://www.fushanlang.com/css-specificity-things-you-should-know-change-560/#comments</comments>
		<pubDate>Wed, 15 Sep 2010 00:54:07 +0000</pubDate>
		<dc:creator><![CDATA[fushanlang]]></dc:creator>
				<category><![CDATA[css]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[网页设计]]></category>

		<guid isPermaLink="false">http://www.fushanlang.com/blog/?p=560</guid>
		<description><![CDATA[<p>Apart from Floats, the CSS Specificity is one of the most difficult concepts to grasp in Cascading Stylesheets. The different weight of selectors is usually the reason why your CSS-rules don’t apply to some elements, although you think they should have. In order to minimize the time for bug hunting you need to understand, how browsers <span style="color:#777"> . . . &#8594; Read More: <a href="https://www.fushanlang.com/css-specificity-things-you-should-know-change-560/">CSS Specificity: Things You Should Know(转)</a></span>]]></description>
				<content:encoded><![CDATA[<p>Apart from <a href="http://www.smashingmagazine.com/2007/05/01/css-float-theory-things-you-should-know/">Floats</a>, the <strong>CSS Specificity</strong> is one of the most difficult concepts to grasp in Cascading Stylesheets. The different weight of selectors is usually the reason why your CSS-rules don’t apply to some elements, although you think they should have. In order to minimize the time for bug hunting you need to understand, how browsers interpret your code. And to understand that, you need to have a <strong>firm understanding on how specificity works</strong>. In most cases such problems are caused by the simple fact that somewhere among your CSS-rules you’ve defined a more specific selector.</p>
<p>CSS Specificity isn’t simple. However, there are methods to explain it in a simple and intuitive way. And that’s what this article is all about. You’ll understand the concept if you love <a href="http://en.wikipedia.org/wiki/Star_Wars">Star Wars</a>. <em>Really.</em></p>
<p>Let’s take a look at some <strong>important issues related to CSS Specificity</strong> as well as examples, rules, principles, common solutions and resources.<span id="more-560"></span></p>
<ul>
<li>You can find the most important things you should know about CSS specificity in a brief overview at the beginning of the article.</li>
</ul>
<p>[Offtopic: by the way, did you already get your copy of the <a href="http://creatives.commindo-media.de/www/delivery/ck.php?oaparams=2__bannerid=1368__zoneid=0__cb=b06a9d8418__oadest=http%3A%2F%2Fshop.smashingmagazine.com%2Fsmashingbook-dispatcher.php%3Fd%3Dsmashing-book%26utm_source%3DSmashing%252BMagazine%26utm_medium%3Deditorialbox2%26utm_content%3DBTW-Werbesatz%252Bmit%2520Shop%2520als%2520Ziel%26utm_campaign%3DSmashing%252BMagazine%2520-%2520BTW%2520Editorial%2520Box%2520-%2520Shttp://creatives.commindo-media.de/www/delivery/ck.php?oaparams=2__bannerid=1368__zoneid=0__log=no__cb=b06a9d8418__oadest=http%3A%2F%2Fshop.smashingmagazine.com%2Fsmashingbook-dispatcher.php%3Fd%3Dsmashing-book%26utm_source%3DSmashing%252BMagazine%26utm_medium%3Deditorialbox2%26utm_content%3DBTW-Werbesatz%252Bmit%2520Shop%2520als%2520Ziel%26utm_campaign%3DSmashing%252BMagazine%2520-%2520BTW%2520Editorial%2520Box%2520-%2520Smashing%2520Bookmashing%2520Book">Smashing Book</a>?]</p>
<h3>CSS Specificity: An Overview</h3>
<ol>
<li>Specificity <strong>determines, which CSS rule is applied</strong> by the browsers.</li>
<li>Specificity is usually the reason why your CSS-rules don’t apply to some elements, although you think they should.</li>
<li>Every selector has its place in the specificity hierarchy.</li>
<li>If two selectors apply to the same element, the one with <strong>higher specificity wins</strong>.</li>
<li>There are four distinct categories which define the specificity level of a given selector: inline styles, IDs, classes+attributes and elements.</li>
<li>You can understand specificity if you love <em>Star Wars</em>: <a href="http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html">CSS Specificity Wars</a>.</li>
<li>You can understand specificity if you love poker: <a href="http://iamacamera.org/default.aspx?id=95">CSS Specificity for Poker Players</a></li>
<li>When selectors have an equal specificity value, the latest rule is the one that counts.</li>
<li>When selectors have an unequal specificity value, the more specific rule is the one that counts.</li>
<li>Rules with more specific selectors have a greater specificity.</li>
<li>The last rule defined overrides any previous, conflicting rules.</li>
<li>The embedded style sheet has a greater specificity than other rules.</li>
<li>ID selectors have a higher specificity than attribute selectors.</li>
<li>You should always try to <strong>use IDs to increase the specificity</strong>.</li>
<li>A class selector beats any number of element selectors.</li>
<li>The universal selector and inherited selectors have a specificity of 0, 0, 0, 0.</li>
<li>You can calculate CSS specificity with CSS Specificity Calculator.</li>
</ol>
<h3>What is Specificity?</h3>
<ul>
<li><strong>Specificity determines, which CSS rule is applied by browsers.</strong> “Specificity is a type of weighting that has a bearing on how your cascading style sheet (CSS) rules are displayed.” [<a href="http://www.adobe.com/devnet/dreamweaver/articles/css_specificity.html">Understanding Specificity</a>]</li>
<li>Selector specificity is a process used to <em>determine which rules take precedence</em> in CSS when several rules could be applied to the same element in markup. [<a href="http://juicystudio.com/article/selector-specificity.php">Selector Specificity</a>]</li>
<li><em>Every selector has its specificity.</em> “All rules in your CSS carry a specificity rating regardless of selector type, although the weighting that is given to each selector type varies and will ultimately affect the styling of your web documents.” [<a href="http://www.adobe.com/devnet/dreamweaver/articles/css_specificity.html">Understanding Specificity</a>]</li>
<li><strong>If two selectors apply to the same element, the one with higher specificity wins.</strong></li>
</ul>
<h3>Specificity hierarchy</h3>
<ul>
<li>Every selector has its place in the specificity hierarchy. There are four distinct categories which define the specificity level of a given selector:1. Inline styles (Presence of style in document).<br />
An inline style lives within your XHTML document. It is attached directly to the element to be styled. E.g. <code>&lt;h1 style="color: #fff;"&gt;</code></p>
<p>2. IDs (# of ID selectors)<br />
ID is an identifier for your page elements, such as <code>#div</code>.</p>
<p>3. Classes, attributes and pseudo-classes (# of class selectors).<br />
This group includes <code>.classes</code>, <code>[attributes]</code> and pseudo-classes such as <code>:hover</code>, <code>:focus</code> etc.</p>
<p>4. Elements and pseudo-elements (# of Element (type) selectors).<br />
Including for instance <code>:before</code> and <code>:after</code>.</li>
</ul>
<p>If you don’t know what exactly each of these terms stands for, you can take a look at the brief overview of them; in the last section of this article.</p>
<h3>How to measure specificity?</h3>
<ul>
<li><strong>Memorize how to measure specificity.</strong> “Start at 0, add 1000 for style attribute, add 100 for each ID, add 10 for each attribute, class or pseudo-class, add 1 for each element name or pseudo-element. So in
<pre>body #content .data img:hover</pre>
<p>the specificity value would be 122 (<strong>0,1,2,2</strong> or 0122): 100 for #content, 10 for .data, 10 for :hover, 1 for body and 1 for img.” [<a href="http://forums.invisionpower.com/lofiversion/index.php/t176404.html">CSS Specificity</a>]</li>
<li><strong>Alternative way:</strong> “Count the number of ID attributes in the selector (= a). Count the number of other attributes and pseudo-classes in the selector (= b). Count the number of element names and pseudo-elements in the selector (= c). Concatenating the three numbers <strong>a-b-c gives the specificity</strong>. [<a href="http://juicystudio.com/article/selector-specificity.php">CSS Selector Specificity</a>]</li>
<li><a href="http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html">CSS Specificity Wars – Cheat Sheet</a><br />
To help me understand calculating specificity better I made a chart based on the following specificity (or Sith power) values. Each character (selector) is given its own Sith power (specificity value) depending on how powerful they are in the ways of the Dark Side. A storm trooper is less powerful than Vader who is in turn less powerful than the Emperor.</li>
</ul>
<p><a href="http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html"><img src="http://www.fushanlang.com/blog/wp-content/uploads/auto_save_image/2010/09/085409Q6C.jpg" alt="Specificity-wars in CSS Specificity: Things You Should Know" width="507" height="386" /></a></p>
<h3>Specificity Examples: Test Yourself</h3>
<p>It’s easier to calculate the specificity using the first method. Let’s find out, how it actually is done.</p>
<table cellspacing="0">
<tbody>
<tr>
<td>1</td>
<td>* { }</td>
<td>0</td>
</tr>
<tr>
<td>2</td>
<td>li { }</td>
<td>1 (one element)</td>
</tr>
<tr>
<td>3</td>
<td>li:first-line { }</td>
<td>2 (one element, one pseudo-element)</td>
</tr>
<tr>
<td>4</td>
<td>ul li { }</td>
<td>2 (two elements)</td>
</tr>
<tr>
<td>5</td>
<td>ul ol+li { }</td>
<td>3 (three elements)</td>
</tr>
<tr>
<td>6</td>
<td>h1 + *[rel=up] { }</td>
<td>11 (one attribute, one element)</td>
</tr>
<tr>
<td>7</td>
<td>ul ol li.red { }</td>
<td>13 (one class, three elements)</td>
</tr>
<tr>
<td>8</td>
<td>li.red.level { }</td>
<td>21 (two classes, one element)</td>
</tr>
<tr>
<td>9</td>
<td>style=”&#8221;</td>
<td>1000 (one inline styling)</td>
</tr>
<tr>
<td>10</td>
<td>p { }</td>
<td>1 (one HTML selector)</td>
</tr>
<tr>
<td>11</td>
<td>div p { }</td>
<td>2 (two HTML selectors)</td>
</tr>
<tr>
<td>12</td>
<td>.sith</td>
<td>10 (one class selector)</td>
</tr>
<tr>
<td>13</td>
<td>div p.sith { }</td>
<td>12 (two HTML selectors and a class selector)</td>
</tr>
<tr>
<td>14</td>
<td>#sith</td>
<td>100 (one id selector)</td>
</tr>
<tr>
<td>15</td>
<td>body #darkside .sith p { }</td>
<td>112 (HTML selector, id selector, class selector, HTML selector; 1+100+10+1)</td>
</tr>
</tbody>
</table>
<h3>Specificity: Basic Principles</h3>
<ul>
<li><strong>Equal specificity: the latest rule is the one that counts.</strong> “If you have written the same rule into your external style sheet twice, than the lower rule in your style sheet is closer to the element to be styled, it is deemed to be more specific and therefore will be applied.<br />
When selectors have an equal specificity value, such as</p>
<pre>#content h1 {
padding: 5px;
}

#content h1 {
padding: 10px;
}</pre>
<p>where both rules have the specificity <strong>0, 1, 0, 1</strong>, the latter rule is always applied.</li>
<li><strong>Unequal specificity: the more specific rule is the one that counts.</strong> “A selector can contain one or more reference points, the greater the specificity rating of these reference points the more specific your rule is.” Browsers select the most specific rules to display. [<a href="http://www.adobe.com/devnet/dreamweaver/articles/css_specificity_02.html">Understanding Specificity</a>]</li>
</ul>
<h3>Specificity Rules</h3>
<ul>
<li><strong>Rules with more specific selectors have a greater specificity.</strong> The more specific the references are in your selector, the greater the specificity for the rule. [<a href="http://www.adobe.com/devnet/dreamweaver/articles/css_specificity_03.html">Understanding Specificity</a>]</li>
<li><strong>ID selectors have a higher specificity than attribute selectors.</strong> For example, in HTML, the selector<code>#p123</code> is more specific than <code>[id=p123]</code> in terms of the cascade. Example: in
<pre>A:
a#a-02 { background-image : url(n.gif); }

and

B:
a[id="a-02"] { background-image : url(n.png); }</pre>
<p>the first rule (A) is more specific than the second one (B). [<a href="http://www.w3.org/TR/CSS21/selector.html">W3C CSS 2.1 Specification</a>]</li>
<li><strong>Contextual selectors are more specific than a single element selector.</strong> It also holds for other selectors involving more than one HTML element selector. [<a href="http://www.westciv.com/style_master/academy/css_tutorial/advanced/cascade_inheritance.html#specificity">Cascade Inheritance</a>]</li>
</ul>
<ul>
<li><strong>The embedded style sheet is closer to the element to be styled.</strong> So in the following situationCSS:
<pre>#content h1 {
padding: 5px;
}</pre>
<p>(X)HTML:</p>
<pre>&lt;style type="text/css"&gt;
#content h1 {
padding: 10px;
}
&lt;/style&gt;</pre>
<p>the latter rule will be applied. [<a href="http://www.adobe.com/devnet/dreamweaver/articles/css_specificity_02.html">Understanding Specificity</a>]</li>
<li><strong>The last rule defined overrides any previous, conflicting rules.</strong> For example, given these two rules
<pre>p { color: red; background: yellow }
p { color: green }</pre>
<p>paragraphs would appear in green text. They would also have a yellow background however, because the first rule is not completely negated. [<a href="http://www.brainjar.com/css/using/default4.asp">BrainJar.com</a>]</li>
<li><strong>A class selector beats any number of element selectors.</strong> <code>.introduction</code> beats <code>html body div div h2 p</code>. [<a href="http://iamacamera.org/default.aspx?id=95">CSS Specificity for Poker Players</a>]</li>
<li><strong>The universal selector has a specificity of 0, 0, 0, 0.</strong> <code>*</code>, <code>body *</code> and similar selectors have a zero specificity. <em>Inherited values also have a specificity of 0, 0, 0, 0.</em> [<a href="http://molly.com/2005/10/06/css2-and-css21-specificity-clarified/">CSS Specificity Clarified</a>]</li>
</ul>
<h3>Specificity Example</h3>
<ul>
<li>Consider three code fragments:
<pre>A: h1
B: #content h1
C: &lt;div id="content"&gt;
&lt;h1 style="color: #fff"&gt;Headline&lt;/h1&gt;
&lt;/div&gt;</pre>
<p>The specificity of A is <strong>0,0,0,1</strong> (<em>one</em> element), the specificity of B is <strong>0,1,0,1</strong> (one ID reference point and one element), the specificity value of C is <strong>1,0,0,0</strong>, since it is an inline styling.</p>
<p>Since</p>
<p>0001 = 1 &lt; 0101 = 101 &lt; 1000,</p>
<p>the third rule has a greater level of specificity, and therefore will be applied. If the third rule didn’t exist, the second rule would have been applied.</li>
</ul>
<h3>Specificity in Practice</h3>
<ul>
<li><strong>Use LVHA for link styling.</strong><br />
“To ensure that you see your various link styles, you’re best off putting your styles in the order “link-visited-hover-active”, or “LVHA” for short.” [<a href="http://meyerweb.com/eric/css/link-specificity.html">Link Specificity</a>]</li>
<li><strong>Never use <code>!important</code>.</strong><br />
“If you’re having specificity issues, there’s some quick ways to solve it. First, avoid <code>!important</code>.” “The<code>!important</code> declaration overrides normal declarations, but is unstructured and rarely required in an author’s style sheet.” [<a href="http://www.snook.ca/archives/html_and_css/understanding_c/">Understanding Specificity</a>, <a href="http://juicystudio.com/article/selector-specificity.php">Selector Specificity</a>]</li>
<li><strong>Use id to make a rule more specific.</strong><br />
Replacing <code>a.highlight</code> with <code>ul#blogroll a.highlight</code> changes the specificity from <strong>0, 0, 1, 1</strong> to <strong>0, 1, 1, 2</strong>.</li>
<li><strong>Minimize the number of selectors.</strong><br />
“Use the least number of selectors required to style an element.” [<a href="http://www.snook.ca/archives/html_and_css/understanding_c/">Understanding Specificity</a>]</li>
</ul>
<h3>CSS Specificity Tools &amp; Resources</h3>
<ul>
<li><a href="http://iamacamera.org/default.aspx?id=95">CSS Specificity for Poker Players</a><br />
If you’re not from the programming world and CSS seems a bit confusing, perhaps this analogy may help clear some concepts up. Think of CSS rules as poker hands. The best hand determines an element’s style.</li>
<li>CSS specificity calculator<br />
Calculates the specificity of a given selector.</li>
<li><a href="http://www.adobe.com/devnet/dreamweaver/articles/css_specificity.html">Understanding Specificity Tutorial</a><br />
In this tutorial you will look at specificity. Specificity is a type of weighting that has a bearing on how your cascading style sheet (CSS) rules are displayed. All rules in your style sheet carry a specificity rating regardless of selector type, although the weighting that is given to each selector type varies and will ultimately affect the styling of your web documents.</li>
<li><a href="http://www.westciv.com/style_master/academy/css_tutorial/advanced/cascade_inheritance.html#specificity">Cascade Inheritance: Specificity</a><br />
At this point it might be timely to have a quick discussion of specificity. Both inside a single style sheet, and in a cascade of style sheets, it should be clear that more than one rule can apply to the same element. What happens when two properties in separate rules which both apply to an element contradict one another?</li>
<li><a href="http://www.456bereastreet.com/archive/200509/css_21_selectors_part_1/">CSS 2.1 Selectors Explained</a><br />
An extensive overview of CSS 2.1 selectors. Learning how to use the full range of CSS selectors available in CSS 2.1 properly can actually help you keep your HTML a lot cleaner. It will let you minimise unnecessary use of the class attribute and the need for adding extraneous <code>div</code> and <code>span</code>elements to the markup.</li>
<li><a href="http://www.brunildo.org/test/IEASpec.html">CSS Specificity Bugs in IE</a><br />
A brief overview of specificity bugs implemented in Microsoft Internet Explorer/Win.</li>
<li><a href="http://www.htmlhelp.com/reference/css/structure.html">CSS Structure and Rules</a><br />
Basic Syntax, Pseudo-classes and Pseudo-elements, Cascading Order.</li>
<li><a href="http://www.htmldog.com/guides/cssadvanced/specificity/">Specificity</a><br />
It may not seem like something that important, and in most cases you won’t come across any conflicts at all, but the larger and more complex your CSS files become, or the more CSS files you start to juggle with, the greater likelihood there is of conflicts turning up.</li>
</ul>
<h3>What is what?</h3>
<ul>
<li>A <strong>selector</strong> is the element that is linked to a particular style. E.g. <code>p</code> in
<pre>p { padding: 10px; }</pre>
</li>
<li>A <strong>class selector</strong> is a selector that uses a defined class (multiple per page). E.g. <code>p.section</code> in
<pre>p.section { padding: 10px; }</pre>
</li>
<li>An <strong>ID selector</strong> is a selector that uses an individually assigned identifier (one per page). E.g.<code>p#section</code> in
<pre>CSS: #section { padding: 10px; }
(X)HTML: &lt;p id="section"&gt;Text&lt;/&gt;</pre>
</li>
<li>A <strong>contextual selector</strong> is a selector that defines a precise cascading order for the rule. E.g. <code>p span</code> in
<pre>p span { font-style: italic; }</pre>
<p>defines that all span-elements within a p-element should be styled in <em>italics</em>.</li>
<li>An <strong>attribute selector</strong> matches elements which have a specific attribute or its value. E.g. <code>p span</code> in
<pre>p[title] { font-weight: bold; }</pre>
<p>matches all p-elements which have a <code>title</code> attribute.</li>
<li><strong>Pseudo-classes</strong> are special classes that are used to define the behavior of HTML elements. They are used to add special effects to some selectors, which are applied automatically in certain states. E.g. <code>:visited</code> in
<pre>a:visited { text-decoration: underline; }</pre>
</li>
<li><strong>Pseudo-elements</strong> provide designers a way to assign style to content that does not exist in the source document. Pseudo-element is a specific, unique part of an element that can be used to generate content “on the fly”, automatic numbering and lists. E.g. <code>:first-line</code> or <code>:after</code> in
<pre>p:first-line { font-variant: small-caps; }
a:link:after { content: " (" attr(href) ")"; }</pre>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://www.fushanlang.com/css-specificity-things-you-should-know-change-560/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>超链接css样式优先级</title>
		<link>https://www.fushanlang.com/hyperlink-css-style-558/</link>
		<comments>https://www.fushanlang.com/hyperlink-css-style-558/#comments</comments>
		<pubDate>Wed, 15 Sep 2010 00:51:36 +0000</pubDate>
		<dc:creator><![CDATA[fushanlang]]></dc:creator>
				<category><![CDATA[css]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[网页设计]]></category>
		<category><![CDATA[超链接]]></category>

		<guid isPermaLink="false">http://www.fushanlang.com/blog/?p=558</guid>
		<description><![CDATA[<p>超链接的样式，例如 hover,visited,active样式 定义会出现没有实际效果的情况，实际上为了保证能看到不同状态下的连接样式,正确的样式顺序应该是：</p> <p>&#8221; link &#8211; visited &#8211; hover &#8211; active &#8220;或&#8221; LVHA &#8220;(缩写)。</p> <p>分析原因：</p> <p>每个选择符selector都有一个“specificity” 权重，如果两个selectors应用于同一个元素,具有较高specificity的选择符将胜出，具有优先权。例如：</p> <p>P.hithere {color: green;} /* specificity = 1,1 */</p> <p>P {color: red;} /* specificity = 1 */</p> <p>任何设置了类class＝hithere的段落内容显示为绿色而不是红色。两个selectors都设置了颜色，但是具有更高specificity的选择符将胜出。</p> <p>伪类如何影响specificity呢？它们具有完全相同的加权值，下列样式具有相同的specificity加权值：</p> <p>A:link {color: blue;} /* specificity = 1,1 */</p> <p>A:active {color: red;} /* specificity = 1,1 */</p> <p>A:hover {color: magenta;} <span style="color:#777"> . . . &#8594; Read More: <a href="https://www.fushanlang.com/hyperlink-css-style-558/">超链接css样式优先级</a></span>]]></description>
				<content:encoded><![CDATA[<p>超链接的样式，例如 hover,visited,active样式 定义会出现没有实际效果的情况，实际上为了保证能看到不同状态下的连接样式,正确的样式顺序应该是：</p>
<p>&#8221; link &#8211; visited &#8211; hover &#8211; active &#8220;或&#8221; LVHA &#8220;(缩写)。</p>
<p>分析原因：</p>
<p>每个选择符selector都有一个“specificity” 权重，如果两个selectors应用于同一个元素,具有较高specificity的选择符将胜出，具有优先权。例如：</p>
<p>P.hithere {color: green;} /* specificity = 1,1 */</p>
<p>P {color: red;} /* specificity = 1 */</p>
<p>任何设置了类class＝hithere的段落内容显示为绿色而不是红色。两个selectors都设置了颜色，但是具有更高specificity的选择符将胜出。</p>
<p>伪类如何影响specificity呢？它们具有完全相同的加权值，下列样式具有相同的specificity加权值：<span id="more-558"></span></p>
<p>A:link {color: blue;} /* specificity = 1,1 */</p>
<p>A:active {color: red;} /* specificity = 1,1 */</p>
<p>A:hover {color: magenta;} /* specificity = 1,1 */</p>
<p>A:visited {color: purple;} /* specificity = 1,1 */</p>
<p>这些都是用于超链接的样式设置。大部分情况下需要同时设置其中的几个样式，例如,一个未被访问的超链接在鼠标悬停和点击时可设置“鼠标悬停”和“鼠标激活“两种状态下的不同样式,由于上述三个规则都可应用于超链接,并且全部选择符具备相同的specificity,那么根据规则,最后一个样式“胜出”。所以&#8221; active &#8220;式样永远也不会显示出来，因为它总是被&#8221; hover &#8220;式样覆盖(即&#8221; hover &#8220;优先)。现在再来分析一下已经被访问过的超链接鼠标悬停是什么效果，结果永远是purple紫色的:( ，因为它的&#8221; visited &#8220;式样总是优先于其它的状态样式规则(包括&#8221; active &#8220;和&#8221; hover&#8221;)而显示。</p>
<p>这就是为什么CSS1推荐样式顺序的原因：</p>
<p>A:link</p>
<p>A:visited</p>
<p>A:hover</p>
<p>A:active</p>
<p>实际上，开头两个样式的顺序可以调换，因为一个超链接不可能同时存在“未访问”和“已访问”两种状态。( :link意思是&#8221; unvisited &#8220;；我不知道为什么不这样定义呢.)</p>
<p>CSS2现在允许伪类可以以“联合成组”形式出现，例如：</p>
<p>A:visited:hover {color: maroon;} /* specificity = 2,1 */</p>
<p>A:link:hover {color: magenta;} /* specificity = 2,1 */</p>
<p>A:hover:active {color: cyan;} /* specificity = 2,1 */</p>
<p>They have the same specificity, but they apply to fundamentally different beasts, and so don&#8217;t conflict. You can get hover-active combinations, for example.</p>
<p>如何理解本文当中所涉及到的“specificity”呢？specificity可以理解未简单地连在一起的号码字符串,上面的一个例子：</p>
<p>P.hithere {color: green;} /* specificity = 11 */</p>
<p>P {color: red;} /* specificity = 1 */</p>
<p>这好像是一个基于十进制的简单运算。然而计算“specificity”不能使用十进制算法，例如你把15种选择符连在一起使用、它们具有的“specificity”加权值还是比简单的class选择符低。举例：</p>
<p>.hello {color: red;} /* specificity = 10 */</p>
<p>HTML BODY DIV UL LI OL LI UL LI OL LI UL LI OL LI (color: green;} /* specificity = 15 */</p>
<p>&#8221; 10 &#8220;实际上是一个“1”后面接着“零”、不是&#8221;十&#8221;，我们可以使用十六进制描述前面的样式规则的specificitiy，像下面：</p>
<p>.hello {color: red;} /* specificity = 10 */</p>
<p>HTML BODY DIV UL LI OL LI UL LI OL LI UL LI OL LI (color: green;} /* specificity = F */</p>
<p>唯一的问题是如果你想为第二个样式规则增加两个或更多的选择符时，那时你就可能得到一个“17”的specificity、会再一次混淆。事实上specificity可能是无穷大的，所以为了避免更多的混乱，建议使用逗号来分隔specificity的值。</p>
]]></content:encoded>
			<wfw:commentRss>https://www.fushanlang.com/hyperlink-css-style-558/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
