【CSS三種居中方案全解】CSS垂直居中常用方法集結
阿新 • • 發佈:2020-12-14
技術標籤:筆記CSS前端定位htmlcsscss3html5
目錄
前言
CSS垂直居中
參考資料
系列文章
一、前言
難得搜尋整理一番 CSS 垂直居中,水平居中,水平垂直居中的近乎所有的方案。既能回顧知識查漏補缺,又能提升知識增長見識。CSS 本身就沒有道理,以下內容全是個人蒐集整理,參考資料放在最後。居中的方案只是為了實現居中,不代表每個方案都是最好的解決辦法,因為有些方案還是很離譜的,一般用不上。希望能幫助到你們。
話不多 BB,直接上才藝(程式碼演示)
tips:內容挺多的,順著標題找吧
二、CSS 垂直居中
1、父元素display:table-cell;vertical-align:center
,裡面的子元素就會實現垂直居中,不需要知道子元素的寬高
/* HTML */ <div class='father'> <div class='son'></div> </div> <style> .father { display: table-cell; vertical-align: middle; width: 300px; height: 300px; border: 3px solid red; } .son { width: 50px; height: 50px; background-color: aqua; } </style>
- 效果展示
2、absolute+margin:auto
,定位為 absolute 的元素垂直居中,不需要知道該元素的寬高
<!-- HTMl --> <div class="father"> <div class="son"></div> </div> <style> .father { position: relative; width: 300px; height: 300px; border: 3px solid red; } .son { position: absolute; background-color: aqua; width: 50px; height: 50px; top: 0; bottom: 0; margin: auto; } </style>
- 效果展示
3、absolute+負margin
,定位為 absolute 的元素垂直居中,需要知道該元素的寬高
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
position: relative;
width: 300px;
height: 300px;
border: 3px solid red;
}
.son {
position: absolute;
width: 100px;
height: 100px;
background-color: aqua;
top: 50%;
/* 負margin須是高度的一半 */
margin-top: -50px;
}
</style>
效果展示
4、absolute+calc(css3計算屬性)
,定位為 absolute 的元素垂直居中,需要知道該元素的寬高
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
position: relative;
width: 300px;
height: 300px;
border: 3px solid red;
}
.son {
position: absolute;
width: 100px;
height: 100px;
background-color: aqua;
/* 注意"-"兩邊要隔開 減去的須是高度的一半*/
top: calc(50% - 50px);
}
</style>
效果展示
5、absolute+transform
,定位為 absolute 的元素垂直居中,不需要知道元素的寬高
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
position: relative;
width: 300px;
height: 300px;
border: 3px solid red;
}
.son {
position: absolute;
width: 100px;
height: 100px;
background-color: aqua;
top: 50%;
transform: translateY(-50%);
}
</style>
效果展示
6、line-height
,父元素:line-height=height。子元素:display:inline-block。子元素垂直居中,不需要知道子元素的寬高
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
line-height: 300px;
}
.son {
background-color: aqua;
width: 100px;
height: 100px;
display: inline-block;
vertical-align: middle;
}
</style>
效果展示
7、flex
,目前主流的佈局方案,父元素為 flex 容器且新增 align-items: center,控制子元素的佈局。不需要知道子元素的寬高
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
display: flex;
align-items: center;
}
.son {
background-color: aqua;
width: 100px;
height: 100px;
}
</style>
效果展示
8、grid
,目前最強大的佈局方案,使用還尚未流行。父元素為 grid,子元素新增 align-self: center。不需要知道子元素的寬高
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
display: grid;
}
.son {
background-color: aqua;
width: 100px;
height: 100px;
align-self: center;
}
</style>
效果展示
9、偽元素after或before
,這是我搜出來整理的。CSS 真的太神(s)奇(d)了,毫無道理。子元素垂直居中不需要知道寬高
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
display: block;
}
.father::after {
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}
.son {
background-color: aqua;
width: 50px;
height: 50px;
display: inline-block;
vertical-align: middle;
}
</style>
效果展示
10、隱藏節點(盒子)實現
該原理就是使用盒子佔位置,但不顯示出該盒子。另外的盒子垂直居中,子盒子的寬高需由實際計算時確定
<!-- HTML -->
<div class="father">
<div class="hide"></div>
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
}
.son {
background-color: aqua;
width: 50%;
height: 50%;
}
.hide {
width: 50px;
height: 25%;
}
</style>
效果展示
11、writing-mode
,這是搜尋整理而來,參考資料見最後。子元素盒子 display: inline-block。子元素垂直居中,不需要知道該盒子的寬高
<!-- HTML -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
writing-mode: vertical-lr;
text-align: center;
}
.son {
background-color: aqua;
width: 100px;
height: 100px;
writing-mode: horizontal-tb;
display: inline-block;
}
</style>
效果展示