CSS实现水平、垂直居中的6种方式

1.块级元素和行内元素
2.水平居中和垂直居中
3行内元素的水平居中
	 1.table
     2.设置line-height
     3.text-align:center
     4.margin:0 auto;
     5.绝对定位
     6.flex弹性盒模型
     7.calc
     8.auto
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<style>
    *{
        padding: 0;
        margin: 0;
    }
    /*方式1:display:table实现居中*/
    #div1{
        width: 400px;
        height: 400px;
        border: 1px solid #000;
        display: table;
        margin-left: 100px;
    }
    #div1 p {
        width: 100px;
        height: 100px;
        display: table-cell;
        vertical-align: middle;
        text-align: center;
    }
    /*方式1:text-align: center;line-height实现居中*/
    #div2{
        width: 400px;
        height: 400px;
        border: 1px solid red;
        text-align: center;
        line-height: 400px;
        margin-left: 100px;
    }
    #div3{
        width: 400px;
        height: 400px;
        border: 1px solid blue;
        margin-left: 100px;
        position: relative;
    }
    /*绝对定位实现居中*/
    #div3 p{
        width: 100px;
        height: 100px;
        position: absolute;
        left: 50%;
        top: 50%;
        background-color: #4CAF50;
        margin-left: -50px;
        margin-top: -50px;
    }
    /*弹性盒子实现居中,推荐使用这个方法*/
    #div4 {
        width: 400px;
        height: 400px;
        border: 1px solid green;
        display: flex;
        margin-left: 100px;
        justify-content: center;
        align-items: center;
    }
    #div5{
        width: 400px;
        height: 400px;
        border: 1px solid purple;
        margin-left: 100px;
        position: relative;
    }
    /*绝对定位之---calc实现居中*/
    #div5 p{
        width: 100px;
        height: 100px;
        position: absolute;
        left: calc(50% - 50px);
        top:calc(50% - 50px);
        background-color: yellowgreen;
    }
    #div6{
        width: 400px;
        height: 400px;
        border: 1px solid deeppink;
        margin-left: 100px;
        position: relative;
    }
    /*绝对定位之---上下左右等于0实现居中*/
    #div6 p{
        background: yellowgreen;
        position: absolute;
        width: 100px;
        height: 100px;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        margin: auto;
    }
    #div7{
        width: 400px;
        height: 400px;
        border: 1px solid deeppink;
        margin-left: 100px;
        position: relative;
    }
    /*绝对定位之---上下左右等于0实现居中*/
    #div7 p{
        background: red;
        position: absolute;
        width: 100px;
        height: 100px;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
    }
    #div8{
        width: 400px;
        height: 400px;
        border: 1px solid deeppink;
        margin-left: 100px;
        position: relative;
    }
    #div8 p{
        width: 100px;
        height: 400px;
        margin: 0 auto;
        background: red;
    }
</style>
<body>
<div id="div1"><p>居中1</p></div>
<div id="div2">居中2</div>
<div id="div3"><p>居中3</p></div>
<div id="div4"><p>居中4</p></div>
<div id="div5"><p>居中5</p></div>
<div id="div6"><p></p></div>
<div id="div7"><p></p></div>
<div id="div8"><p></p></div>

</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_44171297/article/details/109967796