How to center div in the page? (Height & Width)?

懵懂的女人 提交于 2019-12-02 16:41:50

问题


silly question, but I can't find answer on it as all answers assume centering div in terms of width.

What I need is to center div in means of height and width so in a very very center of the page.

I tried this, but it just centers div in means of page width no height.

#myDiv {
   width: 500px;
   height: 500px;
   margin: auto;
}

So how can I get it in the VERY center of the page? )))


回答1:


Create a wrapper with ID wrapper around the #myDiv element, and apply this CSS code:

#wrapper{
    display: table;
}
#myDiv{
    display: table-cell;
    vertical-align: middle; /*Vertically centered*/
    text-align: center; /* Horizontally centered */
}

This code centers elements of any width/height.




回答2:


position: absolute;
left: 50%;
top: 50%;
margin-left: -250px; /*(half of width)*/
margin-top: -250px;  /*(half of height)*/

that should work.



来源:https://stackoverflow.com/questions/7728811/how-to-center-div-in-the-page-height-width

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!