天天看點

用css設定水準居中,垂直居中和垂直水準居中

水準居中

  • 行内元素

給父元素設定text-align:center。

若父元素不是塊元素,則給父元素設定display:block即可

<style>
    .father {
        width: 200px;
        height: 200px;
        background-color: green;
        text-align: center;
    }

    .son {
        background-color: red;
    }
</style>
<div class='father'>
    <span class="son">
        sssssss
    </span>
</div>
           

效果圖:

用css設定水準居中,垂直居中和垂直水準居中
  • 塊元素

    1、設定margin:0 auto讓其行方向自适應分布。

<style>
    .father {
        width: 200px;
        height: 200px;
        background-color: green;
    }

    .son {
        width: 100px;
        height: 100px;
        background-color: red;
        margin: 0 auto;
    }
</style>
<div class='father'>
    <div class="son">
        sssssss
    </div>
</div>
           

效果圖:

用css設定水準居中,垂直居中和垂直水準居中

2、給父元素設定相對定位,子元素設定絕對定位,通過left:50%将子元素的左上角水準居中,然後通過margin-left:-50px(該值為子元素寬度的一半)将其整體水準居中。

<style>
    .father {
        width: 200px;
        height: 200px;
        background-color: green;
        position: relative;
    }

    .son {
        width: 100px;
        height: 100px;
        background-color: red;
        position: absolute;
        left: 50%;
        margin-left: -50px;
    }
</style>
<div class='father'>
    <div class="son">
        sssssss
    </div>
</div>
           

3、同第二條,先給父元素設定相對定位,子元素設定絕對定位,通過left:50%将子元素的左上角水準居中,然後通過transform:translateX(-50%)将其整體水準居中。

<style>
    .father {
        width: 200px;
        height: 200px;
        background-color: green;
        position: relative;
    }

    .son {
        width: 100px;
        height: 100px;
        background-color: red;
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
    }
</style>
<div class='father'>
    <div class="son">
        sssssss
    </div>
</div>
           

4、給父元素設定display:flex,采用flex布局的元素相當于一個容器,它的所有子元素自動成為容器成員。

該容器預設存在兩根軸:水準方向的主軸以及垂直方向的交叉軸。

然後通過justify-content:center将其子元素在主軸方向上居中。

<style>
    .father {
        width: 200px;
        height: 200px;
        background-color: green;
        display: flex;
        justify-content: center;
    }

    .son {
        width: 100px;
        height: 100px;
        background-color: red;
    }
</style>
<div class='father'>
    <div class="son">
        sssssss
    </div>
</div>
           

垂直居中

  • 行内元素

    給行内元素添加line-hight:200px(該值為父元素的高度)

<style>
    .father {
        width: 200px;
        height: 200px;
        background-color: green;
    }

    .son {
        background-color: red;
        line-height: 200px;
    }
</style>
<div class='father'>
    <span class="son">
        sssssss
    </span>
</div>
           

效果圖:

用css設定水準居中,垂直居中和垂直水準居中
  • 塊元素

    1、同水準居中,先給父元素設定相對定位,子元素設定絕對定位,通過top:50%将子元素的左上角垂直居中,然後通過margin-top:-50px(該值為子元素高度的一半)将其整體垂直居中。

<style>
    .father {
        width: 200px;
        height: 200px;
        background-color: green;
        position: relative;
    }

    .son {
        width: 100px;
        height: 100px;
        background-color: red;
        position: absolute;
        top: 50%;
        margin-top: -50px;
    }
</style>
<div class='father'>
    <div class="son">
        sssssss
    </div>
</div>
           

效果圖:

用css設定水準居中,垂直居中和垂直水準居中

2、同水準居中,先給父元素設定相對定位,子元素設定絕對定位,通過top:50%将子元素的左上角垂直居中,然後通過transform:translateY(-50%)将其整體垂直居中。

<style>
    .father {
        width: 200px;
        height: 200px;
        background-color: green;
        position: relative;
    }

    .son {
        width: 100px;
        height: 100px;
        background-color: red;
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
    }
</style>
<div class='father'>
    <div class="son">
        sssssss
    </div>
</div>
           

3、同水準居中,給父元素設定display:flex,采用flex布局的元素相當于一個容器,它的所有子元素自動成為容器成員。

該容器預設存在兩根軸:水準方向的主軸以及垂直方向的交叉軸。

然後通過align-items:center将其子元素在交叉軸方向上居中。

<style>
    .father {
        width: 200px;
        height: 200px;
        background-color: green;
        display: flex;
        align-items: center;
    }

    .son {
        width: 100px;
        height: 100px;
        background-color: red;
    }
</style>
<div class='father'>
    <div class="son">
        sssssss
    </div>
</div>
           

垂直水準居中

1、垂直水準居中理所當然的是将水準居中和垂直居中合起來就行了。

先給父元素設定相對定位,子元素設定絕對定位,通過top:50%和left:50%将子元素的左上角垂直水準居中,然後通過transform:translate(-50%,-50%)将其整體垂直水準居中(translate中第一個參數即translateX,第二個參數即translateY)。

<style>
    .father {
        width: 200px;
        height: 200px;
        background-color: green;
        position: relative;
    }

    .son {
        width: 100px;
        height: 100px;
        background-color: red;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }
</style>
<div class='father'>
    <div class="son">
        sssssss
    </div>
</div>
           

效果圖:

用css設定水準居中,垂直居中和垂直水準居中

2、先給父元素設定相對定位,子元素設定絕對定位,通過top:50%和left:50%将子元素的左上角垂直水準居中,然後通過margin-top:-50px(該值為子元素高度的一半)和margin-left:-50px(該值為子元素寬度的一半)将其整體垂直水準居中。

<style>
    .father {
        width: 200px;
        height: 200px;
        background-color: green;
        position: relative;
    }

    .son {
        width: 100px;
        height: 100px;
        background-color: red;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-top: -50px;
        margin-left: -50px;
    }
</style>
<div class='father'>
    <div class="son">
        sssssss
    </div>
</div>
           

3、先給父元素設定相對定位,子元素設定絕對定位,通過top:0,left:0,right:0和bottom:0将子元素的左上角定于父元素的左上角,然後通過margin:auto将其整體垂直水準居中。

<style>
    .father {
        width: 200px;
        height: 200px;
        background-color: green;
        position: relative;
    }

    .son {
        width: 100px;
        height: 100px;
        background-color: red;
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        margin: auto;
    }
</style>
<div class='father'>
    <div class="son">
        sssssss
    </div>
</div>
           

4、給父元素設定display:flex,采用flex布局的元素相當于一個容器,它的所有子元素自動成為容器成員。

該容器預設存在兩根軸:水準方向的主軸以及垂直方向的交叉軸。

然後通過justify-content:center和align-items:center将其子元素在主軸方向和交叉軸方向上居中。

<style>
    .father {
        width: 200px;
        height: 200px;
        background-color: green;
        display: flex;
        justify-content: center;
        align-items: center;
    }

    .son {
        width: 100px;
        height: 100px;
        background-color: red;
    }
</style>
<div class='father'>
    <div class="son">
        sssssss
    </div>
</div>