自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

十個CSS實現元素居中的方法匯總

開發(fā) 前端
在今天的文章中,我為大家整理了10個關于實現元素居中的CSS技巧,希望可以幫助你提升CSS技術。

在前端開發(fā)工程師的日常生活中,使用 CSS 使元素居中是很常見的,這也是面試中經常被問到的問題。

也許你已經使用 flex 或 absolute + transform 來實現它,但你知道至少有 10 種方法可以做到元素居中嗎?因此,在今天的文章中,我為大家整理了10個關于實現元素居中的CSS技巧,希望可以幫助你提升CSS技術。

1、absolute + (-margin)

如果元素的寬度和高度已知,我們可以使用至少 3 種方法來使元素居中。例如,在下圖中,小貓的寬度和高度分別為“500px”和“366px”。我們應該如何居中?

用 ‘absolute + (-margin) ` 很容易完成!代碼如下:

HTML

<div class="container">
<img class="cat" src="https://images.unsplash.com/photo-1533743983669-94fa5c4338ec?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1584&q=80" alt="">
</div>

CSS

.container {
width: 800px;
height: 600px;
border: solid 1px #e3c1a9;
border-radius: 30px;
/* Key css */
position: relative;
}
.cat{
width: 500px;
height: 366px;
border-radius: 50%;
position: absolute;
/* Key css */
left: 50%;
top: 50%;
/* half the width */
margin-left: -250px;
/* half the height */
margin-top: -183px;
}

這種方法簡單易懂,兼容性好,缺點是我們需要知道子元素的寬高。

演示地址:https://codepen.io/qianlong/pen/yLKXLxM

2、 absolute + margin auto

我們還可以通過將所有方向的距離設置為 0 ,并將邊距設置為自動來使小貓居中。

CSS代碼如下:

.container {
width: 800px;
height: 600px;
border: solid 1px #e3c1a9;
border-radius: 30px;
position: relative;
}
.cat{
width: 500px;
height: 366px;
border-radius: 50%;
/* Key css */
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}

和第一種方法一樣,它的兼容性也很好,缺點是需要知道子元素的寬高。

演示地址:https://codepen.io/qianlong/pen/RwMgweO

3、absolute + calc

CSS3 帶來了 calc 計算屬性,它允許我們通過它來居中一個元素,代碼如下:

.container {
width: 800px;
height: 600px;
border: solid 1px #e3c1a9;
border-radius: 30px;
position: relative;
}
.cat{
width: 500px;
height: 366px;
border-radius: 50%;
position: absolute;
/* Key css */
top: calc(50% - 183px);
left: calc(50% - 250px);
}

這種方法的兼容性取決于calc的兼容性,缺點是需要知道子元素的寬高。

演示地址:https://codepen.io/qianlong/pen/zYWzYyR

4、flex

以上三種方法必須提前知道元素的寬高,但元素的寬高不確定怎么辦?于是就有了flex ,它非常適合這個。

HTML代碼:

<div class="container">
<span contenteditable="true" class="content">hello medium</span>
</div>

CSS代碼:

.container {
width: 400px;
height: 200px;
border: solid 1px #e3c1a9;
border-radius: 30px;
/* Key css */
display: flex;
align-items: center;
justify-content: center;
}
.content{
padding: 20px;
border-radius: 10px;
background-color: #e3c1a9;
color: #ffffff;
}

這真的很酷,我們可以用很少的代碼來居中一個元素,這是我最喜歡的使用方式。

演示地址:https://codepen.io/qianlong/pen/abYyzvG

5、grid

像 flex 一樣,grid 也可以非常方便地用于使元素居中。

CSS代碼:

.container {
width: 400px;
height: 200px;
border: solid 1px #e3c1a9;
border-radius: 30px;
/* Key css */
display: grid;
}
.content{
/* Key css */
align-self: center;
justify-self: center;
padding: 20px;
border-radius: 10px;
background-color: #e3c1a9;
color: #ffffff;
}

演示地址:https://codepen.io/qianlong/pen/dymzPMa

6、absolute + transform

使用變換,我們還可以在事先不知道元素的寬度和高度的情況下使元素居中。

CSS代碼:

.container {
width: 400px;
height: 200px;
border: solid 1px #e3c1a9;
border-radius: 30px;
/* Key css */
position: relative;
}
.content{
/* Key css */
position: absolute;
left: 50%;
top: 50%;
/* Key css */
transform: translate(-50%, -50%);
padding: 20px;
border-radius: 10px;
background-color: #e3c1a9;
color: #ffffff;
}

演示地址:https://codepen.io/qianlong/pen/KKovwgW

7、text-align + line-height + vertical-align

以上6種方式比較容易理解,在我們的工作中也經常用到,接下來的 4 種方法似乎使用頻率較低,但也值得學習。

首先,我們可以將 span 的“display”屬性設置為“inline-block”。然后通過設置容器的text-align屬性為center,span元素可以水平居中。結合 line-height 和其他屬性使其垂直居中。

CSS代碼:

.container {
width: 400px;
height: 200px;
border: solid 1px #e3c1a9;
border-radius: 30px;
/* Key css */
text-align: center;
line-height: 200px;
font-size: 0px;
}
.content{
font-size: 16px;
/* Key css */
display: inline-block;
vertical-align: middle;
line-height: initial;
text-align: left;
padding: 20px;
border-radius: 10px;
background-color: #e3c1a9;
color: #ffffff;
}

演示地址:https://codepen.io/qianlong/pen/dymzPWL

8、css-table

CSS新的table屬性讓我們可以將普通元素變成表格元素的真實效果,通過這個特性,一個元素也可以居中。

CSS

.container {
width: 400px;
height: 200px;
border: solid 1px #e3c1a9;
border-radius: 30px;
/* Key css */
display: table-cell;
text-align: center;
vertical-align: middle;
}
.content {
/* Key css */
display: inline-block;
padding: 20px;
border-radius: 10px;
background-color: #e3c1a9;
color: #ffffff;
}

演示地址:https://codepen.io/qianlong/pen/dymzPJE

9、writing-mode

過去,我習慣使用 write-mode 將內容的布局方向更改為垂直。

但令人驚奇的是它還可以使元素居中,不過這種方法有點難理解,代碼量會比較多。

HTML代碼:

<div class="container">
<div class="content-wrap">
<span contenteditable="true" class="content">hello medium</span>
</div>
</div>

CSS代碼:

.container {
width: 400px;
height: 200px;
border: solid 1px #e3c1a9;
border-radius: 30px;
/* Key css */
writing-mode: vertical-lr;
text-align: center;
}
.content-wrap{
/* Key css */
writing-mode: horizontal-tb;
display: inline-block;
text-align: center;
width: 100%;
}
.content {
/* Key css */
display: inline-block;
margin: auto;
text-align: left;
padding: 20px;
border-radius: 10px;
background-color: #e3c1a9;
color: #ffffff;
}

演示地址:https://codepen.io/qianlong/pen/vYRJErY

10、table

最后,當然,最后一種方式是最不推薦的方式,但我提到它只是作為學習的一個例子。我不建議你在工作中使用它,因為它(在我看來有點)很糟糕。

HTML代碼:

<table>
<tbody>
<tr>
<td class="container">
<span contenteditable="true" class="content">hello medium</span>
</td>
</tr>
</tbody>
</table>

CSS代碼:

.container {
width: 400px;
height: 200px;
border: solid 1px #e3c1a9;
border-radius: 30px;
/* Key css */
text-align: center;
}
.content {
/* Key css */
display: inline-block;
padding: 20px;
border-radius: 10px;
background-color: #e3c1a9;
color: #ffffff;
}

演示地址:https://codepen.io/qianlong/pen/yLKoyqv

寫在最后

以上就是我今天與你分享的10個關于CSS實現元素居中的小技巧,希望你能從中學到你想要的知識,如果你覺得它有用的話,請記得點贊我,關注我,并將它分享給你身邊做開發(fā)的朋友,也許能夠幫助到他。

最后感謝你的閱讀,祝編程愉快,我們明天見。

責任編輯:龐桂玉 來源: web前端開發(fā)
相關推薦

2022-09-15 10:30:06

CSS

2010-09-08 14:35:22

CSS

2022-12-20 15:17:29

CSS開發(fā)

2021-07-26 08:31:17

算法

2022-08-22 11:34:42

開源數據

2010-09-06 14:19:54

CSS

2024-08-26 14:57:36

2023-06-29 15:10:48

Web開發(fā)CSS

2011-08-15 09:15:09

私有云云計算

2023-09-21 15:10:55

2010-09-03 14:57:33

CSS樣式表CSS

2010-08-31 15:07:45

CSS居中

2023-10-29 17:12:26

Python編程

2023-09-06 07:22:48

控制臺UI工具

2023-04-03 06:38:41

2024-07-24 11:40:33

2023-08-29 07:52:09

CSS庫網絡動畫

2024-01-07 20:14:18

CSS開發(fā)工具

2023-06-29 15:41:40

CSSWeb 開發(fā)

2010-09-03 15:59:19

DIV CSS
點贊
收藏

51CTO技術棧公眾號