How to make a div always centered using css?

零下一度
Release: 2017-05-12 13:59:40
Original
2249 people have browsed it

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>
Copy after login

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;
}
Copy after login

After writing, it’s so simple, don’t you think it’s unexpected?

【Related recommendations】

1. Free css online video tutorial

2. css online manual

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!