views:

136

answers:

1

I want to have my list (nav) align to the center of an image (logo). I tried using vertical-align: middle;, but I couldn't get it to work when I floated the images left and right.

Here's my code:

HTML:

<div id="head">
    <img id="logo" src="logo.png" />
    <ul id="nav">
        <li><a href="#">Item 1</a></li>
        <li><a href="#">Item 2</a></li>
        <li><a href="#">Item 3</a></li>
    </ul>
</div>

CSS:

#head {
    margin-top: 2px;
    width: 100%;
}
    #logo {
        float: left;
        }

    ul#nav {
        float: right;
        }

        ul#nav li {
            display: inline;
            list-style-type: none;
            }

I took all the vertical-align: middle;'s from where I put them (I tested it in each element, even though it was only supposed to be applied to #logo, from what I've read.)

Anyways, any help would be appreciated.

+2  A: 

Vertical-align:middle aligns the median of child element to the median of parent element. If all child elements have float:left, then the parent has a height of 0px and hence its median is above the child elements.

So, you might add a <br style='clear:both' /> after your menu and the DIV will finally get its vertical size.

naivists
I added the `<br style='clear:both' />`, set a height attribute on `#head`, and put `vertical-align: middle` on the `#logo` and `#nav` IDs, but they still align to the top.
Andrew
But actually, why do you need `float:left` for the UL element?
naivists
I don't. I'm floating the logo left and the list right.
Andrew
anyway, floated elements are 'taken out' of the regular text stream, so "vertical-align" attribute does not apply to them. A simple workaround to align the figures is to set `padding-top` of the image.
naivists