日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
多個你不知道的CSS居中方案!

水平居中

成都創(chuàng)新互聯(lián)公司長期為1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為孟村企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè),孟村網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

1. 內(nèi)聯(lián)元素

要使內(nèi)聯(lián)元素(如鏈接,span 或img)居中,使用 text-align: center 足夠了。

 
 
 
 
  1.  
  2.     
 
 
 
 
 
  1. .desk { 
  2.   text-align: center; 

對于多個內(nèi)聯(lián)元素,也可以使用text-align:center:

 
 
 
 
  1.  
  2.     
  3.     
 
 
 
 
 
  1. .desk { 
  2.   text-align: center; 

2. Flexbox

使用 flexbox 也可以快速居中元素:

 
 
 
 
  1. .desk { 
  2.   display: flex; 
  3.   justify-content: center; 

對于多個內(nèi)聯(lián)的項目,也可以正常工作。

CSS Grid

使用網(wǎng)格容器時,圖中的盤子將根據(jù)其網(wǎng)格區(qū)域居中。請注意,除非將它們包裹在一個元素中,否則這將不適用于多個盤子。

 
 
 
 
  1. .desk { 
  2.   display: grid; 
  3.   justify-content: center; 

塊元素

1. Auto Margin

寬度和高度已知的塊元素可以通過設(shè)置margin-left:auto 和 margin-right:auto 居中元素。

 
 
 
 
  1. .plate { 
  2.   width: 120px; 
  3.   height: 120px; 
  4.   margin-left: auto; 
  5.   margin-right: auto; 

對于多個塊元素,它們應(yīng)該包裝在一個元素中,然后讓這個父元素居中。

 
 
 
 
  1. .tray { 
  2.   display: flex; 
  3.   margin-left: auto; 
  4.   margin-right: auto; 

2. Flexbox

對于 flexbox 同樣也是使用 justify-content:center 來居中元素:

 
 
 
 
  1. .desk { 
  2.   display: flex; 
  3.   justify-content: center; 

對于多個元素,我們不需要將它們包裹在一個元素中,flexbox 可以將它們都居中。

CSS定位

通過絕對定位,我們可以輕松地通過CSS transform將其水平居中。

 
 
 
 
  1. .plate { 
  2.   position: absolute; 
  3.   left: 50%; 
  4.   transform: translateX(-50%); 

在已知元素寬度的情況下,可以使用負邊距代替CSS transform。

 
 
 
 
  1. .plate { 
  2.   position: absolute; 
  3.   left: 50%; 
  4.   margin-left: -60px; 

垂直居中

1. 內(nèi)聯(lián)元素

Vertical Padding

垂直居中元素最簡單的方法之一是使用padding:

 
 
 
 
  1.   padding-top: 24px; 
  2.   padding-bottom: 24px; 

2. Vertical Align

vertical-align屬性可用于一個或多個元素。

在此示例中,叉子和刀子應(yīng)與桌子垂直居中。

 
 
 
 
  1. .desk { 
  2.   text-align: center; 
  3.  
  4. .plate, 
  5. .fork, 
  6. .knife { 
  7.   vertical-align: middle; 

3. Flexbox

為了對齊盤子,叉子和刀,我們可以使用 flexbox:

 
 
 
 
  1. .desk { 
  2.   display: flex; 
  3.   justify-content: center; 
  4.   align-items: center; 

塊元素

1. 絕對定位

通過絕對定位元素,可以使用 CSS transform將元素垂直居中:

 
 
 
 
  1. .plate { 
  2.   position: absolute; 
  3.   top: 50%; 
  4.   transform: translateY(-50%); 

如果知道元素高度,則可以使用負邊距代替transform。

 
 
 
 
  1. .plate { 
  2.   position: absolute; 
  3.   top: 50%; 
  4.   margin-top: -60px; 

2. CSS Grid

使用CSS網(wǎng)格,我們可以使用align-items將項目垂直于其網(wǎng)格區(qū)域居中。

 
 
 
 
  1. .desk { 
  2.   display: grid; 
  3.   align-items: center; 

水平垂直居中

內(nèi)聯(lián)元素

Padding 和Text Align

 
 
 
 
  1. .plate { 
  2.   text-align: center; 
  3.   padding-top: 24px; 
  4.   padding-bottom: 24px; 

其他元素類型

1. 絕對定位

 
 
 
 
  1. .plate { 
  2.   position: absolute; 
  3.   left: 50%; 
  4.   top: 50%; 
  5.   transform: translate(-50%,-50%); 

2. Flexbox

通過 justify-content:center 和 align-items:center 就可以將元素垂直水平居中:

 
 
 
 
  1. .plate { 
  2.   display: flex; 
  3.   justify-content: center; 
  4.   align-items: center; 

CSS Grid

通過place-items屬性就可以通過,它結(jié)合了justify-content和align-items:

 
 
 
 
  1. .desk { 
  2.   display: grid; 
  3.   place-items: center; 

本文轉(zhuǎn)載自微信公眾號「大遷世界」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系大遷世界公眾號。


當(dāng)前文章:多個你不知道的CSS居中方案!
文章來源:http://www.5511xx.com/article/ccogisp.html