<?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>The Bright Lines &#187; selector</title>
	<atom:link href="http://www.thebrightlines.com/tag/selector/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thebrightlines.com</link>
	<description>HTML, CSS, Javascript and more</description>
	<lastBuildDate>Thu, 29 Jul 2010 22:44:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Too advanced CSS selectors</title>
		<link>http://www.thebrightlines.com/2009/12/24/too-advanced-css-selectors/</link>
		<comments>http://www.thebrightlines.com/2009/12/24/too-advanced-css-selectors/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 22:27:05 +0000</pubDate>
		<dc:creator>Wouter Bos</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[selector]]></category>

		<guid isPermaLink="false">http://www.thebrightlines.com/?p=273</guid>
		<description><![CDATA[CSS offers lots of different selectors, but sometimes it's better not to use them.]]></description>
			<content:encoded><![CDATA[<p>A few years ago the only reasonable way to select an element through CSS was by using classes and/or tag names. If you wanted to target a specific element, you had to select it by using its classname. Selecting elements with tag names wasn&#8217;t always practicle. For example: if you use the selector <code>div.container div</code>, you style all <code>div</code> elements inside the <code>div.container</code>.</p>
<p>Most of the time that&#8217;s not what you want. Using tag names as selectors can also make design changes troublesome if you have to add more <code>div</code>s and need to give it a different styling. But since IE7 we can use new selectors that help us to build much more delicate CSS selectors. Those new selectors are:</p>
<ul>
<li>Child selector: <code>.parent &gt; .child</code></li>
<li>First child selector: <code>.parent .child:first-child</code></li>
<li>Adjacent selector: <code>.sibling + .sibling</code></li>
<li>First child selector: <code>div:first-child</code></li>
<li>Attribute selectors: <code>div[id]</code></li>
</ul>
<p>Now you can style the following HTML with just a single class:</p>
<h3>HTML:</h3>
<pre name="code" class="xhtml:nogutter">
<div class='container'>
	<span>This is some title</span>
	<a href="#">Lorem ipsum</a>
	<a href="#">Lorem ipsum</a>
	<a href="#">Lorem ipsum</a>
	<span>Some body text with lots of <span>lorem <span>ipsums</span></span> in it</span>
	<span>Some body text with lots of <span>lorem <span>ipsums</span></span> in it</span>
	<a href="#">Lorem ipsum</a>
</div>
</pre>
<h3>CSS:</h3>
<pre name="code" class="css:nogutter">
.container span {
	font-family: sans-serif;
}
.container > span:first-child {
	font-size: 1.5em;
	font-weight: bold;
	font-family: serif;
}
.container > span > span {
	font-style: italic;
}
.container > span > span > span {
	font-weight: bold;
}
.container > span + span {
	display: block;
	margin-top: 0.5em;
}
.container a:nth-child(2) {
	font-weight: bold;
}
.container a {
	display: block;
	color: #f0f;
}
.container a:nth-child(4) ~ a {
	font-size: 0.9em;
}
</pre>
<p>The advantage is that you can, as you can see, style whole blocks of HTML code with just one class. In theory you could even use no classes at all. Without &#8216;class&#8217;-attributes on every element, the HTML code looks just a little bit more cleaner and readable. You can also use those and other selectors in javascript libraries like jQuery to select particular HTML elements. Since most libraries are cross-browser you don&#8217;t have to worry if a selector is or isn&#8217;t supported by a browser. Consider this piece of code in jQuery:</p>
<pre name="code" class="javascript:nogutter">
jQuery(".container a:nth-child(4) ~ a").click( function() {
	alert('hello world');
})
</pre>
<h2>That&#8217;s all nice, but should I use it?</h2>
<p>No. I use them some times, but I try to stay away from it if possible. The problem with these selectors is that they rely so much on the structure of the HTML that it becomes harder to change the HTML structure. If you take another look at the HTML and CSS that I showed as an example in the beginning of the article, it&#8217;s clear that you can&#8217;t add a few links or spans without having to change the CSS. Add some advanced jQuery selectors in the mix and you may have locked the HTML structure forever since no-one dares to change it.</p>
<h2>Should I discard those new selectors then?</h2>
<p>Not necessarily. Sometimes you just have no choice if you for some reason are not able to change the HTML. My personal guideline is that styling has to be predictable. So when you edit some piece of HTML code, the styling should behave as expected. That means that you don&#8217;t style your HTML in a way that the text-color of the 4th <code>span</code> is red.</p>
<p>So what can be styled predictable? If you ask me advanced selectors shine when the structure is relatively fixed. Lists are a good example. Just take a look at these code blocks below. The styling will behave normal, regardless what you do with the HTML.</p>
<h3>HTML:</h3>
<pre name="code" class="xhtml:nogutter">
<ul class="list">
<li>Lorem</li>
<li>Ipsum</li>
<li>Dolor
<ul>
<li>Sit</li>
<li>Amet</li>
</ul>
</ul>
</pre>
<h3>CSS:</h3>
<pre name="code" class="css:nogutter">
ul.list li + li {
	margin-top: 10px; /* adds top margin to all list items except the first */
}
ul.list > li {
	font-weight: bold; /* styles only the first level list items differently */
}
</pre>
<h3>Javascript:</h3>
<pre name="code" class="javascript:nogutter">
jQuery("ul.list li:nth-child(even)").addClass("zebra"); // adds zebra striping to a list
</pre>
<h2>HTML elements you can easily apply advanced selectors</h2>
<p>Besides <code>ul</code>, you can style <code>ul</code>s, <code>dl</code>s, <code>table</code>s. That doesn&#8217;t mean that other elements should only be styled with classes. There are always some exceptions to the rule. Just let your common sense be the judge.</p>


<p>Related posts:<ol><li><a href='http://www.thebrightlines.com/2009/11/08/how-to-overcome-the-limits-of-css-in-internet-explorer-6/' rel='bookmark' title='Permanent Link: How to overcome the limits of CSS in Internet Explorer 6'>How to overcome the limits of CSS in Internet Explorer 6</a></li>
<li><a href='http://www.thebrightlines.com/2009/10/28/for-the-sloppy-clickers/' rel='bookmark' title='Permanent Link: For the sloppy clickers'>For the sloppy clickers</a></li>
<li><a href='http://www.thebrightlines.com/2010/01/04/alternative-for-nth-of-type-and-nth-child/' rel='bookmark' title='Permanent Link: Alternative for :nth-of-type() and :nth-child()'>Alternative for :nth-of-type() and :nth-child()</a></li>
<li><a href='http://www.thebrightlines.com/2010/01/31/hack-how-to-enable-first-child-in-ie6/' rel='bookmark' title='Permanent Link: Hack: how to enable :first-child in IE6'>Hack: how to enable :first-child in IE6</a></li>
<li><a href='http://www.thebrightlines.com/2009/11/27/filtering-ie6-and-ie7-in-quirks-mode/' rel='bookmark' title='Permanent Link: What you can&#8217;t do in IE&#8217;s Quirks Mode'>What you can&#8217;t do in IE&#8217;s Quirks Mode</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.thebrightlines.com/2009/12/24/too-advanced-css-selectors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
