<?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>KirkHings.com &#187; selector</title>
	<atom:link href="http://kirkhings.com/tag/selector/feed/" rel="self" type="application/rss+xml" />
	<link>http://kirkhings.com</link>
	<description>The '-berger' part trips up so many folks that I sometimes simplify my name for you. Yes, just for you.</description>
	<lastBuildDate>Sun, 27 Feb 2011 23:53:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>How to Count CSS Specificity Scores and Leverage !Important Overrides</title>
		<link>http://kirkhings.com/code/css/how-to-count-css-specificity-scores-and-leverage-important-overrides/401/</link>
		<comments>http://kirkhings.com/code/css/how-to-count-css-specificity-scores-and-leverage-important-overrides/401/#comments</comments>
		<pubDate>Mon, 25 May 2009 00:43:09 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[concept]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[element]]></category>
		<category><![CDATA[id]]></category>
		<category><![CDATA[inline]]></category>
		<category><![CDATA[selector]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=401</guid>
		<description><![CDATA[photo credit: laffy4k If you are having difficulty getting a particular CSS style to &#8220;appear&#8221;, one thing to check is your specificity. CSS specificity is a four digit score for resolving cascade conflicts. You can actually add up the specificity score to determine which conflicting style should prevail. The four digit score is comprised of [...]


Related posts:<ul><li><a href='http://kirkhings.com/code/css/space-critical-css-selectors/65/' rel='bookmark' title='Space is Critical in CSS Selectors'>Space is Critical in CSS Selectors</a> <small>photo credit: makelessnoise If you are having difficulty getting a...</small></li>
<li><a href='http://kirkhings.com/code/c/test-scores-and-letter-grade/413/' rel='bookmark' title='Test Scores and Letter Grade'>Test Scores and Letter Grade</a> <small>photo credit: quinn.anya This programming challenge is from an initial...</small></li>
</ul>]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/73207064@N00/173579871/" title="Score Kept Here" target="_blank"><img src="http://farm1.static.flickr.com/45/173579871_4cae652b36.jpg" alt="Score Kept Here" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by/2.0/" title="Attribution License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/73207064@N00/173579871/" title="laffy4k" target="_blank">laffy4k</a></small></div>
<p>If you are having difficulty getting a particular CSS style to &#8220;appear&#8221;, one thing to check is your specificity. CSS specificity is a four digit score for resolving cascade conflicts. You can actually add up the specificity score to determine which conflicting style should prevail. The four digit score is comprised of inline style, id, class, and element. You can express it as #, #, #, #. Examples are provided below.</p>
<p>For example, what gets displayed when you have two conflicting styles specified for the same element?</p>
<pre class="brush: css; title: ; notranslate">
li { background-color: blue; }
li { background-color: yellow; }
</pre>
<p>Of course we know intuitively and through practice that the second style is displayed because it is read after the first and so it &#8220;overwrites&#8221; that first style. Less intuitively, you should know that the second wins because it has the same level of specificity as that first style. The two styles above each have the score 0, 0, 0, 1 because they are not inline styles, they have no id or class specification, and they specify only one element each.</p>
<p>Specificity refers to how specific you identify an element. Referring to just &#8216;li&#8217; is just the single list item element, you are not referring to additional element information. This is like specifying different styles based on nesting, such as &#8216;div a&#8217; vs. &#8216;p a&#8217;. Those could have two different styles because they have different nesting.</p>
<p>For another example, which style do you think will get displayed?</p>
<pre class="brush: css; title: ; notranslate">
ul li { background-color: blue; }
li { background-color: yellow; }
</pre>
<p>The first style will win over the second because of greater element specificity, despite the potential of overwriting later in the script. The first style&#8217;s specificity score is 0, 0, 0, 2 while the second style&#8217;s score is still just 0, 0, 0, 1. That tiny extra specificity causes the first style to win out and get displayed. Now the power of specificity should be dawning on you, and why specificity can cause frustrating display problems in a large stylesheet. Let&#8217;s explore further. </p>
<p>Which style do you think will get displayed?</p>
<pre class="brush: css; title: ; notranslate">
.nav li { background-color: blue; }
ul li { background-color: yellow; }
</pre>
<p>Of course the first style wins again because the class selector has greater specificity than the element selectors. The score is 0, 0, 1, 1, versus 0, 0, 0, 2.</p>
<p>Which style do you think will get displayed?</p>
<pre class="brush: css; title: ; notranslate">
#nav li { background-color: blue; }
.nav ul li { background-color: yellow; }
</pre>
<p>Of course the first style wins again because the id selector has greater specificity than the class selector. The score is 0, 1, 0, 1, versus 0, 0, 1, 2.</p>
<p>Which style do you think will get displayed?</p>
<pre class="brush: css; title: ; notranslate">
#nav .nav div ul li { background-color: yellow; }
...
&lt;body&gt;
&lt;div id=&quot;nav&quot;&gt;&lt;ul&gt;&lt;li style=&quot;background-color: blue;&quot;&gt;Data&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/body&gt;
</pre>
<p>The inline style gets displayed here (though only on the elements containing the inline style) because inline styles have slightly higher specificity than id, class, or element styles specified in header styles or external stylesheets (remember &#8216;cascading&#8217; is a key concept). The score is 1, 0, 0, 0 versus 0, 1, 1, 3. Although the specificity is greater here, you realize inline styles are an evil thing because you are mixing style with content. You should know the rule exists and how it works, and then pledge to never take it that far unless you are debugging code. </p>
<p>Speaking of debugging code, what if you specify something as much as you reasonably can, yet you cannot get it to display? Rather than dropping it into an inline style, you can use the !important keyword in your style, which functions as a kind of override function as far as specificity goes. </p>
<p>Which style do you think will get displayed?</p>
<pre class="brush: css; title: ; notranslate">
#nav .nav div ul li { background-color: blue !important; }
#nav .nav div ul li { background-color: yellow; }
</pre>
<p>Of course the first style wins again because of the !important keyword (yes, it will also beat an inline style specification). This case indicates you have conflicting styles in your stylesheet and you need to check specificity to target what is conflicting. If your style does not display when you use !important, it indicates you have another problem like misspelling an element, class, or id. </p>
<p>After you have fixed the conflict, go back and remove the !important keyword or you will forever be chasing your tail across stylesheets with more and more !important declarations. Use !important only for debugging purposes. </p>
<p>Finally, remember specificity only comes into play when styles conflict. You can have dozens of separate style statements work beautifully together so long as they are specifying different style elements (such as background-color, font-style, color, padding, etc.) It is only when they specify the same style elements with conflicting values does the specificity score become important.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fkirkhings.com%2Fcode%2Fcss%2Fhow-to-count-css-specificity-scores-and-leverage-important-overrides%2F401%2F&amp;title=How%20to%20Count%20CSS%20Specificity%20Scores%20and%20Leverage%20%21Important%20Overrides" id="wpa2a_2"><img src="http://kirkhings.com/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share"/></a></p>

<p>Related posts:<ul><li><a href='http://kirkhings.com/code/css/space-critical-css-selectors/65/' rel='bookmark' title='Space is Critical in CSS Selectors'>Space is Critical in CSS Selectors</a> <small>photo credit: makelessnoise If you are having difficulty getting a...</small></li>
<li><a href='http://kirkhings.com/code/c/test-scores-and-letter-grade/413/' rel='bookmark' title='Test Scores and Letter Grade'>Test Scores and Letter Grade</a> <small>photo credit: quinn.anya This programming challenge is from an initial...</small></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/css/how-to-count-css-specificity-scores-and-leverage-important-overrides/401/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Space is Critical in CSS Selectors</title>
		<link>http://kirkhings.com/code/css/space-critical-css-selectors/65/</link>
		<comments>http://kirkhings.com/code/css/space-critical-css-selectors/65/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 05:04:58 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[concept]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[selector]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=65</guid>
		<description><![CDATA[photo credit: makelessnoise If you are having difficulty getting a particular CSS style to &#8220;appear&#8221;, one thing to look at is your selector. A tiny space can cause two different selections if you are not careful. For example, notice the single space difference between these two selectors: That presence or lack of a single space [...]


Related posts:<ul><li><a href='http://kirkhings.com/code/css/how-to-count-css-specificity-scores-and-leverage-important-overrides/401/' rel='bookmark' title='How to Count CSS Specificity Scores and Leverage !Important Overrides'>How to Count CSS Specificity Scores and Leverage !Important Overrides</a> <small>photo credit: laffy4k If you are having difficulty getting a...</small></li>
</ul>]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/13447091@N00/251478651/" title="Andromeda, again." target="_blank"><img src="http://farm1.static.flickr.com/96/251478651_e0b3f9944d.jpg" alt="Andromeda, again." border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by/2.0/" title="Attribution License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/13447091@N00/251478651/" title="makelessnoise" target="_blank">makelessnoise</a></small></div>
<p>If you are having difficulty getting a particular CSS style to &#8220;appear&#8221;, one thing to look at is your selector. A tiny space can cause two different selections if you are not careful.</p>
<p>For example, notice the single space difference between these two selectors:</p>
<pre class="brush: css; title: ; notranslate">
div#navbar {
    background-color: #999;
}
div #navbar {
    background-color: #333;
}
</pre>
<p>That presence or lack of a single space between the (div) element and the class selection symbol (#) causes two different selections to occur. No space means the style will apply to (div) elements which are themselves marked with the class navbar. A single space means the style will apply to elements within the (div) element which themselves are marked with the class navbar. </p>
<p>For example, </p>
<pre class="brush: css; title: ; notranslate">
div#navbar {
    background-color: blue;
}
</pre>
<p>would make this entire div&#8217;s background blue:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;div class=&quot;navbar&quot;&gt;
  &lt;p&gt;item&lt;/p&gt;
  &lt;p&gt;item&lt;/p&gt;
&lt;/div&gt;
</pre>
<p>However, this:</p>
<pre class="brush: css; title: ; notranslate">
div #navbar {
    background-color: blue;
}
</pre>
<p>would not apply the style. That critical space indicates a descendant element selector. It is expecting the inner (p) elements to have the navbar class. Since those (p) elements do not have navbar classes, you would see nothing and probably wonder why &#8220;it is not working&#8221;.</p>
<p>The descendant selector can be useful sometimes, but know the difference of what happens if that space appears in that critical spot so you don&#8217;t pull your hair out in frustration. If your style is not appearing, check for this easy space mistake. </p>
<p>Nerd Aside: The # symbol goes by many names around the world, such as pound sign, number sign, hash, hatch, tic-tac-toe, gate, crunch, or my favorite octothorpe. Mmm, octothorpe!</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fkirkhings.com%2Fcode%2Fcss%2Fspace-critical-css-selectors%2F65%2F&amp;title=Space%20is%20Critical%20in%20CSS%20Selectors" id="wpa2a_4"><img src="http://kirkhings.com/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share"/></a></p>

<p>Related posts:<ul><li><a href='http://kirkhings.com/code/css/how-to-count-css-specificity-scores-and-leverage-important-overrides/401/' rel='bookmark' title='How to Count CSS Specificity Scores and Leverage !Important Overrides'>How to Count CSS Specificity Scores and Leverage !Important Overrides</a> <small>photo credit: laffy4k If you are having difficulty getting a...</small></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/css/space-critical-css-selectors/65/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

