<?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>Cool Javascripts &#187; Tutorials</title>
	<atom:link href="http://cool-javascripts.com/category/tutorials/feed" rel="self" type="application/rss+xml" />
	<link>http://cool-javascripts.com</link>
	<description>The best javascripts for your site</description>
	<lastBuildDate>Tue, 22 Mar 2011 14:35:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Building a reusabe font size controller interface using jQuery</title>
		<link>http://cool-javascripts.com/jquery/building-a-reusabe-font-size-controller-interface-using-jquery.html</link>
		<comments>http://cool-javascripts.com/jquery/building-a-reusabe-font-size-controller-interface-using-jquery.html#comments</comments>
		<pubDate>Wed, 14 Jan 2009 13:57:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[change font size]]></category>
		<category><![CDATA[font]]></category>
		<category><![CDATA[font size controller]]></category>

		<guid isPermaLink="false">http://cool-javascripts.com/jquery/building-a-reusabe-font-size-controller-interface-using-jquery.html</guid>
		<description><![CDATA[Most of the web2.0 design allows the user to change the font size of the main content area. This is indeed a good usability. So I thought, I should contribute something here. The result is a small jQuery code which can be used to generate a font size changer interface. Only thing you need to [...]


Related posts:<ol><li><a href='http://cool-javascripts.com/jquery/vertical-alignment-of-contents-inside-an-element-using-jquery.html' rel='bookmark' title='Permanent Link: Vertical alignment of contents inside an element using jQuery'>Vertical alignment of contents inside an element using jQuery</a> <small>One of the old problems! Vertical alignment of contents inside...</small></li><li><a href='http://cool-javascripts.com/jquery/add-icons-to-your-links-automatically-using-jquery-css.html' rel='bookmark' title='Permanent Link: Add icons to your links automatically using jQuery &#038; CSS'>Add icons to your links automatically using jQuery &#038; CSS</a> <small>You want to show a particular icon to a particular...</small></li><li><a href='http://cool-javascripts.com/effects/make-curved-corners-with-jquery-corner-plugin.html' rel='bookmark' title='Permanent Link: Make curved corners with jQuery Corner plugin'>Make curved corners with jQuery Corner plugin</a> <small>If you want a curved edge for your element, the...</small></li></ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Most of the web2.0 design allows the user to change the font size of the main content area. This is indeed a good usability. So I thought, I should contribute something here. The result is a small jQuery code which can be used to generate a font size changer interface. </p>
<p><img src="http://cool-javascripts.com/wp-content/uploads/2009/01/fontsize-controller.jpg" /></p>
<p>Only thing you need to make is a holder element for the controller and then call the function like</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">fontSize<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#holder&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;#content&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">9</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">12</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #009900;">&#41;</span>;
<span style="color: #006600; font-style: italic;">// holder is the id of the element where the font size controller interface will be shown</span>
<span style="color: #006600; font-style: italic;">// content is the id of the element where the font size changes will take place.</span>
<span style="color: #006600; font-style: italic;">// The next three arguments are Minimum font size, Default font size and Maximum font size respectively</span></pre></div></div>

<p>Yes, all done and it will automatically generate all required buttons. It also supports jquery cookie plugin. So when you open the page again, the preferred font size will be restored automatically. (Cookie plugin is not required, but recommended)</p>
<p>After the function is called, the controllers will be generated in the specified area like the following.</p>
<p><img src="http://cool-javascripts.com/wp-content/uploads/2009/01/jquery-automatic-font-size-controller-cool-javascriptscom-ultimate-javascript-resources.jpg" /></p>
<h3>Advantages</h3>
<ul>
<li>Small size, built on jQuery</li>
<li>Fully skinnable using css</li>
<li>Multiple interface can be used in the same page for two different content areas</li>
<li>Support jQuery Cookie plugin for remembering the preferred font size</li>
<li>Very easy to use</li>
</ul>
<p><span id="more-127"></span></p>
<h3>The logic</h3>
<ul>
<li>The function will be called with the holder element and target element with default, maximum and minimum font size</li>
<li>Create three elements for decreasing, default and increasing font size </li>
<li>Style the elements using CSS so that it will be fully skinnable</li>
<li>Upon clicking read the current font size for the target and calculate the new font size. Then apply it to target element</li>
<li>Save the modified font size value to cookie, so that the preferred font size can be saved and restored</li>
</ul>
<h3>The CSS</h3>
<p>We need some buttons for the backgrounds and the best places to search is <a href="http://www.iconlook.com" target="_blank">Icon look</a>, <a href="http://www.iconfinder.net" target="_blank">Icon Finder</a> and <a href="http://www.iconlet.com" target="_blank">Icon Let</a> as usual. I found the above images, but you can use another or design yours.</p>
<p>Now we need to style the elements. Mainly we need 5 styles &#8211; Button for smaller font, default font and larger font. We need two more styles for the disabled state of buttons. (If current font size is the minimum or maximum allowed, we need to show a disabled state).</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">&nbsp;
&nbsp;
<span style="color: #6666ff;">.smallFont</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">outline</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span>; <span style="color: #808080; font-style: italic;">/*for making it look nice in FF */</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">images/font-down.png</span><span style="color: #00AA00;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">text-indent</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-9999px</span>;
	<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</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: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">32px</span>;
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">32px</span>;
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.defaultFont</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #808080; font-style: italic;">/*if you want to hide the default font button, use display:none */</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">images/font-default.png</span><span style="color: #00AA00;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">text-indent</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-9999px</span>;
	<span style="color: #000000; font-weight: bold;">outline</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span>;
	<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</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: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">32px</span>;
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">32px</span>;
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.largeFont</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">images/font-up.png</span><span style="color: #00AA00;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">outline</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span>;
	<span style="color: #000000; font-weight: bold;">text-indent</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-9999px</span>;
	<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</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: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">32px</span>;
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">32px</span>;
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.ldisabled</span><span style="color: #00AA00;">,</span><span style="color: #6666ff;">.sdisabled</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #808080; font-style: italic;">/*Styles for disabled buttons*/</span>
	opacity<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0.3</span>;
	-ms-filter<span style="color: #00AA00;">:</span> <span style="color: #ff0000;">&quot;progid:DXImageTransform.Microsoft.Alpha(Opacity=30)&quot;</span>;
	<span style="color: #808080; font-style: italic;">/*This is for IE8 */</span>
	filter<span style="color: #00AA00;">:</span> alpha<span style="color: #00AA00;">&#40;</span>opacity <span style="color: #00AA00;">=</span> <span style="color: #cc66cc;">30</span><span style="color: #00AA00;">&#41;</span>;
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>As you can see I&#8217;ve used three background images for the three buttons. Need to set the display:block property as the buttons are of 32px height. I could have optimized the css more by applying common styles to elements. For eg: For the three styles only background is different, so it could&#8217;ve declared like the following.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"> <span style="color: #6666ff;">.smallFont</span><span style="color: #00AA00;">,</span> <span style="color: #6666ff;">.defaultFont</span><span style="color: #00AA00;">,</span> <span style="color: #6666ff;">.largeFont</span> <span style="color: #00AA00;">&#123;</span> 
.... <span style="color: #808080; font-style: italic;">/*common properties*/</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.smallFont</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>...<span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&#125;</span> 
...</pre></div></div>

<p>But I didn&#8217;t use that because it will be easier to declare as is now, if you want to use different sized images or different styles for each button. </p>
<p class="note">If you want to have multiple styles in the same page, just use the CSS scope</p>
<p>For eg:</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;"> <span style="color: #cc00cc;">#container1</span> <span style="color: #6666ff;">.smallFont</span> <span style="color: #00AA00;">&#123;</span> ... <span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#container2</span> <span style="color: #6666ff;">.smallFont</span> <span style="color: #00AA00;">&#123;</span> ... <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Now for the disabled state I used same style for all buttons. Using opacity filter is the easiest way to imitate a disabled button. Here for IE8, I have used an extra style after googling around.</p>
<h3>The jQuery function</h3>
<p>I didn&#8217;t write it as a jQuery plugin because I didn&#8217;t find it useful to have the chainable functionality.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> fontSize<span style="color: #009900;">&#40;</span>container<span style="color: #339933;">,</span> target<span style="color: #339933;">,</span> minSize<span style="color: #339933;">,</span> defSize<span style="color: #339933;">,</span> maxSize<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #009966; font-style: italic;">/*Editable settings*/</span>
	<span style="color: #003366; font-weight: bold;">var</span> minCaption <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Make font size smaller&quot;</span>; <span style="color: #006600; font-style: italic;">//title for smallFont button</span>
	<span style="color: #003366; font-weight: bold;">var</span> defCaption <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Make font size default&quot;</span>; <span style="color: #006600; font-style: italic;">//title for defaultFont button</span>
	<span style="color: #003366; font-weight: bold;">var</span> maxCaption <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Make font size larger&quot;</span>; <span style="color: #006600; font-style: italic;">//title for largefont button</span>
&nbsp;
&nbsp;
	<span style="color: #006600; font-style: italic;">//Now we'll add the font size changer interface in container</span>
	smallFontHtml <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&lt;a href='javascript:void(0);' class='smallFont' title='&quot;</span> <span style="color: #339933;">+</span> minCaption <span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;'&gt;&quot;</span> <span style="color: #339933;">+</span> minCaption <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/a&gt; &quot;</span>;
	defFontHtml <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&lt;a href='javascript:void(0);' class='defaultFont' title='&quot;</span> <span style="color: #339933;">+</span> defCaption <span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;'&gt;&quot;</span> <span style="color: #339933;">+</span> defCaption <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/a&gt; &quot;</span>;
	largeFontHtml <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&lt;a href='javascript:void(0);' class='largeFont' title='&quot;</span> <span style="color: #339933;">+</span> maxCaption <span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;'&gt;&quot;</span> <span style="color: #339933;">+</span> maxCaption <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/a&gt; &quot;</span>;
	$<span style="color: #009900;">&#40;</span>container<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>smallFontHtml <span style="color: #339933;">+</span> defFontHtml <span style="color: #339933;">+</span> largeFontHtml<span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #006600; font-style: italic;">//Read cookie &amp;amp; sets the fontsize</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">cookie</span> <span style="color: #339933;">!=</span> undefined<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> cookie <span style="color: #339933;">=</span> target.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[#. ]/g</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #003366; font-weight: bold;">var</span> value <span style="color: #339933;">=</span> $.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#40;</span>cookie<span style="color: #009900;">&#41;</span>;
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>value <span style="color: #339933;">!=</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'font-size'</span><span style="color: #339933;">,</span> parseInt<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">//on clicking small font button, font size is decreased by 1px</span>
	$<span style="color: #009900;">&#40;</span>container <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; .smallFont&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
		curSize <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;font-size&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
		newSize <span style="color: #339933;">=</span> curSize <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span>;
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>newSize <span style="color: #339933;">&gt;=</span> minSize<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'font-size'</span><span style="color: #339933;">,</span> newSize<span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span> 
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>newSize <span style="color: #339933;">&lt;=</span> minSize<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span>container <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; .smallFont&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;sdisabled&quot;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>newSize <span style="color: #339933;">&lt;</span> maxSize<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span>container <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; .largeFont&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ldisabled&quot;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
		updatefontCookie<span style="color: #009900;">&#40;</span>target<span style="color: #339933;">,</span> newSize<span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">//sets the cookie </span>
&nbsp;
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #006600; font-style: italic;">//on clicking default font size button, font size is reset</span>
	$<span style="color: #009900;">&#40;</span>container <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; .defaultFont&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'font-size'</span><span style="color: #339933;">,</span> defSize<span style="color: #009900;">&#41;</span>;
		$<span style="color: #009900;">&#40;</span>container <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; .smallFont&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;sdisabled&quot;</span><span style="color: #009900;">&#41;</span>;
		$<span style="color: #009900;">&#40;</span>container <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; .largeFont&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ldisabled&quot;</span><span style="color: #009900;">&#41;</span>;
		updatefontCookie<span style="color: #009900;">&#40;</span>target<span style="color: #339933;">,</span> defSize<span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #006600; font-style: italic;">//on clicking large font size button, font size is incremented by 1 to the maximum limit</span>
	$<span style="color: #009900;">&#40;</span>container <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; .largeFont&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		curSize <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;font-size&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
		newSize <span style="color: #339933;">=</span> curSize <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span>;
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>newSize <span style="color: #339933;">&lt;=</span> maxSize<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'font-size'</span><span style="color: #339933;">,</span> newSize<span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span> 
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>newSize <span style="color: #339933;">&gt;</span> minSize<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span>container <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; .smallFont&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;sdisabled&quot;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>newSize <span style="color: #339933;">&gt;=</span> maxSize<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span>container <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; .largeFont&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ldisabled&quot;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
		updatefontCookie<span style="color: #009900;">&#40;</span>target<span style="color: #339933;">,</span> newSize<span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #003366; font-weight: bold;">function</span> updatefontCookie<span style="color: #009900;">&#40;</span>target<span style="color: #339933;">,</span> size<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">//Private function for setting cookie</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">cookie</span> <span style="color: #339933;">!=</span> undefined<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">//If cookie plugin available, set a cookie</span>
			<span style="color: #003366; font-weight: bold;">var</span> cookie <span style="color: #339933;">=</span> target.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[#. ]/g</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>;
			$.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#40;</span>cookie<span style="color: #339933;">,</span> size<span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span> 
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I think the code is self explanatory. If you&#8217;ve any doubt in code, please feel free to ask here <img src='http://cool-javascripts.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Usage</h3>
<p>Include the css file, then after including jQuery include this script like the following (Don&#8217;t forget to change the path as required. Also, you need to copy the images in correct folder as specified in CSS file or edit the CSS file to make it correct.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;font-controller.css&quot; /&gt;
&lt;script src=&quot;jquery-1.2.6.pack.js&quot;&gt;&lt;/script&gt; 
&lt;script src=&quot;font-controller.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;jquery.cookie.js&quot;&gt;&lt;/script&gt;</pre></div></div>

<p>The function can be called inside a $document.ready() as usual. For eg:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">fontSize<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#container&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;#content&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">9</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">12</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Then inside your html create the empty container for holding the elements. Give it an ID or class, if possible ID.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;container&quot;&gt;&lt;/div&gt; ... 
&lt;div id=&quot;content&quot;&gt;... Font size of this element can be controlled.... &lt;/div&gt;</pre></div></div>

<p class="note">Do not copy paste the code from this page. Instead download the full demo from below link</p>
<h3>Demo</h3>
<p class="download">
<a class="opensame" rel="shadowbox" href="http://demos.cool-javascripts.com/jquery-fontsize-controller.html">View Demo</a> | <a class="opennew" href="http://demos.cool-javascripts.com/jquery-fontsize-controller.zip" target="_blank">Download full demo</a>
</p>
<p>Please feel free to post your comments and suggestions for improvement <img src='http://cool-javascripts.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>


<p>Related posts:<ol><li><a href='http://cool-javascripts.com/jquery/vertical-alignment-of-contents-inside-an-element-using-jquery.html' rel='bookmark' title='Permanent Link: Vertical alignment of contents inside an element using jQuery'>Vertical alignment of contents inside an element using jQuery</a> <small>One of the old problems! Vertical alignment of contents inside...</small></li><li><a href='http://cool-javascripts.com/jquery/add-icons-to-your-links-automatically-using-jquery-css.html' rel='bookmark' title='Permanent Link: Add icons to your links automatically using jQuery &#038; CSS'>Add icons to your links automatically using jQuery &#038; CSS</a> <small>You want to show a particular icon to a particular...</small></li><li><a href='http://cool-javascripts.com/effects/make-curved-corners-with-jquery-corner-plugin.html' rel='bookmark' title='Permanent Link: Make curved corners with jQuery Corner plugin'>Make curved corners with jQuery Corner plugin</a> <small>If you want a curved edge for your element, the...</small></li></ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://cool-javascripts.com/jquery/building-a-reusabe-font-size-controller-interface-using-jquery.html/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Add icons to your links automatically using jQuery &amp; CSS</title>
		<link>http://cool-javascripts.com/jquery/add-icons-to-your-links-automatically-using-jquery-css.html</link>
		<comments>http://cool-javascripts.com/jquery/add-icons-to-your-links-automatically-using-jquery-css.html#comments</comments>
		<pubDate>Mon, 12 Jan 2009 09:54:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[dynamic icons]]></category>
		<category><![CDATA[link dressup]]></category>
		<category><![CDATA[link styles]]></category>

		<guid isPermaLink="false">http://cool-javascripts.com/jquery/add-icons-to-your-links-automatically-using-jquery-css.html</guid>
		<description><![CDATA[You want to show a particular icon to a particular type of link. For eg: a pdf icon to all pdf file links, a text document icon to all text document links, zip icon to all links that are linked to zip files etc. Why jQuery? Yes, it is not that difficult with css. Just [...]


Related posts:<ol><li><a href='http://cool-javascripts.com/jquery/vertical-alignment-of-contents-inside-an-element-using-jquery.html' rel='bookmark' title='Permanent Link: Vertical alignment of contents inside an element using jQuery'>Vertical alignment of contents inside an element using jQuery</a> <small>One of the old problems! Vertical alignment of contents inside...</small></li><li><a href='http://cool-javascripts.com/jquery/building-a-reusabe-font-size-controller-interface-using-jquery.html' rel='bookmark' title='Permanent Link: Building a reusabe font size controller interface using jQuery'>Building a reusabe font size controller interface using jQuery</a> <small>Most of the web2.0 design allows the user to change...</small></li><li><a href='http://cool-javascripts.com/effects/make-curved-corners-with-jquery-corner-plugin.html' rel='bookmark' title='Permanent Link: Make curved corners with jQuery Corner plugin'>Make curved corners with jQuery Corner plugin</a> <small>If you want a curved edge for your element, the...</small></li></ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>You want to show a particular icon to a particular type of link. For eg: a pdf icon to all pdf file links, a text document icon to all text document links, zip icon to all links that are linked to zip files etc.</p>
<p><img src="http://cool-javascripts.com/wp-content/uploads/2009/01/dynamic-icons.jpg" /></p>
<p><b>Why jQuery?<br />
</b><br />
Yes, it is not that difficult with css. Just declare some classes and give background images then assign that class to the links as required. Then why we need jQuery. The answer is, for simplicity. Who ever entering the content should be cautious to add the right classes for the right links. Also adding classes are difficult in a blog platform &#8211; you will need to switch to HTML view instead of the editor view.</p>
<p>Using jQuery we&#8217;ll make sure that the right icon is showing for a particular link. <span id="more-99"></span></p>
<p><b>Step 1 &#8211; Find the right icons</b></p>
<p>Getting a free icon describing your file type is easy. You might need to search for free icon sets, or better search in any of the three following icon search engines. For eg: search for the term &#8220;pdf&#8221; and you will see a lot of images in different dimensions, select the one that suits your need. Just make sure you&#8217;re not using a copyrighted icon <img src='http://cool-javascripts.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
<ol>
<li><a rel="shadowbox" href="http://www.iconlook.com" rel="shadowbox">Icon Look &#8211; http://www.iconlook.com</a></li>
<li><a rel="shadowbox" href="http://www.iconlet.com">Icon Let &#8211; http://www.iconlet.com</a></li>
<li><a href="http://www.iconfinder.net">Icon Finder &#8211; http:www.iconfinder.net</a></li>
</ol>
<p>Here I am using five icons &#8211; One for pdf files, one for txt documents, one for zip files, one for email links and one for external links. You may add any number of icons for any file type as I am going to describe in the next sections.</p>
<p class="note">I&#8217;ve selected icons of 16px in size because it look better in content. If you need large icons, you will need to use the css display:block property. </p>
<p><b><br />
CSS styles<br />
</b><br />
Now we&#8217;ve have the right icons. Now we need to add classes with background image.</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">a<span style="color: #6666ff;">.pdf</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #808080; font-style: italic;">/*The background image*/</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">images/pdf.png</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #993333;">center</span>;
	<span style="color: #000000; font-weight: bold;">padding-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span>;
	<span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">16px</span>; <span style="color: #808080; font-style: italic;">/* To center the text vertically with the icon */</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
a<span style="color: #6666ff;">.txt</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #808080; font-style: italic;">/*The background image*/</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">images/txt.png</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #993333;">center</span>;
	<span style="color: #000000; font-weight: bold;">padding-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span>;
	<span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">16px</span>;
<span style="color: #00AA00;">&#125;</span>
&nbsp;
a<span style="color: #6666ff;">.zip</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #808080; font-style: italic;">/*The background image*/</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">images/zip.png</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #993333;">center</span>;
	<span style="color: #000000; font-weight: bold;">padding-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span>;
	<span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">16px</span>;
<span style="color: #00AA00;">&#125;</span>
&nbsp;
a<span style="color: #6666ff;">.email</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">images/email.png</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #993333;">center</span>;
	<span style="color: #000000; font-weight: bold;">padding-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span>;
	<span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">16px</span>;
<span style="color: #00AA00;">&#125;</span>
&nbsp;
a<span style="color: #6666ff;">.external</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">images/ext_link.png</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #993333;">center</span>;
	<span style="color: #000000; font-weight: bold;">padding-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span>;
	<span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">16px</span>;
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p class="note" if you want to use large icons, you need to turn the anchor element display style to block. Otherwise, the images will be hidden at the edges.</p>
<p>Eg:</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">&nbsp;
<span style="color: #6666ff;">.download</span> a<span style="color: #6666ff;">.zip</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #808080; font-style: italic;">/*The background image*/</span>
	<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">images/zip.png</span><span style="color: #00AA00;">&#41;</span> <span style="color: #993333;">no-repeat</span> <span style="color: #000000; font-weight: bold;">left</span> <span style="color: #993333;">center</span>;
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">48px</span>;
	<span style="color: #000000; font-weight: bold;">padding-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">55px</span>;
	<span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">48px</span>; <span style="color: #808080; font-style: italic;">/* Center the text vertically with image */</span>
	<span style="color: #000000; font-weight: bold;">vertical-align</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">bottom</span>; <span style="color: #808080; font-style: italic;">/* to align the text with image bottom, line height property required */</span>
	<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span>; <span style="color: #808080; font-style: italic;">/* Need this to show the images fully */</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: #808080; font-style: italic;">/* You might need this as well for aligning it with the parent element */</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p><strong>JQuery</strong> </p>
<p>Now, we can write the jQuery statements.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
&nbsp;
	<span style="color: #006600; font-style: italic;">// Add pdf icons to pdf links</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a[href$='.pdf']&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;pdf&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #006600; font-style: italic;">// Add txt icons to document links (doc, rtf, txt)</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a[href$='.doc'], a[href$='.txt'], a[href$='.rft']&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;txt&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #006600; font-style: italic;">// Add zip icons to Zip file links (zip, rar)</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a[href$='.zip'], a[href$='.rar']&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;zip&quot;</span><span style="color: #009900;">&#41;</span>; 
&nbsp;
	<span style="color: #006600; font-style: italic;">// Add email icons to email links</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a[href^='mailto:']&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;email&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #006600; font-style: italic;">//Add external link icon to external links - </span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">//Compare the anchor tag's host name with location's host name</span>
	    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hostname</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hostname</span> <span style="color: #339933;">!==</span> location.<span style="color: #660066;">hostname</span>;
	  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;external&quot;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #006600; font-style: italic;">//You might also want to set the _target attribute to blank</span>
	<span style="color: #006600; font-style: italic;">/*
	$('a').filter(function() {
		//Compare the anchor tag's host name with location's host name
	    return this.hostname &amp;&amp; this.hostname !== location.hostname;
	  }).addClass(&quot;external&quot;).attr(&quot;target&quot;, &quot;_blank&quot;);
	*/</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p class="note">For determining the links, we&#8217;re using jQuery&#8217;s attribute syntax. For eg: To identify PDF links, we check whether the href attribute ends with a &#8216;.pdf&#8217; extension. </p>
<p>To find external links we&#8217;ll compare the anchor tag&#8217;s host name with the location host name </p>
<p class="wanrning">Do not copy paste from the above code. It is formatted for viewig. Please download the full source code at the end </p>
<p><strong>demo</strong><br />
It will look like the below image. For an actual demo check the below links.</p>
<p><img src="http://cool-javascripts.com/wp-content/uploads/2009/01/demo-links.jpg" /></p>
<p class="download"><strong>Demo &amp; Download</strong><br />
<a class="opensame" rel="shadowbox" href="http://www.demos.cool-javascripts.com/jquery-icons-to-links.html">Demo</a><br />
<a class="opennew" href="http://demos.cool-javascripts.com/jquery-icons-to-links.zip">Download full examples (24 KB) </a></p>


<p>Related posts:<ol><li><a href='http://cool-javascripts.com/jquery/vertical-alignment-of-contents-inside-an-element-using-jquery.html' rel='bookmark' title='Permanent Link: Vertical alignment of contents inside an element using jQuery'>Vertical alignment of contents inside an element using jQuery</a> <small>One of the old problems! Vertical alignment of contents inside...</small></li><li><a href='http://cool-javascripts.com/jquery/building-a-reusabe-font-size-controller-interface-using-jquery.html' rel='bookmark' title='Permanent Link: Building a reusabe font size controller interface using jQuery'>Building a reusabe font size controller interface using jQuery</a> <small>Most of the web2.0 design allows the user to change...</small></li><li><a href='http://cool-javascripts.com/effects/make-curved-corners-with-jquery-corner-plugin.html' rel='bookmark' title='Permanent Link: Make curved corners with jQuery Corner plugin'>Make curved corners with jQuery Corner plugin</a> <small>If you want a curved edge for your element, the...</small></li></ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://cool-javascripts.com/jquery/add-icons-to-your-links-automatically-using-jquery-css.html/feed</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Vertical alignment of contents inside an element using jQuery</title>
		<link>http://cool-javascripts.com/jquery/vertical-alignment-of-contents-inside-an-element-using-jquery.html</link>
		<comments>http://cool-javascripts.com/jquery/vertical-alignment-of-contents-inside-an-element-using-jquery.html#comments</comments>
		<pubDate>Thu, 08 Jan 2009 20:34:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[vertical align]]></category>
		<category><![CDATA[vertical alignment]]></category>

		<guid isPermaLink="false">http://cool-javascripts.com/jquery/vertical-alignment-of-contents-inside-an-element-using-jquery.html</guid>
		<description><![CDATA[One of the old problems! Vertical alignment of contents inside an element. For those who don&#8217;t know about this issue, What is the issue? For eg; there is an area (e.g. &#60;div&#62; or &#60;p&#62;) with known height in the page (For eg: 300px;) an internal object (typically a paragraph, image etc) is inside the area [...]


Related posts:<ol><li><a href='http://cool-javascripts.com/jquery/building-a-reusabe-font-size-controller-interface-using-jquery.html' rel='bookmark' title='Permanent Link: Building a reusabe font size controller interface using jQuery'>Building a reusabe font size controller interface using jQuery</a> <small>Most of the web2.0 design allows the user to change...</small></li><li><a href='http://cool-javascripts.com/jquery/add-icons-to-your-links-automatically-using-jquery-css.html' rel='bookmark' title='Permanent Link: Add icons to your links automatically using jQuery &#038; CSS'>Add icons to your links automatically using jQuery &#038; CSS</a> <small>You want to show a particular icon to a particular...</small></li><li><a href='http://cool-javascripts.com/effects/jquery-seekattention-plugin-to-get-the-users-attention.html' rel='bookmark' title='Permanent Link: jQuery seekAttention plugin to get the users attention'>jQuery seekAttention plugin to get the users attention</a> <small>The &#8220;seekAttention&#8221; plugin gracefully get&#8217;s your users attention by fading...</small></li></ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>One of the old problems! Vertical alignment of contents inside an element. For those who don&#8217;t know about this issue,<br />
<img src="http://cool-javascripts.com/wp-content/uploads/2009/01/jquery-vertical-align.jpg" alt="" /><br />
<strong>What is the issue?</strong></p>
<ul>
<li>For eg; there is an area (e.g. &lt;div&gt; or &lt;p&gt;) with known height in the page (For eg: 300px;)</li>
<li>an internal object (typically a paragraph, image etc) is inside the area  		and<strong> </strong>the height is unknown(May be this content is from a table &#8211; we don&#8217;t know how much is the text)</li>
<li>This object should be centered vertically inside the required area.</li>
<li>Tables should not be used.</li>
</ul>
<p>Though there is a CSS property <em><strong>vertical-align</strong></em>, it won&#8217;t work like attribute  <em><strong>valign</strong></em> in HTML tables. CSS property <strong>vertical-align</strong> doesn&#8217;t seem to be able to solve this vertical alignment problem.</p>
<p>There were ofcourse a couple of solutions using css. But all involves elements inside an element  (For eg: &lt;div&gt;inside another &lt;div&gt; and style). But this may be difficult for bloggers, who want to show some content in special formatting (For eg: In my site, notes are shown in a special style). For eg: you want to give a 200px height to a &lt;p&gt; element and vertically center the elements (Same was the case with mine).</p>
<p>So the simplest method is to use javascript. Here I am describing how to solve the vertical alignment issue with jQuery. <span id="more-74"></span><br />
<strong><br />
The logic:<br />
</strong></p>
<ol>
<li>Find the content inside the element</li>
<li>Create another element inside it and assign the content to the newly created element</li>
<li>Find the height of the new element</li>
<li>Adjust the height of the parent if required (the new height may be more than the given one)</li>
<li>Find the margin, and assign it to the new element using css <em>margin-top</em> property.</li>
</ol>
<p><strong>Code</strong></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
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">vAlign</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>container<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	   <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>container <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	      container <span style="color: #339933;">=</span> <span style="color: #3366CC;">'div'</span>;
	   <span style="color: #009900;">&#125;</span>
	   <span style="color: #003366; font-weight: bold;">var</span> paddingPx <span style="color: #339933;">=</span> <span style="color: #CC0000;">10</span>; <span style="color: #006600; font-style: italic;">//change this value as you need (It is the extra height for the parent element)</span>
	   $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;&quot;</span> <span style="color: #339933;">+</span> container <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&gt;&quot;</span> <span style="color: #339933;">+</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/&quot;</span> <span style="color: #339933;">+</span> container <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&gt;&quot;</span><span style="color: #009900;">&#41;</span>;
	   <span style="color: #003366; font-weight: bold;">var</span> el <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span>container <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;:first&quot;</span><span style="color: #009900;">&#41;</span>;
	   <span style="color: #003366; font-weight: bold;">var</span> elh <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">//new element height</span>
	   <span style="color: #003366; font-weight: bold;">var</span> ph <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">//parent height</span>
	   <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>elh <span style="color: #339933;">&gt;</span> ph<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">//if new element height is larger apply this to parent</span>
	       $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span>elh <span style="color: #339933;">+</span> paddingPx<span style="color: #009900;">&#41;</span>;
	       ph <span style="color: #339933;">=</span> elh <span style="color: #339933;">+</span> paddingPx;
	   <span style="color: #009900;">&#125;</span>
	   <span style="color: #003366; font-weight: bold;">var</span> nh <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ph <span style="color: #339933;">-</span> elh<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span>; <span style="color: #006600; font-style: italic;">//new margin to apply</span>
	   $<span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'margin-top'</span><span style="color: #339933;">,</span> nh<span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
     <span style="color: #009900;">&#125;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span>;</pre></td></tr></table></div>

<p><b>A little explanation of function</b></p>
<ul>
<li>First three lines are for the normal jQuery chainable methods</li>
<li>If no container is given, &lt;div&gt; is used</li>
<li>Line # 7 is for the padding height. It will make sure that there is enough padding even if the height of the new element is larger than the parent. For eg: If you want to ensure that 5px width should be there for top and bottom give the value 10</li>
<li>Line # 8 creates the new container with the existing content</li>
<li>Line #9 &#8211; 11 finds the height of the new element, and newly created element</li>
<li>Line #12 &#8211; 15 ensures that the parent div has enough height and padding to hold the new element</li>
<li>Next lines calculates the center value and applies it to the newly created  element using the css <b>margin-top</b> property</li>
</ul>
<p><b>Usage</b></p>
<p>Include the jQuery library and then include this function in your script file or html. </p>
<p class="warning">Do not copy from the above code, it is formatted for display and will not work as javascript. Please download the files (Scroll below)</p>
<p>Just call the function <b><i>vAlign()</i></b> with the required element. The contents inside will be automatically aligned vertically. You may optionally speciy the new element to be created. Useful, if you want to create a &lt;p&gt; element inside instead of a &lt;div&gt;. By default &lt;div&gt; elements will be created. </p>
<p>Eg:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;p.special&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">vAlign</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div.info&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">vAlign</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;p&quot;</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">//This will create &amp;lt;p&amp;gt; as the holder element.</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;p.warn&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">vAlign</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;span&quot;</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">//This will create a &amp;lt;span&amp;gt; element.</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;p.warn&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">vAlign</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;color&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;red&quot;</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">//Yes, you can chanin methods.</span></pre></div></div>

<p class="note"> &lt;span&gt;selements are inline elements, the block properties do not apply to them. So if you want to use inline elements like &lt;span&gt;, &lt;a&gt; then the display:block property should be applied.</p>
<p>For eg:</p>

<div class="wp_syntax"><div class="code"><pre class="css css" style="font-family:monospace;">p<span style="color: #6666ff;">.warn</span> span <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;">&#125;</span></pre></div></div>

<p><strong>Advantages:</strong></p>
<ol>
<li>No need to worry about additional mark ups and style. All required styles and markups will be generated automatically</li>
<li>Content can grow</li>
<li>Simple for content entry</li>
</ol>
<p><strong>Disadvantages:</strong></p>
<ul>
<li>The only disadvantage is it is javascript based and if javascript is turned off it will not work. But this is a very rare case in the modern world. Even the hand held devices support javascript and there is no reason to turn it off.</li>
</ul>
<p><strong>Demo</strong></p>
<p class="info">This one is a demo of vertical aligning with jQuery. Or you can <a rel="shadowbox" class="opensame"  href="http://demos.cool-javascripts.com/jquery-valign-demo.html">view a demo </a>here</p>
<p><strong>Download</strong></p>
<p class="download"><a href="http://demos.cool-javascript.com/jquery-valign-demo.zip">Download the demo</a></p>
<p></p>
<p>Hope it was useful <img src='http://cool-javascripts.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>


<p>Related posts:<ol><li><a href='http://cool-javascripts.com/jquery/building-a-reusabe-font-size-controller-interface-using-jquery.html' rel='bookmark' title='Permanent Link: Building a reusabe font size controller interface using jQuery'>Building a reusabe font size controller interface using jQuery</a> <small>Most of the web2.0 design allows the user to change...</small></li><li><a href='http://cool-javascripts.com/jquery/add-icons-to-your-links-automatically-using-jquery-css.html' rel='bookmark' title='Permanent Link: Add icons to your links automatically using jQuery &#038; CSS'>Add icons to your links automatically using jQuery &#038; CSS</a> <small>You want to show a particular icon to a particular...</small></li><li><a href='http://cool-javascripts.com/effects/jquery-seekattention-plugin-to-get-the-users-attention.html' rel='bookmark' title='Permanent Link: jQuery seekAttention plugin to get the users attention'>jQuery seekAttention plugin to get the users attention</a> <small>The &#8220;seekAttention&#8221; plugin gracefully get&#8217;s your users attention by fading...</small></li></ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://cool-javascripts.com/jquery/vertical-alignment-of-contents-inside-an-element-using-jquery.html/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

