rmz rmz - 26 days ago 8
HTML Question

How to keep text inside a div, always in the middle?

I'm trying to make text stay in the middle of a resizable DIV.
Here's the example:

CSS

#rightmenu {
position: absolute;
z-index: 999999;
right: 0;
height: 60%;
text-align: center;
}


HTML

<div id="rightmenu">This text should be center aligned and in the middle of the resizable rightmenu</div>


I've tried to make a Class to contain the text with the "margin-top and margin-bottom" both on auto, but doesn't work.

Answer

If you don't care about IE7 support, you can do it like that:

HTML:

<div id=wrap>
  <div id=inside>
    Content, content, content.
  </div> 
</div>

CSS:

#wrap {
    /* Your styling. */
    position: absolute;
    z-index: 999999;
    right: 0;
    height: 60%;
    text-align: center;

    /* Solution part I. */
    display: table;
}

/* Solution part II. */
#inside {
    width: 100%;
    height: 100%;
    display: table-cell;
    vertical-align: middle;
}

The code: http://tinkerbin.com/ETMVplub

If you're OK with JavaScript you can try this jQuery plugin: http://centratissimo.musings.it/ but since it also doesn't seems to support IE7 the CSS solution is probably better.