<?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>Marilyn Burgess &#187; Wordpress</title>
	<atom:link href="http://marilynburgess.com/category/wordpress/feed/" rel="self" type="application/rss+xml" />
	<link>http://marilynburgess.com</link>
	<description></description>
	<lastBuildDate>Mon, 29 Aug 2011 16:33:14 +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>Centering an image in a div</title>
		<link>http://marilynburgess.com/2009/04/centering-an-image-in-a-div/</link>
		<comments>http://marilynburgess.com/2009/04/centering-an-image-in-a-div/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 21:50:32 +0000</pubDate>
		<dc:creator>marilyn</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[Centering images]]></category>
		<category><![CDATA[Php]]></category>

		<guid isPermaLink="false">http://blog.listcentral.me/?p=46</guid>
		<description><![CDATA[While I&#8217;m trying to build List Central, I have also, slowly, been designing another blog called Urpi&#8217;s Dream. I have a long term vision for Urpi&#8217;s Dream, and while I know that I won&#8217;t be able to devote too much time to it during the reign of List Central, on occasion I get completely obsessed [...]]]></description>
			<content:encoded><![CDATA[<p>While I&#8217;m trying to build List Central, I have also, slowly, been designing another blog called <a href="http://urpisdream.com">Urpi&#8217;s Dream</a>. I have a long term vision for Urpi&#8217;s Dream, and while I know that I won&#8217;t be able to devote too much time to it during the reign of List Central, on occasion I get completely obsessed with certain aspects of this other project.</p>
<p>One of these obsessions was to build a new <a href="http://wordpress.org/extend/plugins/">WordPress plugin</a> to hold the specific functionality I want in a image gallery. Yes, I built, yet another, <strong>WordPress Image Gallery Plugin</strong>. I know there are many others out there that do the job, but none that did it to my satisfaction, so I built my own. I still have a few more touch ups to do on Urpi Gallery, but once those are completed, I&#8217;ll be releasing the plugin to the WordPress community. I&#8217;ll likely make an announcement about it here as well as on Urpi&#8217;s Dream.</p>
<p>All of that chatter has little to do with the point of this post. The point of this post is to discuss how to dynamically center and image in a div! So let&#8217;s get to the meat of it!</p>
<p>A part of the Urpi Gallery, mentioned above, is a rotating image widget that can either be used on the sidebar, or in posts. Here&#8217;s an example of it, with some purdy pictures of flowers I found on Flickr under the Creative Commons license :</p>
<h2></h2><p></p><div style='position:relative'><script type='text/javascript' src='http://marilynburgess.com/wp-content/plugins/urpi-gallery/js/urpibox.js'></script><link rel='stylesheet' href='http://marilynburgess.com/wp-content/plugins/urpi-gallery/css/urpibox.css' type='text/css' media='screen' /><script type='text/javascript' src='http://marilynburgess.com/wp-content/plugins/urpi-gallery/js/Transitions.js'></script> <script type='text/javascript'> 
                   jQuery(document).ready(function() { 
                    jQuery('#_slideshow').cycle({             
                        fx:'scrollLeft,scrollDown,scrollRight,scrollUp,blindX,blindY,cover,curtainX,curtainY,fade,fadeZoom,slideX,slideY,turnUp,turnDown,turnLeft,turnRight,uncover,wipe,zoom', 
                        randomizeEffects: true, 
                        timeout:  5000,
                        random:  1                        
                    }); 
                });</script>  <div style=' height:242px; width:242px;padding: 0;margin:0px;background-color:#E0E0E0' id='outter'><div id='_slideshow' style='position: relative; height:242px; width:242px;padding: 0;margin:0px;' class='UrpiDynamicWidgetWrapper'></div></div></div><div style='clear:left'></div><br /><br />
<p>As you can see above, the dynamically chosen image is very nicely centered in that gray div. I thought that centering an image in a div, as above, would be a piece of cake, but it turned out not to be so clear cut.</p>
<p>Centering an image in a table cell, both horizontally and vertically is trivial:</p>
<table border="0">
<tbody>
<tr>
<td class="centeredImageTD"><img src="/images/Benny.jpg" alt="Benny" width="150" /></td>
<td style="vertical-align:top;"><code>&amp;lt;style&amp;gt;<br />
.centeredImageTD {<br />
width: 180px;<br />
height: 180px;<br />
vertical-align:middle;<br />
text-align:center<br />
}<br />
&amp;lt;/style&amp;gt;</code></p>
<p>&lt;td class=&#8221;centeredImageTD&#8221;&gt;<br />
&lt;img src=&#8221;Benny.jpg&#8221; width=&#8221;120&#8243; alt=&#8221;Benny&#8221; /&gt;<br />
&lt;/td&gt;</td>
</tr>
</tbody>
</table>
<p>Isn&#8217;t that easy? All done with some css on the table cell (td) that explicitly states: center the content of this table cell, both horizontally and vertically. The dimensions of the image don&#8217;t matter with this method of centering.</p>
<p>As the web has evolved, the appropriateness of tables in web design has decreased, and the task now is to put our designs together using divs. The use of divs improves many web design tasks, but it makes this one task, of centering images more difficult. While the tag can be used in a pinch to center an image horizontally, there is no equivalent tag or style rule to center an image vertically. The only option is to <strong>use the margins of the image, or the padding on the div</strong>. Both <strong>require that you know the width and height of the image, and div</strong>.</p>
<p>As I was determined to have my images centered in a div, I got down to writing  my  php functions to make it happen, every time. The function below requires that you pass the actual dimension of the image, which can be captured with tools like <em>identify </em>from <a href="http://www.imagemagick.org/script/identify.php">ImageMagick</a>, or with the built in php functions <a href="http://ca3.php.net/imagesx">imagesx</a> and <a href="http://ca3.php.net/imagesy">imagesy</a>.<em> </em>Also required by the function is the  desired width of the border given to the image ($border_width), and the width of the div you want to center your image in as $divsize.  This function assumes your div is square, but it is possible to adapt this function to accommodate rectangular divs.</p>
<p><code>/*<br />
getCenteredMargins  - determines the margins for an the image to be centered in a div</code></p>
<p>1. $imagefile &#8211; Path to the image<br />
2. $divsize &#8211; The width and height of the square div containing the image in pixels<br />
3. $border_width &#8211; The width of the desired boreder around the image in pixels<br />
4. $max_size &#8211; The maximum width and height of the image as it is to appear on the page<br />
*/<br />
function getCenteredMargins($image_width, $image_height, $divsize, $border_width, $max_size){</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;// Initializations<br />
&nbsp;&nbsp;&nbsp;&nbsp;$width = 0; $height = 0;<br />
&nbsp;&nbsp;&nbsp;&nbsp;$margin_top_bottom = &#8220;&#8221;; $margin_left_right = &#8220;&#8221;;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;if($image_width &gt; $image_height){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Most likely a pic landscape orientation, width of &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the image will<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// equal the max_size<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$width = $max_size;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Determine scaled down height of the image with the the ratio<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// of the original image<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$height = (int)($max_size * $image_height / $image_width);</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// right and left margins will be small, and equal to the<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// specified border width<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$margin_left_right = $border_width . &#8220;px&#8221;;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// top and bottom margins will be larger<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$margin_top_bottom = (int)(($divsize &#8211; $height) / 2) . &#8220;px&#8221;;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}else{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Most likely a pic portrait orientation, height of the image will<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// equal the max_size<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$height = $max_size;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Determine scaled down width of the image with the the ratio<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// of the original image<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$width = (int)($max_size * $image_width / $image_height);</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// top and bottom margins will be small, and equal to the<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// specified border width<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$margin_top_bottom = $border_width . &#8220;px&#8221;;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// right and left margins will be larger<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$margin_left_right = (int)(($divsize &#8211; $width) / 2) . &#8220;px&#8221;;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;return array($margin_top_bottom, $margin_left_right,$width, $height);<br />
}<br />
list($margin_top_bottom , $margin_left_right, $width, $height) = getCenteredMargins($image_width, $image_height, $divsize, $border_width, $max_size);<br />
$image_style = &#8220;margin: $margin_top_bottom $margin_left_right;&#8221;;<br />
$html = &#8220;&lt;div&gt;&lt;img  style=&#8221;$image_style&#8221; src=&#8221;$src&#8221; width=&#8221;$width&#8221; height=&#8221;$height&#8221; /&gt;&lt;/div&gt;&#8221;;<br />
</code></p>
<h3  class="related_post_title">You might also like...</h3><ul class="related_post"><li><a href="http://marilynburgess.com/2009/04/share-buttons-are-so-easy/" title="Share Buttons are SO Easy!!">Share Buttons are SO Easy!!</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://marilynburgess.com/2009/04/centering-an-image-in-a-div/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

