<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Food for thought: Tag html</title>
    <link>http://blog.mkolodziej.net/articles/tag/html?tag=html</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>a blog by &lt;a href="/pages/about"&gt;Maciek Kolodziej&lt;/a&gt;</description>
    <item>
      <title>How to vertically align an image within a div. Again.</title>
      <description>&lt;p&gt;Yeah, so the topic is old, all right. One can find hundreds of articles on the web. Nevertheless, I spent a few hours yesterday looking for an elegant and simple solution to this problem.&lt;/p&gt;

&lt;p&gt;I wanted to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;center an image of an unknown size (but of known maximum size) within a div element&lt;/li&gt;
&lt;li&gt;simple implementation in pure CSS, no tables&lt;/li&gt;
&lt;li&gt;cross-browser compatibility on all major browsers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I found one solution on &lt;a href="http://www.webmasterworld.com/forum83/7910.htm"&gt;Webmaster World forum&lt;/a&gt;. It worked on IE, but not in Firefox. After some more research I added a hack &lt;a href="http://www.ibloomstudios.com/articles/applied_css_vertical-align/"&gt;proposed by Nicholas Gagne&lt;/a&gt; and now it seems to work on both browsers:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&amp;gt;
&amp;lt;html xmlns="http://www.w3.org/1999/xhtml"&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;style type="text/css"&amp;gt;
    div {line-height:150px; text-align: center; width: 150px; 
         font-size: 150px;}
    img {vertical-align:middle;}
    *&amp;gt;div{font-size:12px;}
    &amp;lt;/style&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;div style="border:1px solid gray;"&amp;gt;
      &amp;lt;img src="/images/examples/test.jpg"&amp;gt;
    &amp;lt;/div&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Pay attention to the DOCTYPE declaration: it has to be &lt;strong&gt;strict&lt;/strong&gt; XHTML. It doesn&amp;#8217;t work with &lt;em&gt;transitional&lt;/em&gt; in Firefox. You can replace the line-height, width and font-size of course, just keep the &amp;#8220;vertical&amp;#8221; pixel sizes the same - width can be different.&lt;/p&gt;

&lt;p&gt;I tested it in IE6 and Firefox 2.0.0.11&lt;/p&gt;</description>
      <pubDate>Fri, 25 Jan 2008 03:44:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:59c09aa2-5e35-4e0c-ae93-879edf51df5f</guid>
      <author>Maciej Kołodziej</author>
      <link>http://blog.mkolodziej.net/articles/2008/01/25/how-to-vertically-align-an-image-within-a-div-again</link>
      <category>css</category>
      <category>html</category>
      <category>webdesign</category>
      <trackback:ping>http://blog.mkolodziej.net/articles/trackback/18664</trackback:ping>
    </item>
  </channel>
</rss>
