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

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

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

[[413004]]

本文轉載自微信公眾號「三分鐘學前端」,作者sisterAn 。轉載本文請聯(lián)系三分鐘學前端公眾號。

水平居中

1. 行內元素

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

  1. <div class="parent"
  2.   <span class="child">測試</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. 塊級元素

2.1 寬高固定

當寬高固定時,以下 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 寬高不定

當寬高不定時,以下測試示例:

  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.     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 屬性新加的一個屬性值,它配合 margin 可以輕松實現(xiàn)水平居中

垂直居中

1. 行內元素

代碼示例:

  1. <div class="parent"
  2.   <span class="child">測試示例</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> 

當多行時會樣式錯亂,需要用到 vertical-align: middle 布局

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

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

為了使用 vertical-align ,我們需要設置父元素 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é)點

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

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

參照 水平居中的塊級元素布局 ,同樣把對水平方向的轉換為垂直方向的

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 寬高不定

當寬高不定時,以下測試示例:

  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.     background-color: blueviolet; 
  11.   } 
  12. </style> 

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

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

需要設定 parent 為相對定位( 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

 

責任編輯:武曉燕 來源: 三分鐘學前端
相關推薦

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 技巧進度條

2010-09-02 13:03:38

CSS垂直居中

2010-09-09 10:23:23

DIVCSS垂直居中

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

2010-09-02 13:16:44

CSS水平居中

2024-09-10 21:11:55

2023-12-04 09:31:13

CSS卡片
點贊
收藏

51CTO技術棧公眾號