首先,我們來看下垂直居中: (1)、如果是單行文本,則可以設置的line-height的數值,讓其等於父級元素的高度! <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" conte ...
首先,我們來看下垂直居中:
(1)、如果是單行文本,則可以設置的line-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: 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來實現垂直居中;
父級:display:flex;
子元素: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進行垂直居中!
給父元素設置display:table;子元素設置為display:table-cell;
<!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()得到垂直居中
<!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實現,先給父元素相對定位,在給子元素絕對定位
<!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>
二、水平居中:
(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邊框長度控制
原文鏈接:https://blog.csdn.net/qq_35788269/article/details/80691114
<!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