<?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>vijayjoshi.org &#187; Css</title>
	<atom:link href="http://www.vijayjoshi.org/tag/css/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.vijayjoshi.org</link>
	<description>php &#124; javascript &#124; ajax &#124; and all things web</description>
	<lastBuildDate>Sun, 20 Nov 2011 15:24:32 +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>Setting style/css class of html elements from javascript</title>
		<link>http://www.vijayjoshi.org/2009/01/24/setting-stylecss-class-of-html-elements-from-javascript/</link>
		<comments>http://www.vijayjoshi.org/2009/01/24/setting-stylecss-class-of-html-elements-from-javascript/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 08:31:09 +0000</pubDate>
		<dc:creator>Vijay Joshi</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Css]]></category>

		<guid isPermaLink="false">http://www.vijayjoshi.org/2009/01/24/setting-stylecss-class-of-html-elements-from-javascript/</guid>
		<description><![CDATA[
			
				
			
		
Style of an html element can be set in 2 ways.

Specify a css class name for the element.
Set the style inline for the element.

//Method 1:
&#60;p class="boldText">Some text&#60;/p>
//Method 2:
&#60;p style="font-weight:bold;">Some text&#60;/p>

Therefore using these 2 attributes we can change the style of any html element in the page. The only thing we need to do is get hold of these attributes in ...]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F01%2F24%2Fsetting-stylecss-class-of-html-elements-from-javascript%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F01%2F24%2Fsetting-stylecss-class-of-html-elements-from-javascript%2F&amp;source=v08i&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Style of an html element can be set in 2 ways.</p>
<ol>
<li>Specify a css class name for the element.</li>
<li>Set the style inline for the element.</li>
</ol>
<pre class="brush:js">//Method 1:
&lt;p class="boldText">Some text&lt;/p>
//Method 2:
&lt;p style="font-weight:bold;">Some text&lt;/p>
</pre>
<p>Therefore using these 2 attributes we can change the style of any html element in the page. The only thing we need to do is get hold of these attributes in JavaScript and manipulate them.Fortunately accessing a DOM element in JavaScript gives us access to all its attributes. What we get is a DOM object which has the attributes of the element as its properties. So we can perform the same operations we do upon objects.</p>
<p>Changing the css class of an element is quite easy.</p>
<p>Suppose you have a p element on the page and a css defined in your css file</p>
<pre class="brush:js">&lt;p> This is some text&lt;/p>;</pre>
<pre class="brush:css">.redText { color : red; }</pre>
<p>To change or set the class name for this p</p>
<pre class="brush:js">
var x = document.getElementById('myPara');
x.className = "redText";
</pre>
<p>and you are done.</p>
<p>className property changes the class of the specified element. You can specify more then one class, just remember to separate them with a whitespace.</p>
<pre class="brush:js">
x.className = "redText noBorder";
</pre>
<p>The other method is setting the inline style for the element. <span id="more-259"></span>When using the style property we specify the styles as key value pairs.</p>
<pre lang="html">
style="margin-left :10px; display:inline; float:left;"
</pre>
<p>The style attribute and all of its properties are also available in JavaScript. Style can be set as follows:</p>
<pre class="brush:js">
element.style.propertyName = value;
</pre>
<p>But there is a catch here. We cannot do</p>
<pre class="brush:js">
element.style.margin-left = "red";
</pre>
<p>as JavaScript would interpret the hyphen as subtraction operator and it will attempt</p>
<pre class="brush:js">
(element.style.margin) - left = "10px";
</pre>
<p>which is not what we expect from it. There is a special pattern while mapping these html properties to javascript. Properties which do not have a hyphen remain the same while those with a hyphen are converted to lower camel case and the hyphen is removed , which means display will remain display but margin-left  will become marginLeft .</p>
<p>So the correct synatx would be</p>
<pre class="brush:js">
element.style.marginLeft = "10px";
</pre>
<p>However there is one exception to the above rule. float property becomes cssFloat as float is a keyword.</p>
<pre class="brush:js">
element.style.float = "left"; // incorrect
element.style.cssFloat = "left"; // correct
</pre>
<p>Some common properties and their javascript counterparts are as follows</p>
<table style="width: 400px;">
<tbody>
<tr>
<td style="text-align: left; width: 200px;font-weight:bold;">Css Property</td>
<td style="font-weight:bold;">JavaScript Equivalent</td>
</tr>
<tr>
<td style="text-align: left; width: 200px;"> background</td>
<td>background </td>
</tr>
<tr>
<td style="text-align: left; width: 200px;">background-color </td>
<td> backgroundColor </td>
</tr>
<tr>
<td style="text-align: left; width: 200px;">background-image </td>
<td> backgroundImage </td>
</tr>
<tr>
<td style="text-align: left; width: 200px;"> border</td>
<td>border </td>
</tr>
<tr>
<td style="text-align: left; width: 200px;"> clear</td>
<td> clear</td>
</tr>
<tr>
<td style="text-align: left; width: 200px;"> color</td>
<td> color</td>
</tr>
<tr>
<td style="text-align: left; width: 200px;"> display</td>
<td>display </td>
</tr>
<tr>
<td style="text-align: left; width: 200px;"> float</td>
<td> cssFloat</td>
</tr>
<tr>
<td style="text-align: left; width: 200px;"> Font-family</td>
<td> fontFamily</td>
</tr>
<tr>
<td style="text-align: left; width: 200px;"> Font-size</td>
<td> fontSize</td>
</tr>
<tr>
<td style="text-align: left; width: 200px;"> Font-weight</td>
<td> fontWeight</td>
</tr>
<tr>
<td style="text-align: left; width: 200px;"> height</td>
<td> height</td>
</tr>
<tr>
<td style="text-align: left; width: 200px;"> Left</td>
<td> left</td>
</tr>
<tr>
<td style="text-align: left; width: 200px;"> list-style</td>
<td> listStyle</td>
</tr>
<tr>
<td style="text-align: left; width: 200px;"> list-style-image</td>
<td> listStyleImage</td>
</tr>
<tr>
<td style="text-align: left; width: 200px;"> margin</td>
<td> margin</td>
</tr>
<tr>
<td style="text-align: left; width: 200px;"> Text-decoration</td>
<td> textDecoration</td>
</tr>
<tr>
<td style="text-align: left; width: 200px;"> vertical-align</td>
<td> verticalAlign</td>
</tr>
<tr>
<td style="text-align: left; width: 200px;"> Visibility</td>
<td> visibility</td>
</tr>
<tr>
<td style="text-align: left; width: 200px;"> Width</td>
<td> width</td>
</tr>
<tr>
<td style="text-align: left; width: 200px;"> z-index</td>
<td> zIndex</td>
</tr>
</tbody>
</table>
<p>Give this page a Thumbsup on Stumbleupon if you liked this.</p>
<div class="shr-publisher-259"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:right;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.vijayjoshi.org%2F2009%2F01%2F24%2Fsetting-stylecss-class-of-html-elements-from-javascript%2F' data-shr_title='Setting+style%2Fcss+class+of+html+elements+from+javascript'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://www.vijayjoshi.org/2009/01/24/setting-stylecss-class-of-html-elements-from-javascript/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

