偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

CSS 實(shí)現(xiàn)元素水平垂直居中的 N 種方式

開發(fā) 前端
可用 vertical-align 屬性, 而 vertical-align 只有在父層為 td 或者 th 時(shí), 才會(huì)生效,對(duì)于其他塊級(jí)元素, 例如 div 、 p 等,默認(rèn)情況是不支持的。

[[413004]]

本文轉(zhuǎn)載自微信公眾號(hào)「三分鐘學(xué)前端」,作者sisterAn 。轉(zhuǎn)載本文請(qǐng)聯(lián)系三分鐘學(xué)前端公眾號(hào)。

水平居中

1. 行內(nèi)元素

若是行內(nèi)元素,給其父元素設(shè)置 text-align:center 即可實(shí)現(xiàn)行內(nèi)元素水平居中

  1. <div class="parent"
  2.   <span class="child">測(cè)試</span> 
  3. </div> 
  4. <style> 
  5.   .parent { 
  6.     background-color: aqua; 
  7.     text-align: center; /* 水平居中 */ 
  8.   } 
  9.   .child { 
  10.     color: #fff; 
  11.     background-color: blueviolet; 
  12.   } 
  13. </style> 

2. 塊級(jí)元素

2.1 寬高固定

當(dāng)寬高固定時(shí),以下 html 示例:

  1. <div class="parent"
  2.   <div class="child"></div> 
  3. </div> 
  4. <style> 
  5.   .parent { 
  6.     height: 100px; 
  7.     background-color: aqua; 
  8.   } 
  9.   .child { 
  10.     width: 100px; 
  11.     height: 100px; 
  12.     background-color: blueviolet; 
  13.   } 
  14. </style> 

以下四種方式顯示效果均為:

方式一:margin:0 auto

  1. <style> 
  2.   .child { 
  3.     margin:0 auto; 
  4.   } 
  5. </style> 

方式二:absolute + margin-left

  1. <style> 
  2.   .child { 
  3.     position: absolute
  4.     margin-left: -50px; 
  5.     left: 50%; 
  6.   } 
  7. </style> 

方式三:absolute + calc

  1. <style> 
  2.   .child { 
  3.     position: absolute
  4.     left: calc(50% - 50px); 
  5.   } 
  6. </style> 

方式四:absolute + left + right + margin-left

  1. <style> 
  2.   .child { 
  3.     position: absolute
  4.     left: 0; 
  5.     right: 0; 
  6.     margin: 0 auto; 
  7.   } 
  8. </style> 

2.2 寬高不定

當(dāng)寬高不定時(shí),以下測(cè)試示例:

  1. <div class="parent"
  2.   <div class="child">測(cè)試示例</div> 
  3. </div> 
  4. <style> 
  5.   .parent { 
  6.     height: 100px; 
  7.     background-color: aqua; 
  8.   } 
  9.   .child { 
  10.     background-color: blueviolet; 
  11.     color: #fff; 
  12.   } 
  13. </style> 

以下三種方式顯示效果均為:

方式一:使用 CSS3 中新增的 transform 屬性

  1. <style> 
  2.   .child { 
  3.     position: absolute
  4.     left: 50%; 
  5.     transform:translate(-50%, 0); 
  6.   } 
  7. </style> 

方式二:flex 布局

  1. <style> 
  2.   .child { 
  3.     display: flex; 
  4.     justify-content: center; 
  5.   } 
  6. </style> 

方式三:width: fit-content

  1. <style> 
  2.   .child { 
  3.     width: fit-content; 
  4.     margin: 0 auto; 
  5.   } 
  6. </style> 

fit-content 是 CSS3中 給 width 屬性新加的一個(gè)屬性值,它配合 margin 可以輕松實(shí)現(xiàn)水平居中

垂直居中

1. 行內(nèi)元素

代碼示例:

  1. <div class="parent"
  2.   <span class="child">測(cè)試示例</span> 
  3. </div> 
  4. <style> 
  5.   .parent { 
  6.     height: 100px; 
  7.     background-color: aqua; 
  8.     text-align: center; /* 水平居中 */ 
  9.   } 
  10.   .child { 
  11.     color: #fff; 
  12.     background-color: blueviolet; 
  13.   } 
  14. </style> 

方式一:line-height(單行文本)

  1. <style> 
  2.   .child { 
  3.     line-height: 100px; 
  4.   } 
  5. </style> 

當(dāng)多行時(shí)會(huì)樣式錯(cuò)亂,需要用到 vertical-align: middle 布局

方式二:display: table-cell + vertical-align (多行文本)

可用 vertical-align 屬性, 而 vertical-align 只有在父層為 td 或者 th 時(shí), 才會(huì)生效,對(duì)于其他塊級(jí)元素, 例如 div 、 p 等,默認(rèn)情況是不支持的。

為了使用 vertical-align ,我們需要設(shè)置父元素 display:table , 子元素 display:table-cell; vertical-align:middle;

  1. <style> 
  2.   .parent { 
  3.     display: table
  4.   } 
  5.   .child { 
  6.     display: table-cell; 
  7.     vertical-align: middle; 
  8.   } 
  9. </style> 

方式三:display: inline-block + vertical-align 隱式幽靈節(jié)點(diǎn)

設(shè)置幽靈節(jié)點(diǎn)的高度以及幽靈節(jié)點(diǎn)的基線(通過 line-height ),來設(shè)置幽靈節(jié)點(diǎn)的 x-height , 是 span 的中線與幽靈節(jié)點(diǎn)的中線對(duì)齊,同樣也可以使 vertical-align: middle; 居中

  1. <style> 
  2.   .parent { 
  3.     line-height: 100px; /* 通過 line-height 設(shè)置幽靈節(jié)點(diǎn)的基線 */ 
  4.   } 
  5.   .child { 
  6.     vertical-align: middle; 
  7.     line-height: normal; /* 塊級(jí)元素時(shí)需要加 */ 
  8.     display: inline-block; /* 重點(diǎn),要把 line-height 設(shè)置成 normal, 要不然會(huì)繼承100 */ 
  9.   } 
  10. </style> 

方式四:display: grid 布局

  1. <style> 
  2.   .parent { 
  3.     display: grid; 
  4.   } 
  5.   .child { 
  6.     margin: auto; 
  7.   } 
  8. </style> 

效果如上

方式五:writing-mode 布局

writing-mode 屬性定義了文本在水平或垂直方向上如何排布。

  1. <style> 
  2.   .parent { 
  3.     writing-mode: vertical-lr; 
  4.   } 
  5.   .child { 
  6.     writing-mode: horizontal-tb; 
  7.   } 
  8. </style> 

效果如上

2. 塊級(jí)元素

參照 水平居中的塊級(jí)元素布局 ,同樣把對(duì)水平方向的轉(zhuǎn)換為垂直方向的

2.1 寬高固定

示例代碼:

  1. <div class="parent"
  2.   <div class="child"></div> 
  3. </div> 
  4. <style> 
  5.   body { 
  6.     background-color: aqua; 
  7.   } 
  8.   .child { 
  9.     width: 50px; 
  10.     height: 50px; 
  11.     background-color: blueviolet; 
  12.   } 
  13. </style> 

以下幾種方式顯示效果均為:

方式一:absolute + margin-top

  1. <style> 
  2.   .child { 
  3.     position: absolute
  4.     margin-left: -25px; 
  5.     left: 50%; 
  6.     margin-top: -25px; 
  7.     top: 50%; 
  8.   } 
  9. </style> 

方式二:absolute + calc

  1. <style> 
  2.   .child { 
  3.     position: absolute
  4.     left: calc(50% - 25px); 
  5.     top: calc(50% - 25px); 
  6.   } 
  7. </style> 

方式三:absolute + left + right + top + bottom

  1. <style> 
  2.   .child { 
  3.     position: absolute
  4.     left: 0; 
  5.     right: 0; 
  6.     top: 0; 
  7.     bottom: 0; 
  8.     margin: auto; 
  9.   } 
  10. </style> 

2.2 寬高不定

當(dāng)寬高不定時(shí),以下測(cè)試示例:

  1. <div class="parent"
  2.   <div class="child">測(cè)試示例</div> 
  3. </div> 
  4. <style> 
  5.   .parent { 
  6.     height: 100px; 
  7.     background-color: aqua; 
  8.   } 
  9.   .child { 
  10.     background-color: blueviolet; 
  11.   } 
  12. </style> 

以下兩種方式顯示效果均為:

方式一:使用 CSS3 中新增的 transform 屬性

需要設(shè)定 parent 為相對(duì)定位( position: relative )

  1. <style> 
  2.   .parent { 
  3.     position: relative
  4.   } 
  5.   .child { 
  6.     position: absolute
  7.     left: 50%; 
  8.     top: 50px; 
  9.     transform: translate(-50%, -50%); 
  10.   } 
  11. </style> 

方式二:flex 布局

來源:https://github.com/Advanced-Frontend/Daily-Interview-Question

 

責(zé)任編輯:武曉燕 來源: 三分鐘學(xué)前端
相關(guān)推薦

2018-09-18 11:20:07

css html5javascript

2010-09-01 10:49:57

CSS水平居中垂直居中

2010-08-24 14:47:48

CSS居中

2010-08-27 10:30:16

CSS垂直居中

2010-08-26 09:27:07

CSS居中

2010-08-24 14:23:39

DIV居中

2010-08-31 14:49:57

CSS居中

2023-03-06 09:20:33

CSS顏色混合

2022-12-20 15:17:29

CSS開發(fā)

2010-09-09 10:15:35

DIVCSS

2021-11-02 07:44:36

CSS 技巧進(jìn)度條

2010-09-09 10:23:23

DIVCSS垂直居中

2010-09-02 13:03:38

CSS垂直居中

2022-06-22 09:06:54

CSS垂直居中代碼

2022-07-28 13:01:35

CSS前端元素居中

2010-08-26 11:27:35

CSS居中

2012-06-20 13:46:23

CSS

2024-09-10 21:11:55

2010-09-02 13:16:44

CSS水平居中

2023-12-04 09:31:13

CSS卡片
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)