CSS3/CSS之居中解析(水平+垂直居中、水平居中,垂直居中)

首先,咱們來看下垂直居中:css

(1)、若是是單行文本,則能夠設置的line-height的數值,讓其等於父級元素的高度!html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box{
            background: green;
            height:200px;
        }
        a {height:100%;
        line-height: 200px;
        color:red;
}
 </style>
</head>
<body>
   <div class="box">
       <a href="">ggg </a>
   </div>
</body>
</html>

 

 

 (2)、若是元素是行內塊級元素,通常會使用diaplay:inline-block,vertical-align:middle,還有一個僞元素讓元素內容處於容器中央!給父元素添加僞元素!前端

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            background: green;
            height:200px;
        }
     .box::after, .box2{
         display:inline-block;
         vertical-align: middle;
     }
     .box::after{
         content:'';
         height:100%;
     }
     .box2{background-color:red;width:20px;height:20px;}
 </style>
</head>
<body>
   <div class="box">
       <div class="box2"></div>
   </div>
</body>
</html>

 

 

 (3)、經過display:flex來實現垂直居中;web

父級:display:flex;app

子元素:align-self:center;學習

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            background: green;
            height:300px;
            width: 600px;
            display:flex;
           
        }
        .box2{background:red;
        width:30%;
        height:30%;
        align-self: center;

     }
    
 </style>
</head>
<body>
   <div class="box">
       <div class="box2"></div>
   </div>
</body>
</html>

(4)、使用display:table進行垂直居中!flex

給父元素設置display:table;子元素設置爲display:table-cell;ui

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            background-color: green;
            height:300px;
            width: 600px;
            display:table;
        }
      
       .box .box2{
           color:red;
       
        display:table-cell;
         vertical-align: middle;
         
}
    
 </style>
</head>
<body>
   <div class="box">
       <div class="box2">ddddd</div>
   </div>
</body>
</html>

 

 

 (5),已經知道父元素的高度,給子元素相對定位,在經過translaY()獲得垂直居中flexbox

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            background-color: green;
            height:300px;
            width: 600px;
           
        }
      
       .box .box2{
           background-color:red;
            width:50%;
             height:50%;
             position: relative;
             top:50%;
             transform:translateY(-50%);        
}
    
 </style>
</head>
<body>
   <div class="box">
       <div class="box2"></div>
   </div>
</body>
</html>

 

 

 6)、父元素高度不知道,同過transform實現,先給父元素相對定位,在給子元素絕對定位spa

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            background-color: green;
            height:300px;
            width: 600px;
            position: relative;
        }
      
       .box .box2{
           background-color:red;
            width:50%;
             height:50%;
             position: absolute;
             top:50%;
             transform:translateY(-50%);        
}
    
 </style>
</head>
<body>
   <div class="box">
       <div class="box2"></div>
   </div>
</body>
</html>

2、水平居中:

(1)、行內元素方案,只要把行內元素包裹 在一個盒子中,而且在他父級元素添加以下屬性:text-align:center;

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            background-color: green;
            height:300px;
            width: 600px;
            text-align:center;
        }
      P{color:red;}     
 </style>
</head>
<body>
   <div class="box">
       <p>1111</p>
   </div>
</body>
</html>

  

 

 (2)、單個塊狀元素解決方案:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            background-color: green;
            height:300px;
            width: 600px;
            text-align:center;
        }
    .box2{
        background-color: red;
        width: 20px;
        height: 20px;
        margin:0 auto;
    }    
 </style>
</head>
<body>
   <div class="box">
       <div class="box2"></div>
   </div>
</body>
</html>

 

 3)、多個塊狀元素解決方案:父元素設置爲text-align:center;子元素設置爲:display:inline-block;

 

 相信不少人在剛接觸前端或者中期時候總會遇到一些問題及瓶頸期,如學了一段時間沒有方向感或者堅持不下去一我的學習枯燥乏味有問題也不知道怎麼解決,對此我整理了一些資料 喜歡個人文章想與更多資深大牛一塊兒討論和學習的話 歡迎加入個人學習交流羣907694362

!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            background-color: green;
            height:300px;
            width: 600px;
            text-align:center;
        }
    .box2, .box3{
        background-color: red;
        width: 20px;
        height: 20px;
        display:inline-block;
    }    
 </style>
</head>
<body>
   <div class="box">
       <div class="box2"></div>
       <div class="box3"></div>
   </div>
</body>
</html>

也能夠使用flexbox來實現:

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            background-color: green;
            height:300px;
            width: 600px;
           display:flex;
           justify-content:center;
        }
    .box2, .box3, .box4{
        background-color: red;
        width: 20px;
        height: 20px;
      
    }    
 </style>
</head>
<body>
   <div class="box">
       <div class="box2"></div>
       <div class="box3"></div>
       <div class="box4"></div>
   </div>
</body>
</html>

 

(4)、不定寬度塊元素水平居中:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            float:left;
            position: relative;
            left:50%;
        }
    .box ul{
        position:relative;
        left:-50%;
    }
 </style>
</head>
<body>
   <div class="box">
       <ul>
           <li>1111</li>
           <li>111</li>
           <li>111</li>
       </ul>
   </div>
</body>
</html>

三:實現水平+垂直居中,也就是在中央:(1)單行行內元素:父元素設置:text-align:center,display:table-cell;vertical-align:middle,在這裏,圖片,文字,都是同樣的操做

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            background-color: #ccc;
            width:500px;
            height: 500px;
            display:table-cell;
            text-align: center;
            vertical-align: middle;
        }
        img{width:50px;
        height: 50px;}
   
 </style>
</head>
<body>
   <div class="box">
      <img src="5.jpg" alt="">
   </div>
</body>
</html>

 

 文字在中央,還能夠父級設置爲text-align:center;line-height設置爲父元素的height

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            background-color: #ccc;
            width:500px;
            height: 500px;
            text-align: center;
        }
        
   p{line-height: 500px;}
 </style>
</head>
<body>
   <div class="box">
  <p>1111</p>
      
   </div>
</body>

(2)對於單個塊級元素,父元素設置爲相對定位,子元素設置爲絕對定位高度,寬度爲50%

 

 

!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            background-color: #ccc;
            width:500px;
            height: 500px;
            position: relative;  
        }
        .box2{position: absolute;
        top:50%;
        left:50%;
        background-color: black;
        width: 20px;
        height: 20px;}
   
 </style>
</head>
<body>
   <div class="box">
       <div class="box2"></div>
  
      
   </div>
</body>
</html>

(3)、flex實現不定寬度水平+垂直居中

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            background-color: green;
            height:300px;
            width: 600px;
           display:flex;
           justify-content:center;
           align-items:center;
        }
    .box2, .box3, .box4{
        background-color: red;
        width: 20px;
        height: 20px;
      
    }    
 </style>
</head>
<body>
   <div class="box">
       <div class="box2"></div>
      
   </div>
</body>
</html>

推薦閱讀:CSS邊框長度控制 

                   css樣式的書寫順序及原理——很重要!

原文連接:http://www.javashuo.com/article/p-qebzgpfa-h.html

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <meta http-equiv="X-UA-Compatible" content="ie=edge">    <title>Document</title>    <style>        .box{            background: green;            height:200px;        }        a {height:100%;        line-height: 200px;        color:red;} </style></head><body>   <div class="box">       <a href="">ggg </a>   </div></body></html>————————————————版權聲明:本文爲CSDN博主「左手寫愛等等」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處連接及本聲明。原文連接:https://blog.csdn.net/qq_35788269/article/details/80691114

相關文章
相關標籤/搜索