tags:

views:

10980

answers:

6

I have a div with two images and an h1. All of them need to be vertically aligned within the div, next to each other. One of the images needs to be absolute positioned within the div.

What is the css needed for this to work on all common browsers?

  <div id=header">
      <img src=".." ></img>
      <h1>testing...</h1>
      <img src="..."></img>
    </div>
A: 
<div id="header" style="vertical-align:middle;">

...

or CSS

.someClass
{
   vertical-align:middle;
}
dimarzionist
The vertical-align property does not apply since the div is display: block by default and nothing appears to have been done to change it.
David Dorward
+2  A: 

By default h1 is a block element and will render on the line after the first img, and will cause the second img to appear on the line following the block.

To stop this from occurring you can set the h1 to have inline flow behaviour:

#header > h1 { display: inline; }

As for absolutely positioning the img inside the div, you need to set the containing div to have a "known size" before this will work properly. In my experience, you also need to change the position attribute away from the default - position: relative works for me:

#header { position: relative; width: 20em; height: 20em; }
#img-for-abs-positioning { position: absolute; top: 0; left: 0; }

If you can get that to work, you might want to try progressively removing the height, width, position attributes from div.header to get the minimal required attributes to get the effect you want.

UPDATE:

Here is a complete example that works on Firefox 3:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html>
    <head>
        <title>Example of vertical positioning inside a div</title>
        <style type="text/css">
            #header > h1 { display: inline; }
            #header { border: solid 1px red; 
                      position: relative; }
            #img-for-abs-positioning { position: absolute;
                                       bottom: -1em; right: 2em; }
        </style>
    </head>

    <body>
        <div id="header">
            <img src="#" alt="Image 1" width="40" height="40" />
            <h1>Header</h1>
            <img src="#" alt="Image 2" width="40" height="40" 
                 id="img-for-abs-positioning" />
        </div>
    </body>
</html>
fd
I should note that having the right DOCTYPE can sometimes make big differences to how CSS will render, especially on Internet Explorer. I'd recommend you chose a DOCTYPE known to align with a strict standards mode so you can expect more consistent behaviour between browsers.
fd
I wouldn't 'expect' any consistency between browsers.. only way to be sure is to test :/
Cocowalla
+2  A: 

All of them need to be vertically aligned within the div

Aligned how? Tops of the images aligned with the top of the text?

One of the images needs to be absolute positioned within the div.

Absolutely positioned relative to the DIV? Perhaps you could sketch out what you're looking for...?

fd has described the steps for absolute positioning, as well as adjusting the display of the H1 element such that images will appear inline with it. To that, i'll add that you can align the images by use of the vertical-align style:

#header h1 { display: inline; }
#header img { vertical-align: middle; }

...this would put the header and images together, with top edges aligned. Other alignment options exist; see the documentation. You might also find it beneficial to drop the DIV and move the images inside the H1 element - this provides semantic value to the container, and removes the need to adjust the display of the H1:

<h1 id=header">
   <img src=".." ></img>
   testing...
   <img src="..."></img>
</h1>
Shog9
A: 

@dimarzionist: That's the first thing any web developer would think of and it doesn't work. My example is a lot more complicated. I suggest next time first you test what you suggest. You need to be voted down.

Abdu
This should have been left as a comment on his answer.
Hosam Aly
And his answer is not very bad BTW. He doesn't know how "advanced developer" you are! The answer is incorrect, but not offensive!
Hosam Aly
+14  A: 

Wow, this problem is popular. It's based on a misunderstanding in the vertical-align property. This excellent article explains it:

Understanding vertical-align, or "How (Not) To Vertically Center Content"

Konrad Rudolph
A: 

display:inline;