Home  >  Article  >  Web Front-end  >  How to make a div always centered using css?

How to make a div always centered using css?

零下一度
零下一度Original
2017-05-12 13:59:402202browse

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

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!

Statement:
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