<?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>ElbertF &#187; css</title>
	<atom:link href="http://elbertf.com/tag/css/feed/" rel="self" type="application/rss+xml" />
	<link>http://elbertf.com</link>
	<description>Insights and Updates from a Tech Geek</description>
	<lastBuildDate>Sun, 15 Apr 2012 21:46:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Object Oriented CSS</title>
		<link>http://elbertf.com/2009/05/object-oriented-css/</link>
		<comments>http://elbertf.com/2009/05/object-oriented-css/#comments</comments>
		<pubDate>Sat, 16 May 2009 03:56:20 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[oop]]></category>

		<guid isPermaLink="false">http://ElbertF.com/?p=186</guid>
		<description><![CDATA[Okay, there is no such thing as object oriented CSS. But there are advantages in OO programming that we can use in&#160;CSS. 1 2 3 4 5 6 7 p &#123; margin: 0; &#125; &#160; #content p &#123; margin: 1em 0; &#125; In this example p would be a class and #content p would be [...]]]></description>
			<content:encoded><![CDATA[<p>Okay, there is no such thing as object oriented CSS. But there are advantages in <a href="http://en.wikipedia.org/wiki/Object-oriented">OO programming</a> that we can use in&nbsp;CSS.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">p <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#content</span> p <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1em</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>In this example <code>p</code> would be a <em>class</em> and <code>#content p</code> would be an <em>instance</em> of that class. Let&#8217;s say the margin set in <code>p</code> is a <em>method</em> and we got the basic concepts of&nbsp;OOP.</p>
<p><span id="more-186"></span></p>
<h4>Practicle&nbsp;example</h4>
<p>A single HTML element can serve different purposes on a page. A list element for example can be used as a navigational menu (styled horizontally) and as a regular list in content (styled vertically). Sometimes it&#8217;s difficult to change one without changing the&nbsp;other.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">li <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">list-style</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span> <span style="color: #993333;">inside</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#menu</span> li <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#content</span> li <span style="color: #00AA00;">&#123;</span>
    <span style="color: #808080; font-style: italic;">/* We only want bullets if a list appears in content */</span>
    <span style="color: #000000; font-weight: bold;">list-style</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">outside</span> <span style="color: #993333;">disc</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Here I created two <em>instances</em> of the <code>li</code> <em>object</em>. If you change either one it won&#8217;t effect the&nbsp;other.</p>
<h4>CSS&nbsp;Reset</h4>
<p>It&#8217;s useful to reset all style properties first using a CSS reset. In the above example I simply applied <code>list-style: none inside;</code> to the <code>li</code> element so you won&#8217;t see bullets (&bull;) unless you specify it. It&#8217;s easier to just reset everything at once, like&nbsp;this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #00AA00;">*</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">transparent</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1em</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">inherit</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> <span style="color: #ff0000;">'Trebuchet MS'</span><span style="color: #00AA00;">,</span> Verdana<span style="color: #00AA00;">,</span> Arial<span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1.4em</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">list-style</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span> <span style="color: #993333;">inside</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">inherit</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Adding this code on top of your CSS file will give you full control over your&nbsp;code.</p>
<p>As a bonus this will probably solve a lot of your cross-browser compatibility problems. This is because every browser applies <a href="http://lists.w3.org/Archives/Public/www-style/2008Jul/att-0124/defaultstyles.htm">different default CSS</a> to&nbsp;elements.</p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2009/05/object-oriented-css/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

