Home > Article > Web Front-end > How to make a div always centered using css?
No matter how the screen slides, the p will always remain in the center of the screen (supports IE7 (including IE7) and above)
The simple code introduced below can achieve this function, please You can come and refer to it.
<p class="loginBox"></p>
The css part should be written like this:
.loginBox { background: #FA2; width: 700px; height: 400px; position: fixed; left: 50%; top: 50%; z-index: 11; /*设定这个p的margin-top的负值为自身的高度的一半,margin-left的值也是自身的宽度的一半的负值.*/ /*宽为400,那么margin-top为-200px*/ /*高为200那么margin-left为-100px;*/ margin: -200px 0 0 -350px; }
After writing, it’s so simple, don’t you think it’s unexpected?
【Related recommendations】
1. Free css online video tutorial
3. php.cn Dugu Jiujian (2)-css video tutorial
The above is the detailed content of How to make a div always centered using css?. For more information, please follow other related articles on the PHP Chinese website!