-テキストはまだ垂直方向に完全に中央揃えされていないため、上記の回答は完全に正しいとは言えません。
.wrapper{
margin:5px;
max-height:250px;
min-height:250px;/*not required only height:250px will do*/
border:1px solid green;
}
.content
{
margin:5px;
border:1px solid black;
height:100px;/*you have to give the parent element a height and a width within which you wish to center*/
width:100px;
position:relative;/*giving it a position relative so that anything inside it will be positioned absolutely relative to this container*/
text-align:center;/*aligning the h1 to the center*/
}
.content h1{
border:1px solid black;
position:absolute;
top:50%;
left:50%;
line-height:50px;
width:50px;
margin-left:-25px;/*half the width of the h1 so that it exactly centers*/
margin-top:-25px;/*half the height of the h1 so that it exactly centers*/
}
説明:
-ever 要素はhtml
長方形のボックスの形をしているので、適用するmargin-top:50%
と、そのボックスの上部が親要素の 50% に揃えられ、ボックス内のテキストには揃えられません。-これが、テキストが正確に中央に配置されていない理由です。
-親要素 (h1 を中央に配置する要素) に幅と高さを指定することも不可欠です。
探していることを行う正しい方法は、絶対位置と相対位置を使用することです。
.container
-相対的な位置h1
の値と絶対的な値を与える
- h1 に幅と高さを指定することで、幅の半分に等しい負の左マージンと、高さの半分に等しい負の上マージンを適用して、テキストが正確に中央に配置されるようにします。