<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          CSS 垂直居中的正確打開方式

          共 7603字,需瀏覽 16分鐘

           ·

          2022-07-04 22:19

          大廠技術  高級前端  Node進階

          點擊上方 程序員成長指北,關注公眾號

          回復1,加入高級Node交流群

          前言之爆錘面試官神器 - CSS

          無論是實際開發(fā)中,亦或者是求職面試中,css 垂直居中往往都是一個繞不開的話題,其中不乏有許多面試者在多次雙重嘗受打擊之后,而沒有一個很好的反擊點,剛好結合自己以前受的委屈和痛苦,來給大家一個錘爆面試官大佬們的機會。

          其實垂直居中主要分為了兩種類型:居中元素寬高已知居中元素寬高未知,那么我們就結合這兩種類型來一一做舉例。

          css 垂直居中.png

          居中元素寬高已知

          1. absolute + margin auto

          顧名思義,就是利用當前元素的 position: absolute;margin: auto;

          注意使用此方法:父元素與當前元素的高度要設置;

          通過將各個方向的距離都設置為 0,此時將 margin 設置為 auto,就可以實現(xiàn)垂直居中顯示了;

          具體代碼如下:

          .parent{
            position: relative;
            width90vw;
            height90vh;
            border3px solid steelblue;
          }

          .child{
            background: tomato;
            width50vwheight50vh;
            /* 核心代碼 */
            position:absolute;
            top0bottom0left0right0;
            margin: auto;
          }

          復制代碼

          具體效果如下:

          image-20210729232149048.png

          2. absolute + 負 margin

          利用絕對定位百分比 50% 來實現(xiàn),因為當前元素的百分比是基于相對定位(也就是父元素)來定位的;

          然后再用負的 margin-top 和 margin-left 來進行簡單的位移即可,因為現(xiàn)在的負 margin 是基于自身的高度和寬度來進行位移的。

          具體代碼如下:

          .parent{
            position:relative;
            width90vw;
            height90vh;
            border3px solid steelblue;
          }

          .child{
            background: tomato;
            width100pxheight100px;
            /* 核心代碼 */
            position:absolute;
            top50%left50%;
            margin-top: -50px;
            margin-left: -50px;
          }

          復制代碼

          具體效果如下:

          image-20210729232317142.png

          3. absolute + calc

          使用 CSS3 的一個計算函數(shù)來進行計算即可;方法與上面類似

          具體代碼如下:

          .parent{
            width90vw;
            height90vh;
            border3px solid steelblue;
            /* 核心代碼 */
            position:relative;
          }

          .child{
            background: tomato;
            width200pxheight200px;
            /* 核心代碼 */
            position:absolute;
            topcalc(50% - 100px);
            leftcalc(50% - 100px);
          }

          復制代碼

          具體效果如下:

          image-20210729232434257.png

          居中元素寬高未知

          4. absolute + transform

          利用 CSS3 的新特性 transform;因為 transformtranslate 屬性值如果是一個百分比,那么這個百分比將是基于自身的寬高計算出來的。

          具體代碼如下:

          .parent{
            width90vw;
            height90vh;
            border3px solid steelblue;
            /* 核心代碼 */
            position:relative;
          }

          .child{
            background: tomato;
            /* 核心代碼 */
            position:absolute;
            top50%;
            left50%;
            transformtranslate(-50%, -50%);
          }
          復制代碼

          具體效果如下:

          image-20210729232624466.png

          5. line-height + vertical-align

          把當前元素設置為行內元素,然后通過設置父元素的 text-align: center; 實現(xiàn)水平居中;

          同時通過設置當前元素的 vertical-align: middle; 來實現(xiàn)垂直居中;

          最后設置當前元素的 line-height: initial; 來繼承父元素的line-height

          具體代碼如下:

          .parent{
            width90vw;
            border3px solid steelblue;
            /* 核心代碼 */
            line-height500px;
            text-align: center;
          }

          .child{
            background: tomato;
            /* 核心代碼 */
            display: inline-block;
            vertical-align: middle;
            line-height: initial;
          }
          復制代碼

          具體效果如下:

          image-20210729232748854.png

          6. table 表格元素

          通過最經(jīng)典的 table 元素來進行水平垂直居中,不過代碼看起來會很冗余,不推薦使用;

          具體代碼如下:

          <table>
            <tbody>
              <tr>
                <td class="parent">
                  <div class="child"></div>
                </td>
              </tr>
            </tbody>
          </table>

          <style>
            .parent {
              width90vw;
              height90vh;
              border3px solid steelblue;
              /* 核心代碼 */
              text-align: center;
            }
            .child {
              background: tomato;
              /* 核心代碼 */
              display: inline-block;
            }
          </style>
          復制代碼

          具體效果如下:

          image-20210729233002861.png

          7. css-table 表格樣式

          如果一定要使用 table 的特性,但是不想寫 table 元素的話,那么css-table 就很適合你;

          具體代碼如下:

          .parent{
            width90vw;
            height90vh;
            border3px solid steelblue;
            /* 核心代碼 */
            display: table-cell;
            text-align: center;
            vertical-align: middle;
          }

          .child{
            background: tomato;
            /* 核心代碼 */
            display: inline-block;
          }
          復制代碼

          具體效果如下:

          image-20210729233055305.png

          8. flex 布局(一)

          要說現(xiàn)在較為流行和使用較多的布局方案,那么非 flex 莫屬了,那么舉例兩個最常見的使用方式 ~

          直接在 flex-container 上通過幾行代碼即可很優(yōu)雅的實現(xiàn)

          具體代碼如下:

          .parent {
            width90vw;
            height90vh;
            border3px solid steelblue;
            
            /* 核心代碼 */
            display: flex;
            justify-content: center;
           align-items: center;
          }
          .child{
            background: tomato;
          }
          復制代碼

          justify-content 表示:設置或檢索彈性盒子元素在主軸(橫軸)方向上的對齊方式;

          align-items 表示:定義 flex 子項在 flex 容器的當前行的側軸(縱軸)方向上的對齊方式。

          具體效果如下:

          image-20210729233155581.png

          9. flex + margin auto(二)

          flex-item 上更加優(yōu)雅的實現(xiàn)

          具體代碼如下:

          .parent{
            width90vw;
            height90vh;
            border3px solid steelblue;
            
            /* 核心代碼 */
            display: flex;
          }

          .child{
            background: tomato;
            
            /* 核心代碼 */
            margin: auto;
          }
          復制代碼

          具體效果如下:

          image-20210729233243684.png

          flex 的兩種方法使用取舍,任憑您意

          附贈 flex 兼容性圖片一張

          image-20210725020146492.png

          10. grid 網(wǎng)格布局 (一)

          grid 布局相信大家在實際項目中用的較少,主要是該布局實在是太超前,導致了兼容性不是那么理想,但是不可否認的是 grid 的能力在 css 布局中絕對是一個質的飛越。

          不熟悉的可以看下阮一峰老師的詳細入門教程[1]

          CSS Grid 包含與 Flexbox 幾乎相同的對齊選項,因此我們可以在 grid-container 上優(yōu)雅的實現(xiàn)

          具體代碼如下:

          .parent{
            width90vw;
            height90vh;
            border3px solid steelblue;
            /* 核心代碼 */
            display: grid;
            align-items: center;
            justify-content: center;
          }

          .child{
            background: tomato;  
          }
          復制代碼

          具體效果如下:

          image-20210729233416689.png

          11. grid 網(wǎng)格布局 (二)

          同樣我們可以像 Flexbox 一樣,在 grid-item 上優(yōu)雅的實現(xiàn)

          具體代碼如下:

          .parent{
            width90vw;
            height90vh;
            border3px solid steelblue;
            /* 核心代碼 */
            display: grid
          }

          .child{
            background: tomato;
            /* 核心代碼 */
            align-self: center;
            justify-self: center;
          }
          復制代碼

          具體效果如下:

          image-20210729233458535.png

          flex 的兩種方法使用取舍,任憑您高興

          附贈 grid 兼容性圖片一張

          image-20210725020025034.png

          總結

          在學習了上面的 11 種垂直居中布局方法后,我們簡單概括一下

          • 如果你的項目在 PC 端有兼容性要求并且寬高固定,推薦使用 absolute + 負 margin 方法實現(xiàn);
          • 如果你的項目在 PC 端有兼容性要求并且寬高不固定,推薦使用 css-table 方式實現(xiàn);
          • 如果你的項目在 PC 端無兼容性要求 ,推薦使用 flex 實現(xiàn)居中,當然不考慮 IE 的話,grid 也是個不錯的選擇;
          • 如果你的項目在移動端使用 ,那么推薦你使用 flexgrid 也可作為備選。

          寫在最后

          其實以上的是一種垂直居中方法,只是比較常見的,其實還有一些比較冷門的方式方法,例如 偽類元素grid-container 的 grid-template-rows 等,大家下去可以自行嘗試一下 ~

          周末已結束,周一還會遠嗎,祝大家新的一周新的開始 ~

          帥氣美麗的你給個贊唄再走唄 ~

          參考資料

          [1]

          https://www.ruanyifeng.com/blog/2019/03/grid-layout-tutorial.html: https://link.juejin.cn?target=https%3A%2F%2Fwww.ruanyifeng.com%2Fblog%2F2019%2F03%2Fgrid-layout-tutorial.html

          關于本文

          來自:易師傅

          https://juejin.cn/post/6991465721565806605


          Node 社群



          我組建了一個氛圍特別好的 Node.js 社群,里面有很多 Node.js小伙伴,如果你對Node.js學習感興趣的話(后續(xù)有計劃也可以),我們可以一起進行Node.js相關的交流、學習、共建。下方加 考拉 好友回復「Node」即可。



          如果你覺得這篇內容對你有幫助,我想請你幫我2個小忙:

          1. 點個「在看」,讓更多人也能看到這篇文章
          2. 訂閱官方博客 www.inode.club 讓我們一起成長

          點贊和在看就是最大的支持

          瀏覽 54
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  熟女日本在线 | 成人黄色电影免费在线观看 | 淫色无限一区二区 | 夜撸日撸香蕉视频 | 超碰国产操逼 |