この回答はもはや最良の回答ではありません...代わりに以下のフレックスボックスの回答を参照してください!
完全に中央に配置するには(デビッドの回答で述べたように)、負の上マージンを追加する必要があります。1行のテキストしかないことがわかっている(または強制する)場合は、次を使用できます。
margin-top: -0.5em;
例えば:
http://jsfiddle.net/45MHk/623/
//CSS:
html, body, div {
height: 100%;
}
#parent
{
position:relative;
border: 1px solid black;
}
#child
{
position: absolute;
top: 50%;
/* adjust top up half the height of a single line */
margin-top: -0.5em;
/* force content to always be a single line */
overflow: hidden;
white-space: nowrap;
width: 100%;
text-overflow: ellipsis;
}
//HTML:
<div id="parent">
<div id="child">Text that is suppose to be centered</div>
</div>
最初に受け入れられた回答は、テキストの中央で垂直方向の中央に配置されません (テキストの上部に基づいて中央に配置されます)。したがって、親の背があまり高くない場合は、中央に表示されません。次に例を示します。
http://jsfiddle.net/45MHk/
//CSS:
#parent
{
position:relative;
height: 3em;
border: 1px solid black;
}
#child
{
position: absolute;
top: 50%;
}
//HTML:
<div id="parent">
<div id="child">Text that is suppose to be centered</div>
</div>