這篇文章主要講解了“CSS怎么實現(xiàn)水平垂直同時居中”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“CSS怎么實現(xiàn)水平垂直同時居中”吧!
10年積累的成都網(wǎng)站制作、網(wǎng)站設計經驗,可以快速應對客戶對網(wǎng)站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡服務。我雖然不認識你,你也不認識我。但先做網(wǎng)站后付款的網(wǎng)站建設流程,更有云陽免費網(wǎng)站建設讓你可以放心的選擇與我們合作。
水平居中和垂直居中已經單獨介紹過,本文將介紹水平垂直同時居中的5種思路
思路一: text-align + line-height實現(xiàn)單行文本水平垂直居中
CSS Code復制內容到剪貼板
<style>
.test{
text-align: center;
line-height: 100px;
}
</style>
XML/HTML Code復制內容到剪貼板
<div class="test" style="background-color: lightblue;width: 200px;">測試文字</div>
思路二:text-align + vertical-align
【1】在父元素設置text-align和vertical-align,并將父元素設置為table-cell元素,子元素設置為inline-block元素
[注意]若兼容IE7-瀏覽器,將結構改為<table>結構來實現(xiàn)table-cell的效果;用display:inline;zoom:1;來實現(xiàn)inline-block的效果
CSS Code復制內容到剪貼板
<style>
.parent{
display: table-cell;
text-align: center;
vertical-align: middle;
}
.child{
display: inline-block;
}
</style>
XML/HTML Code復制內容到剪貼板
<div class="parent" style="background-color: gray; width:200px; height:100px;">
<div class="child" style="background-color: lightblue;">測試文字</div>
</div>
【2】若子元素是圖像,可不使用table-cell,而是其父元素用行高替代高度,且字體大小設為0。子元素本身設置vertical-align:middle
CSS Code復制內容到剪貼板
<style>
.parent{
text-align: center;
line-height: 100px;
font-size: 0;
}
.child{
vertical-align: middle;
}
</style>
XML/HTML Code復制內容到剪貼板
<div class="parent" style="background-color: gray; width:200px; ">
<img class="child" src="/upload/otherpica42/10685.gif" width="50%" alt="test">
</div>
思路三:margin + vertical-align
要想在父元素中設置vertical-align,須設置為table-cell元素;要想讓margin:0 auto實現(xiàn)水平居中的塊元素內容撐開寬度,須設置為table元素。而table元素是可以嵌套在tabel-cell元素里面的,就像一個單元格里可以嵌套一個表格
[注意]若兼容IE7-瀏覽器,需將結構改為<table>結構
CSS Code復制內容到剪貼板
<style>
.parent{
display:table-cell;
vertical-align: middle;
}
.child{
display: table;
margin: 0 auto;
}
</style>
XML/HTML Code復制內容到剪貼板
<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<div class="child" style="background-color: lightblue;">測試文字</div>
</div>
思路四:使用absolute
【1】利用絕對定位元素的盒模型特性,在偏移屬性為確定值的基礎上,設置margin:auto
CSS Code復制內容到剪貼板
<style>
.parent{
position: relative;
}
.child{
position: absolute;
top: 0;
left: 0;
rightright: 0;
bottombottom: 0;
height: 50px;
width: 80px;
margin: auto;
}
</style>
XML/HTML Code復制內容到剪貼板
<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<div class="child" style="background-color: lightblue;">測試文字</div>
</div>
【2】利用絕對定位元素的偏移屬性和translate()函數(shù)的自身偏移達到水平垂直居中的效果
[注意]IE9-瀏覽器不支持
CSS Code復制內容到剪貼板
<style>
.parent{
position: relative;
}
.child{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
XML/HTML Code復制內容到剪貼板
<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<div class="child" style="background-color: lightblue;">測試文字</div>
</div>
【3】在子元素寬高已知的情況下,可以配合margin負值達到水平垂直居中效果
CSS Code復制內容到剪貼板
<style>
.parent{
position: relative;
}
.child{
position: absolute;
top: 50%;
left: 50%;
width: 80px;
height: 60px;
margin-left: -40px;
margin-top: -30px;
}
</style>
XML/HTML Code復制內容到剪貼板
<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<div class="child" style="background-color: lightblue;">測試文字</div>
</div>
思路五:使用flex
[注意]IE9-瀏覽器不支持
【1】在伸縮項目上使用margin:auto
CSS Code復制內容到剪貼板
<style>
.parent{
display: flex;
}
.child{
margin: auto;
}
</style>
XML/HTML Code復制內容到剪貼板
<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<div class="child" style="background-color: lightblue;">測試文字</div>
</div>
【2】在伸縮容器上使用主軸對齊justify-content和側軸對齊align-items
CSS Code復制內容到剪貼板
<style>
.parent{
display: flex;
justify-content: center;
align-items: center;
}
</style>
XML/HTML Code復制內容到剪貼板
<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<div class="child" style="background-color: lightblue;">測試文字</div>
</div>
感謝各位的閱讀,以上就是“CSS怎么實現(xiàn)水平垂直同時居中”的內容了,經過本文的學習后,相信大家對CSS怎么實現(xiàn)水平垂直同時居中這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關知識點的文章,歡迎關注!
網(wǎng)頁題目:CSS怎么實現(xiàn)水平垂直同時居中
鏈接URL:http://muchs.cn/article28/iehdjp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設、響應式網(wǎng)站、網(wǎng)站導航、自適應網(wǎng)站、定制網(wǎng)站、Google
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)