CSS實現垂直居中的問題

Yaojiiu-發表於2020-10-12

一個div在另一個div中的垂直居中的設定?

(1)利用text-align 實現塊元素水平垂直居中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .continer{
            width: 400px;
            height: 300px;
            background-color: red;
            text-align: center;
            padding-top: 100px;
        }
        .inner{
            width: 200px;
            height: 200px;
            background-color: green;
            display: inline-block;
        }
    </style>
</head>
<body>
    <div class="continer"> 
        <div class="inner"></div> 
    </div>
</body>
</html>

在這裡插入圖片描述

(2)利用padding設定,且父元素和子元素的大小一致

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .continer{ 
           width:200px;
           height: 200px;
           background-color: red;
           padding: 20px;
        }
        .inner{ 
           width:200px;
           height: 200px;
           background-color: green;
        }
    </style>
</head>
<body>
    <div class="continer">
        <div class="inner"></div> 
    </div>
</body>
</html>

在這裡插入圖片描述

(3)利用position和margin進行元素水平垂直居中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .continer{ 
            width: 200px; 
            height: 200px; 
            background: skyblue; 
            position: relative; 
        }
        .inner{ 
            width: 100px; 
            height: 100px; 
            background: orangered; 
            position: absolute; 
            left: 50%; 
            top: 50%; 
            margin: -50px 0px 0px -50px;
        }
    </style>
</head>
<body>
    <div class="continer"> 
        <div class="inner"></div>
    </div>
</body>
</html>

在這裡插入圖片描述

(4)利用position進行元素的水平垂直居中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .continer{ 
            width: 200px; 
            height: 200px; 
            background: skyblue; 
            position: relative; 
        }
            .inner{
            width: 100px; 
            height: 100px; 
            background: orangered; 
            position: absolute; 
            left: 0px; 
            top: 0px; 
            right:0px; 
            bottom:0px;  
            margin: auto; 
        }
    </style>
</head>
<body>
    <div class="continer"> 
        <div class="inner"></div>
     </div>
</body>
</html>

在這裡插入圖片描述

(5)適用於圖片的居中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .div1{
            width: 400px;
            height: 400px;
            background-color: pink;
            text-align: center;
            line-height: 400px;
        }
        img{
            width: 200px;
            height: 200px;
            vertical-align: middle;
        }
    </style>
</head>
<body>
    <div class="div1">
        <img src="img/3.jpg" alt="">
    </div>
</body>
</html>

在這裡插入圖片描述

相關文章