<?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>Scrunchup &#187; practical</title>
	<atom:link href="http://scrunchup.com/tag/practical/feed/" rel="self" type="application/rss+xml" />
	<link>http://scrunchup.com</link>
	<description>The Web Magazine for Young Designers and Developers</description>
	<lastBuildDate>Mon, 30 Jan 2012 16:23:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>CSS Nuggets in Scrunchup</title>
		<link>http://scrunchup.com/article/css-nuggets/</link>
		<comments>http://scrunchup.com/article/css-nuggets/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 00:03:48 +0000</pubDate>
		<dc:creator>anna</dc:creator>
				<category><![CDATA[article]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[intermediate]]></category>
		<category><![CDATA[practical]]></category>

		<guid isPermaLink="false">http://scrunchup.com/?p=484</guid>
		<description><![CDATA[Some CSS tips and techniques that have been applied on the Scrunchup site.


Related posts:<ol><li><a href='http://scrunchup.com/article/the-evolution-of-a-jquery-plugin/' rel='bookmark' title='The Evolution of a jQuery Plugin'>The Evolution of a jQuery Plugin</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>While building Scrunchup, I learnt a few little CSS tricks.  Some of them are visual enhancements, others help make your code cleaner.  While they&#8217;re not all compatible in some of the older or less capable browsers (*coughIEcough*), you should still be able to start using some of them today.</p>
<h3>:first-of-type</h3>
<p>For an example of this, have a look at the first paragraph of this article.  I have set the font size of the first paragraph to be slightly larger than the paragraph text in the rest of the article to draw the reader in.</p>
<div class="example">
<code>p {<br />
font-size:1em;<br />
}<br />
p:first-of-type {<br />
font-size:1.2em;<br />
}</code>
</div>
<h3>:last-of-type</h3>
<p>The :last-of-type selector is useful if you want to take the bottom border out of a table.  In this example I&#8217;m changing the bottom border from black to red so you can see it working.</p>
<div class="code-example">
<code>tr {<br />
border-bottom:3px solid black;<br />
}<br />
tr:last-of-type {<br />
border-bottom: 3px solid red;<br />
}</code>
</div>
<div class="visual-example">
<style type="text/css">
tr:nth-child(2n+1) {
background-color:none;
}
table tr {
border-bottom:3px solid black;
}
table tr:last-of-type {
border-bottom:3px solid red;
}</style>
<table border="0">
<tbody>
<tr>
<th>Rank</th>
<th>Top Rated Films of the New Millennium (IMDB)</th>
</tr>
<tr>
<td>1</td>
<td>The Dark Knight (2008)</td>
</tr>
<tr>
<td>2</td>
<td>The Lord of the Rings: The Return of the King (2003)</td>
</tr>
<tr>
<td>3</td>
<td>City of God (2002)</td>
</tr>
<tr>
<td>4</td>
<td>The Lord of the Rings: The Fellowship of the Ring (2001)</td>
</tr>
<tr>
<td>5</td>
<td>Up (2009)</td>
</tr>
</tbody>
</table>
</div>
<p>Remember this isn&#8217;t just restrained to tables, you can use it for other elements too.</p>
<h3>nth-of-type</h3>
<p><img class="alignleft example" src="http://maban.co.uk/wp-content/uploads/2009/08/grids-1.gif" alt="A graphic of a perfectly aligned grid" width="300" height="300" />One of the things that CSS is not great at (at the moment) is grid layouts.  Scrunchup&#8217;s homepage has grid blocks for each article, and getting them to line up vertically and horizontally was a bit of a challenge.  I want to tell the block on the end of the row to clear, but none of the others, and the only way of doing this with CSS that I knew of was by adding a class to the last block on each row with, and targeting it with clear:left.  Not only does this add extra markup, but the content I&#8217;m styling is dynamic &#8211; what is the last block in the row at one time may be the first at another.  Our search results page is a good example of this.</p>
<h4>Why you shouldn&#8217;t define heights</h4>
<p><img class="alignleft example" src="http://maban.co.uk/wp-content/uploads/2009/08/grids-2.gif" alt="A graphic of a poorly aligned grid" width="300" height="300" />We <em>could</em> give each block a height of, say, 400px and that would solve the problem.  But this is a really bad idea.  If someone visiting your site wants to increase the text size, or you have just a little more text in the block than the block can hold, it will break the layout.  This is one of the things that differentiates web design from print design &#8211; content on the web is dynamic.</p>
<h4>Using a wrapper div</h4>
<p><img class="alignleft example" src="http://maban.co.uk/wp-content/uploads/2009/08/grids-3.gif" alt="A graphic of a perfectly aligned grid with a wrapper for each row" width="300" height="300" />Having different sized boxes without setting the 1st one on each row to clear causes the layout to break.  The most common way of fixing this is to put each row in a wrapper div.  But this adds extra markup, and you still have the same problem if the grid blocks are not static.</p>
<h3>Using nth-of-type</h3>
<p>Here&#8217;s the code I used to solve this problem:</p>
<div class="example"><code>.block:nth-of-type(3n+1){<br />
clear:both;<br />
}</code></div>
<p>It uses a little bit of maths that, very basically put, tells the 4th block in every row to fully clear the blocks above it.  Being able to specifically target an element without giving it a different class is incredibly useful.</p>
<p>We can also use exactly the same technique to turn off the margins in the 3rd block of every row so it lines up flush to the left and the right.</p>
<p><img class="aligncenter visual-example" src="http://maban.co.uk/wp-content/uploads/2009/08/grids-4.gif" alt="A graphic of blocks without margins on the edge blocks" width="300" height="300" /></p>
<div class="code-example">
<code>.block:nth-of-type(3n+3){<br />
margin-right:0;<br />
}</code>
</div>
<p>nth-of-type is an incredibly powerful selector, and can be used in some really creative ways.</p>
<h4>Faux Randomness</h4>
<p>For the tag cloud at the bottom of each page, I got the tags to rotate at different angles.  It looks random, but it&#8217;s not really.  Here&#8217;s the code:</p>
<div class="example">
<code>.tag:nth-of-type(3n+1){<br />
transform:rotate(2deg);<br />
}<br />
.tag:nth-of-type(5n+1){<br />
transform:rotate(-2deg);<br />
}</code>
</div>
<h3>Shadows</h3>
<p>In case you didn&#8217;t know how to already, here&#8217;s how to use CSS to make shadows.</p>
<div class="example">
<code>.block {<br />
box-shadow:1px 2px 3px #333333;<br />
}</code>
</div>
<p>I&#8217;m going to break this up to show you what does what.  The first bit we define is how far we want the shadow to offset to the right, so 1px shifts the shadow 1px to the right.  The next bit shifts it down, so in this example, it will be shifted 2px down. Where I&#8217;ve put 3px is the amount of blur the shadow will have.  If I put 0px, it would be a solid shadow.  Finally, I&#8217;ve given the shadow a colour of #333333 which is a dark grey.</p>
<h4>Inset Shadows</h4>
<p>You can also do inset shadows.  These can look quite good on form labels, such as search boxes to make it look like it&#8217;s cut into the background.  Here&#8217;s the code:</p>
<div class="example"><code>.block{<br />
box-shadow:inset 1px 2px 3px #333333;<br />
}</code></div>
<p>Each browser has its own proprietary way of understanding box-shadow because it&#8217;s quite new, so to make sure it shows up in previous versions of browsers, you&#8217;ll need to repeat the code like this:</p>
<div class="example">
<code>-webkit-box-shadow:1px 2px 3px #333333; /* Safari */<br />
-moz-box-shadow:1px 2px 3px #333333; /* Firefox */<br />
box-shadow:1px 2px 3px #333333; /* Catch-all (I put this at the end of the list so it's the most specific) */</code>
</div>
<p>IE has it&#8217;s own unique way of doing shadows, so you&#8217;ll need to put this in your CSS too…</p>
<div class="example">
<code>filter:shadow(color=gray, direction=100,<br />
strength=5);</code>
</div>
<h3>Transform</h3>
<p>If you have a look at the author photo at the top of the page, you should see it&#8217;s slightly tilted (depending on your browser version).<br />
To achieve this effect, you can use the following code:</p>
<div class="example">
<code>.img {<br />
transform:rotate(2deg);<br />
}</code>
</div>
<p>This rotates the element 2 degrees clockwise.  If you put (-2deg), it turns it the opposite way.<br />
To make it look like a polaroid photo, you can use this code:</p>
<div class="example">
<code>.img{<br />
transform:rotate(2deg); /* tilts the photo */<br />
padding:3px; /* adds a border round the image */<br />
background-color:white; /* colours the border white */<br />
border:1px solid gray; /* makes the photo stand out against a white background */<br />
box-shadow:2px 2px 3px gray; /* gives the image a shadow */<br />
}</code>
</div>
<h3>Icons</h3>
<p>To automatically show a filetype icon on a link to a file, you can use something called an attribute selector.  In this example, I&#8217;ve set all links to .pdf files to display a little pdf icon.  The href$=&#8217;.pdf&#8217; bit looks for anything that ends with the string .pdf.</p>
<div class="example">
<code>a[href$='.pdf'],<br />
a[href$='.PDF']{<br />
background-image:url(../images/icons/pdf.gif);<br />
background-repeat:no-repeat;<br />
background-position:left 5px;<br />
padding:0.3em 0 0.4em 25px;<br />
}</code>
</div>
<p>You can also do the same thing with links.  I&#8217;ve set all the links on this site that go to Flickr to display a little Flickr icon. The href*=&#8221;http://flickr.com&#8221; bit looks for anything that contains the string http://flickr.com.</p>
<div class="example">
<code>a[href*=”http://flickr.com”],<br />
a[href*=”http://www.flickr.com”] {<br />
background-image:url(../images/icons/flickr.gif);<br />
}</code>
</div>
<h3>Browser Support</h3>
<p>Most modern browsers support all of these CSS nuggets, with the exception of IE.  I&#8217;d put a full browser support table for each of these selectors but it would date very quickly, so to find out which browsers currently support which selectors, head on down to <a href="http://www.quirksmode.org/compatibility.html">www.quirksmode.org</a> &#8211; it has a lovely compatability table and resources which I refer to extensively, and I really recommend it.</p>
<p>Got your own CSS Nuggets?  Share them in the comments.</p>


<p>Related posts:<ol><li><a href='http://scrunchup.com/article/the-evolution-of-a-jquery-plugin/' rel='bookmark' title='The Evolution of a jQuery Plugin'>The Evolution of a jQuery Plugin</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://scrunchup.com/article/css-nuggets/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>

