-the ANS心不是完全正確的文本將仍然不能完全垂直居中的上方。
.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*/
}
解釋:
在
html
-ever元件是矩形框,以便施加margin-top:50%
的是,框的頂部對準的父元素的50%並且不箱內文本的形式。 - 這就是文本與中心不完全一致的原因。
- 同樣重要的是提供父元素(其中你希望居中h1)的寬度和高度。
正確的做你正在尋找的方法是通過使用絕對和相對定位。
-give的.container
的相對位置值和h1
的絕對
-by給H1的寬度和高度的值,我們然後施加負左邊距等於寬度和負上邊距半等於高度的一半,以便文本完全居中。
你需要H1是「中央中」到.TOP DIV?或.top div到'center middle'到.wrapper div? – Sowmya