Home  >  Article  >  Web Front-end  >  What are the methods for vertical and horizontal centering in CSS?

What are the methods for vertical and horizontal centering in CSS?

php中世界最好的语言
php中世界最好的语言Original
2018-03-20 14:23:051597browse

This time I will bring you some methods of CSS vertical and horizontal centering, and notes on CSS vertical and horizontal centering. The following is a practical case, let’s take a look.

CSS center alignment

  • The browser prefix is ​​omitted in the code

  • The following examples are sorted according to my personal standards

  • Of course there are more centering methods, but I think only these 5 methods are the most complete solutions

flex centering

Advantages: Can center unknown heights

<style>
    .wrap{height: 100%;display: flex; justify-content: center; align-items: center;align-content:center;}
    
    .other{background-color: #ccc; width: 400px;height: 400px;} /* 额外的样式 可去除 */
</style>
<p class="wrap">
    <p class="other">
        <h2>这是第二层的内容 不会居中</h2>
    </p>
</p>

position + translate Centering

Advantages: Can center unknown heights, with minimum nesting levels

<style>
    /* position 可选 absolute|fixed*/
    .center{position: absolute;left: 50%;top: 50%; transform: translate(-50%,-50%);}
    
    .other{background-color: #ccc; } /* 额外的样式 可去除 */
</style>
<p class="center other">
    <h2>这一层的内容 不会居中</h2>
</p>

table-cell Centering

Disadvantages: 1. The center layer needs to set the width (.center). 2. The outer layer is nested one layer (.cell) 3. The width of the center layer must be set (% allowed)

<style>
    .wrap{display: table;width: 100%;height: 100%;}
    .cell{display: table-cell;vertical-align:middle;}
    .center{width: 400px;margin-left:auto;margin-right:auto;}
    .other{background-color: #ccc;  height: 400px;} /* 额外的样式 可去除 */
</style>
<p class="wrap">
    <p class="cell">
        <p class="center other">
            <h2>这一层的内容 不会居中</h2>
        </p>
    </p>
</p>

Traditional centering (2 types)

Disadvantages: 1. The margin value must for auto. 2. The center layer must have a fixed height and width (% allowed) 3. Position

<style>
    /*
        1. left、top、right、bottom 可以任意,但必须相等
        2. position 可选 absolute|fixed
    */
    .center{position: absolute;left: 10px;top: 10px;right: 10px;bottom: 10px;margin: auto;width: 400px;height: 400px;}
    .other{background-color: #ccc; } /* 额外的样式 可去除 */
</style>
<p class="center other">
    <h2>这一层的内容 不会居中</h2>
</p>

must be used Disadvantages: The center layer must have a fixed height and width, and magin needs to be calculated from the height and width.

<style>
    .wrap{position: relative;height: 100%;}
    .center{position: absolute;left: 50%;top: 50%; width: 400px;height: 300px; margin-left: -200px;margin-top: -150px;}
    .other{background-color: #ccc; } /* 额外的样式 可去除 */
</style>
<p class="wrap">
    <p class="center other">
        <h2>这一层的内容 不会居中</h2>
    </p>
</p>

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to use Redraw and Rearrange

Canvas to create the animation of rotating Tai Chi

The above is the detailed content of What are the methods for vertical and horizontal centering in 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