Search code examples
htmlcsspositioningalignment

Vertically and horizontally aligned elements while floating left and right


I want to center four links in a div.

This is what I did so far: jsfiddle

Html:

<div id="menu">
    <section>
        <a class="top" href="#">Top</a>
        <a class="left" href="#">Left</a>
        <a class="right" href="#">Right</a>
        <a class="bottom" href="#">Bottom</a>
    </section>
</div>

Css:

#menu {
    width: 200px;
    height: 200px;
    border: 1px solid #000;
    background: #eee;
    position: relative;
    padding: 10px;
}

#menu>section {
    position: absolute;
    text-align: center;
    width: 200px;
}

#menu a {
    display: block;
    vertical-align: middle;
    height: 20px;
}

#menu .left {
    float: left;
    height: 160px;
}

#menu .right {
    float: right;
}

#menu .bottom {
    clear: both;
}

The problem is that the floated elements do not vertically centered as they should. I want the left and right elements to be in the middle and not at the top.


Solution

  • May be you can use line-height property for this. Like this:

    #menu .left, #menu .right {
        height: 160px;
        line-height:160px;
    }
    

    http://jsfiddle.net/YdPzP/13/