<?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>Creative Individual Design Blog &#187; CSS3</title>
	<atom:link href="https://creativeindividual.co.uk/category/css3/feed/" rel="self" type="application/rss+xml" />
	<link>https://creativeindividual.co.uk</link>
	<description>A Place of Inspiration</description>
	<lastBuildDate>Wed, 21 Aug 2019 17:33:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4</generator>
		<item>
		<title>CSS3 Pseudo-classes: Harnessing the power!</title>
		<link>https://creativeindividual.co.uk/2011/08/css3-pseudo-classes/</link>
		<comments>https://creativeindividual.co.uk/2011/08/css3-pseudo-classes/#comments</comments>
		<pubDate>Wed, 17 Aug 2011 15:21:47 +0000</pubDate>
		<dc:creator>Laura Montgomery</dc:creator>
				<category><![CDATA[CSS3]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://creativeindividual.co.uk/?p=246</guid>
		<description><![CDATA[<a href="/2011/08/css3-pseudo-classes" title="CSS3 Pseudo-classes: Harnessing the power!"><img src="/wp-content/uploads/2011/08/css3-selectors-banner.png" /></a>

New to CSS3? Then understanding CSS3 pseudo-classes is a great place to start. This post will go into 4 common CSS3 pseudo-class selectors and explain how they work, plus how to use them. The 4 CSS3 pseudo-class selectors discussed in this post are:

<ul>
<li>:first-of-type</li>
<li>:last-of-type</li>
<li>:nth-of-type</li>
<li>:nth-last-of-type</li>
</ul>

There's is a working example included, so you can see the CSS at work!]]></description>
			<content:encoded><![CDATA[<p>If you have a passing interest in website development, or work with code everyday, there&#8217;s a pretty good chance that you&#8217;ve heard of HTML5 and CSS3 by now. There&#8217;s lots of discussion around HTML5 and how wonderful it is &#8211; and rightfully so. However I&#8217;m going to discuss some simple but power CSS3. Because let&#8217;s face it, CSS3 is where things get really exciting when developing a website.</p>
<p>This post will go into 4 of the most common (in my opinion) CSS3 pseudo-class selectors and how to use them in the real world. The 4 that I will be discussing are:</p>
<ul>
<li>:first-of-type</li>
<li>:last-of-type</li>
<li>:nth-of-type</li>
<li>:nth-last-of-type</li>
</ul>
<p>However there are lots more pseudo-classes on offer and its always worth getting a bit more familiar with them. I recommend the information on the sitepoint site for further reading:</p>
<p><a href="http://reference.sitepoint.com/css/css3psuedoclasses">http://reference.sitepoint.com/css/css3psuedoclasses</a></p>
<h2>:first-of-type</h2>
<p>If you can read English, you probably already have a pretty good idea what this CSS3 selector does. Simply, it allows you to pick the first item of a given type, such as the first list item in an unordered list.</p>
<p>Here&#8217;s a simple example of how you&#8217;d use it:</p>
<pre><strong>HTML</strong>
&lt;ul&gt;
  &lt;li&gt;First&lt;/li&gt;
  &lt;li&gt;Second&lt;/li&gt;
  &lt;li&gt;Third&lt;/li&gt;
&lt;/ul&gt;

<strong>CSS</strong>
ul li:first-of-type {
  font-weight: bold;
}</pre>
<p>The code above will create an unordered list of three items. The first item is then displayed in bold using CSS3.</p>
<p><strong>Example:</strong> <a title="CSS3 Pseudo-classes - Examples" href="http://creativeindividual.co.uk/wp-content/uploads/2011/css3-pseudo-classes/">http://creativeindividual.co.uk/wp-content/uploads/2011/css3-pseudo-classes/</a></p>
<h2>:last-of-type</h2>
<p>Similar to the :first-of-type selector, only this selector will pick the last item of a given type. So if we go back to our unordered list and simply change the CSS from :first-of-type to :last-of-type, we will see that the last item (the third in this case) is now in bold.</p>
<pre><strong>HTML</strong>
&lt;ul&gt;
  &lt;li&gt;First&lt;/li&gt;
  &lt;li&gt;Second&lt;/li&gt;
  &lt;li&gt;Third&lt;/li&gt;
&lt;/ul&gt;

<strong>CSS</strong>
ul li:last-of-type {
  font-weight: bold;
}</pre>
<p><strong>Example:</strong> <a title="CSS3 Pseudo-classes - Examples" href="http://creativeindividual.co.uk/wp-content/uploads/2011/css3-pseudo-classes/">http://creativeindividual.co.uk/wp-content/uploads/2011/css3-pseudo-classes/</a></p>
<h2>:nth-of-type</h2>
<p>Using the nth-of-type is when our CSS can get really powerful. There are several ways to use the :nth-of-type selector but I am only going to show you two. If you want to read more on the :nth-of-type, check out what sitepoint has to say:</p>
<p><a href="http://reference.sitepoint.com/css/pseudoclass-nthoftype">http://reference.sitepoint.com/css/pseudoclass-nthoftype</a></p>
<h3>:nth-of-type(3)</h3>
<p>This is the simpler form of :nth-of-type, simply declaring that we want to target <strong>only the third</strong> item of a given type. So back to our unordered list and by changing the CSS selector, only the third list item is in bold (no matter how many list items we add).</p>
<pre><strong>HTML</strong>
&lt;ul&gt;
  &lt;li&gt;First&lt;/li&gt;
  &lt;li&gt;Second&lt;/li&gt;
  &lt;li&gt;Third&lt;/li&gt;
&lt;/ul&gt;

<strong>CSS</strong>
ul li:nth-of-type(3) {
  font-weight: bold;
}</pre>
<p><strong>Example:</strong> <a title="CSS3 Pseudo-classes - Examples" href="http://creativeindividual.co.uk/wp-content/uploads/2011/css3-pseudo-classes/">http://creativeindividual.co.uk/wp-content/uploads/2011/css3-pseudo-classes/</a></p>
<h3>:nth-of-type(3n)</h3>
<p>Even more useful again is when we target every third item of a type using :nth-of-type(3n). We could change this to target every fourth, fifth, sixth, etc. by changing 3 to 4, 5, 6, etc.</p>
<p>If we create an unordered list of 6 items, you can see how :nth-of-type(3n) works:</p>
<pre><strong>HTML</strong>
&lt;ul&gt;
  &lt;li&gt;First&lt;/li&gt;
  &lt;li&gt;Second&lt;/li&gt;
  &lt;li&gt;Third&lt;/li&gt;
  &lt;li&gt;Forth&lt;/li&gt;
  &lt;li&gt;Fifth&lt;/li&gt;
  &lt;li&gt;Sixth&lt;/li&gt;
&lt;/ul&gt;

<strong>CSS</strong>
ul li:nth-of-type(3n) {
  font-weight: bold;
}</pre>
<p><strong>Example:</strong> <a title="CSS3 Pseudo-classes - Examples" href="http://creativeindividual.co.uk/wp-content/uploads/2011/css3-pseudo-classes/">http://creativeindividual.co.uk/wp-content/uploads/2011/css3-pseudo-classes/</a></p>
<p><em><strong>Note:</strong> If you want to target every second item, use <strong>:nth-of-type(even)</strong>. There is also a <strong>:nth-of-type(odd)</strong> selector.</em></p>
<h2>:nth-last-of-type</h2>
<p>After reading the last few examples, you&#8217;ve probably worked out what this selector does and how to use it. Rather than targeting just the last item of a given type, you can target the second or third last item, for example, or even the last two or three items. A really handy selector!</p>
<h3>:nth-last-of-type(3)</h3>
<p>By using :nth-last-of-type(3), we can target the third last item in our list:</p>
<pre><strong>HTML</strong>
&lt;ul&gt;
  &lt;li&gt;First&lt;/li&gt;
  &lt;li&gt;Second&lt;/li&gt;
  &lt;li&gt;Third&lt;/li&gt;
  &lt;li&gt;Forth&lt;/li&gt;
  &lt;li&gt;Fifth&lt;/li&gt;
  &lt;li&gt;Sixth&lt;/li&gt;
&lt;/ul&gt;

<strong>CSS</strong>
ul li:nth-last-of-type(3) {
  font-weight: bold;
}</pre>
<p><strong>Example:</strong> <a title="CSS3 Pseudo-classes - Examples" href="http://creativeindividual.co.uk/wp-content/uploads/2011/css3-pseudo-classes/">http://creativeindividual.co.uk/wp-content/uploads/2011/css3-pseudo-classes/</a></p>
<h3>:nth-last-of-type(-n+3)</h3>
<p>And finally, by using :nth-last-of-type(-n+3), we can target the last three items in our unordered list:</p>
<pre><strong>HTML</strong>
&lt;ul&gt;
  &lt;li&gt;First&lt;/li&gt;
  &lt;li&gt;Second&lt;/li&gt;
  &lt;li&gt;Third&lt;/li&gt;
  &lt;li&gt;Forth&lt;/li&gt;
  &lt;li&gt;Fifth&lt;/li&gt;
  &lt;li&gt;Sixth&lt;/li&gt;
&lt;/ul&gt;

<strong>CSS</strong>
ul li:nth-last-of-type(-n+3) {
  font-weight: bold;
}</pre>
<p><strong>Example:</strong> <a title="CSS3 Pseudo-classes - Examples" href="http://creativeindividual.co.uk/wp-content/uploads/2011/css3-pseudo-classes/">http://creativeindividual.co.uk/wp-content/uploads/2011/css3-pseudo-classes/</a></p>
<h2>Older Browsers</h2>
<p>If you are knowledgable with CSS3, you probably already realise that there is a small problem with using CSS3 pseudo-classes, and it is that older browsers will not support them!</p>
<p>Don&#8217;t let that put you off for fear not, there has already been some very clever people who have realised this problem and have used Javascript to work around it, saving our sanity in the process!</p>
<p>The Javascript that I use on a regular basis is Selectivizr, which you can read more about and download from here:</p>
<p><a href="http://selectivizr.com/">http://selectivizr.com/</a></p>
<h2>Conclusion</h2>
<p>I love the power and simplicity of CSS3 pseudo-classes. It saves us from littering our HTML and CSS with unnecessary classes just to deal with unwanted padding or margin, such as on the last paragraph of a block of text. It enables us to easily create zebra-strips on an index or list page. I find they help me a lot with fine-tuning the spacing in a grid of thumbnails on a gallery page. And they&#8217;ll definitely make your life easier when using a CMS (content management system) to stop the site from looking broken long after the initial launch.</p>
<p>As with much HTML5 and CSS3, the possibilities are only limited by your own imagination! And I hope my list of examples of four common pseudo-class selectors has given you some food for thought.</p>
<p><em>Have you have used CSS3 pseudo-classes in a recent website project? Then why not tell us about it in the comments?</em></p>
]]></content:encoded>
			<wfw:commentRss>https://creativeindividual.co.uk/2011/08/css3-pseudo-classes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
