1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

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

        共 7603字,需瀏覽 16分鐘

         ·

        2022-07-04 22:19

        大廠技術(shù)  高級前端  Node進階

        點擊上方 程序員成長指北,關(guān)注公眾號

        回復(fù)1,加入高級Node交流群

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

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

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

        css 垂直居中.png

        居中元素寬高已知

        1. absolute + margin auto

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

        注意使用此方法:父元素與當(dāng)前元素的高度要設(shè)置;

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

        具體代碼如下:

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

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

        復(fù)制代碼

        具體效果如下:

        image-20210729232149048.png

        2. absolute + 負 margin

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

        然后再用負的 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;
        }

        復(fù)制代碼

        具體效果如下:

        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);
        }

        復(fù)制代碼

        具體效果如下:

        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%);
        }
        復(fù)制代碼

        具體效果如下:

        image-20210729232624466.png

        5. line-height + vertical-align

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

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

        最后設(shè)置當(dāng)前元素的 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;
        }
        復(fù)制代碼

        具體效果如下:

        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>
        復(fù)制代碼

        具體效果如下:

        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;
        }
        復(fù)制代碼

        具體效果如下:

        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;
        }
        復(fù)制代碼

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

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

        具體效果如下:

        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;
        }
        復(fù)制代碼

        具體效果如下:

        image-20210729233243684.png

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

        附贈 flex 兼容性圖片一張

        image-20210725020146492.png

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

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

        不熟悉的可以看下阮一峰老師的詳細入門教程[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;  
        }
        復(fù)制代碼

        具體效果如下:

        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;
        }
        復(fù)制代碼

        具體效果如下:

        image-20210729233458535.png

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

        附贈 grid 兼容性圖片一張

        image-20210725020025034.png

        總結(jié)

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

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

        寫在最后

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

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

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

        參考資料

        [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

        關(guān)于本文

        來自:易師傅

        https://juejin.cn/post/6991465721565806605


        Node 社群



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



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

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

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

        瀏覽 54
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

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

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            久久伦理 | 奶罩不戴乳罩邻居hd播放 | 国产黄在线视频 | 久久99操| 亚洲欧美日韩成人高清在线一区 | 2023国产精品视频 | 日本二区三区 | 亚洲 简体 自由 管 | 韩国伦理一区二区 | 一类黄色大片 |