<?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>CSS Tutorials and CSS Articles &#187; CSS ID</title>
	<atom:link href="http://www.cssmaster.co.uk/tag/css-id/feed" rel="self" type="application/rss+xml" />
	<link>http://www.cssmaster.co.uk</link>
	<description>CSS tutorials and articles</description>
	<lastBuildDate>Tue, 02 Feb 2010 01:43:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>CSS Classes and CSS IDs</title>
		<link>http://www.cssmaster.co.uk/css-classes-and-css-ids</link>
		<comments>http://www.cssmaster.co.uk/css-classes-and-css-ids#comments</comments>
		<pubDate>Thu, 14 Jan 2010 22:43:26 +0000</pubDate>
		<dc:creator>CSS Master</dc:creator>
				<category><![CDATA[CSS Tutorials]]></category>
		<category><![CDATA[CSS Class]]></category>
		<category><![CDATA[CSS ID]]></category>

		<guid isPermaLink="false">http://www.cssmaster.co.uk/?p=15</guid>
		<description><![CDATA[In CSS you can use ID&#8217;s and classes to reference your elements individually.  
CSS Classes
Look at this running example.
HTML:
&#60;img src="images/image1.jpg" alt="image 1" /&#62;
&#60;img src="images/image2.jpg" alt="image 2" /&#62;
&#60;img src="images/image3.jpg" alt="image 3" /&#62;
&#60;img src="images/image4.jpg" alt="image 4" /&#62;
&#60;img src="images/image5.jpg" alt="image 5" /&#62;
These 5 images are all on one HTML page.  I want the middle image to [...]]]></description>
			<content:encoded><![CDATA[<p>In <strong>CSS</strong> you can use ID&#8217;s and classes to reference your elements individually.  </p>
<h2>CSS Classes</h2>
<p>Look at this running example.</p>
<p><strong>HTML:</strong><br />
<code>&lt;img src="images/image1.jpg" alt="image 1" /&gt;<br />
&lt;img src="images/image2.jpg" alt="image 2" /&gt;<br />
&lt;img src="images/image3.jpg" alt="image 3" /&gt;<br />
&lt;img src="images/image4.jpg" alt="image 4" /&gt;<br />
&lt;img src="images/image5.jpg" alt="image 5" /&gt;</code></p>
<p>These 5 images are all on one HTML page.  I want the middle image to have a 5 pixel border.  If I use a global selector:<br />
<code>img { border:5px solid red; }</code><br />
then all of the images will have the 5 pixel border.  This is where classes come in.  Take the HTML for before with a tiny addition:<br />
<code>&lt;img src="images/image1.jpg" alt="image 1" /&gt;<br />
&lt;img src="images/image2.jpg" alt="image 2" /&gt;<br />
&lt;img src="images/image3.jpg" alt="image 3" class="my_class" /&gt;<br />
&lt;img src="images/image4.jpg" alt="image 4" /&gt;<br />
&lt;img src="images/image5.jpg" alt="image 5" /&gt;</code><br />
Notice how the third image now has a class linked to it.</p>
<p>Now for the CSS:<br />
<code>.my_class{ border:5px solid red; }</code></p>
<p>Now, if you view that code in your browser the middle image will have the red border.</p>
<h2>CSS IDs</h2>
<p>Very similarly you can use <strong>CSS IDs</strong> to reference a <em>SINGLE</em> unqiue HTML element.  This is good for things like website logos, footers and layout elements.  Specific elements that you know will only ever appear once on the page.</p>
<p>Look at the following example of a page layout:<br />
<code>&lt;div class="container"&gt;<br />
&nbsp;&nbsp;Some content here<br />
&nbsp;&nbsp;&lt;div class="footer"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;div class="copyright"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CSS Master<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/div&gt;<br />
&nbsp;&nbsp;&lt;/div&gt;<br />
&lt;/div&gt;</code><br />
And the CSS for this&#8230;<br />
<code>.container {<br />
&nbsp;&nbsp;margin:0 auto;<br />
&nbsp;&nbsp;width:900px;<br />
}<br />
.footer{<br />
&nbsp;&nbsp;background-color:#333;<br />
}<br />
.copyright{<br />
&nbsp;&nbsp;font-size:11px;<br />
&nbsp;&nbsp;color:white;<br />
}</code></p>
<p>My page will only ever have one footer, and it will only ever have one copyright notice.  It may, however, have multiple containers.  So, let&#8217;s revise the HTML.<br />
<code>&lt;div class="container"&gt;<br />
&nbsp;&nbsp;Some content here<br />
&nbsp;&nbsp;&lt;div id="footer"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;div id="copyright"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CSS Master<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/div&gt;<br />
&nbsp;&nbsp;&lt;/div&gt;<br />
&lt;/div&gt;</code></p>
<p>You reference the elements in the CSS in a very similar way, but you use a hash (#) instead of a period (.) for the ID name.<br />
<code>.container {<br />
&nbsp;&nbsp;margin:0 auto;<br />
&nbsp;&nbsp;width:900px;<br />
}<br />
#footer{<br />
&nbsp;&nbsp;background-color:#333;<br />
}<br />
#copyright{<br />
&nbsp;&nbsp;font-size:11px;<br />
&nbsp;&nbsp;color:white;<br />
}</code></p>
<p>Note how our container is still a class (.container) but everything else is using a hash # so must be an ID.</p>
<h2>Why Not Just Use Classes?</h2>
<p>ID&#8217;s speed up performance.  The browser will know it only needs to style the one element with that specific ID the once, so as soon as it has been loaded the CSS can be cast away.  If you use classes for everything then your browser will be using up more memory and resources than it needs to.</p>
<p>Using ID&#8217;s instead of classes also makes it easier to work with.  If you&#8217;re trawling through a CSS file with 1,000 lines in there, you can quickly see which are generic styles, classes and IDs.  You know that the generic styles will be looking after your general elements, your ID&#8217;s are looking after your layout and static elements and your classes </p>
<h2>Notes</h2>
<p>Be careful with your reference names.  <em>DO NOT</em> start a class or ID with a number as a lot of browsers can&#8217;t read them properly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cssmaster.co.uk/css-classes-and-css-ids/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

