<?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; vertical align</title>
	<atom:link href="http://cool-javascripts.com/tag/vertical-align/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>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>

