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 垂直居中的正確打開(kāi)方式

        共 7332字,需瀏覽 15分鐘

         ·

        2022-06-29 20:37

        web-css-gaoji-jichu.jpeg

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

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

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

        css 垂直居中.png

        居中元素寬高已知

        1. absolute + margin auto

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

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

        通過(guò)將各個(gè)方向的距離都設(shè)置為 0,此時(shí)將 margin 設(shè)置為 auto,就可以實(shí)現(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 + 負(fù) margin

        利用絕對(duì)定位百分比 50% 來(lái)實(shí)現(xiàn),因?yàn)楫?dāng)前元素的百分比是基于相對(duì)定位(也就是父元素)來(lái)定位的;

        然后再用負(fù)的 margin-top 和 margin-left 來(lái)進(jìn)行簡(jiǎn)單的位移即可,因?yàn)楝F(xiàn)在的負(fù) margin 是基于自身的高度和寬度來(lái)進(jìn)行位移的。

        具體代碼如下:

        .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 的一個(gè)計(jì)算函數(shù)來(lái)進(jìn)行計(jì)算即可;方法與上面類似

        具體代碼如下:

        .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;因?yàn)?transformtranslate 屬性值如果是一個(gè)百分比,那么這個(gè)百分比將是基于自身的寬高計(jì)算出來(lái)的。

        具體代碼如下:

        .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)元素,然后通過(guò)設(shè)置父元素的 text-align: center; 實(shí)現(xiàn)水平居中;

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

        最后設(shè)置當(dāng)前元素的 line-height: initial; 來(lái)繼承父元素的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 表格元素

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

        具體代碼如下:

        <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 的特性,但是不想寫(xiě) 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 布局(一)

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

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

        具體代碼如下:

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

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

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

        具體效果如下:

        image-20210729233155581.png

        9. flex + margin auto(二)

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

        具體代碼如下:

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

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

        具體效果如下:

        image-20210729233243684.png

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

        附贈(zèng) flex 兼容性圖片一張

        image-20210725020146492.png

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

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

        不熟悉的可以看下阮一峰老師的詳細(xì)入門(mén)教程[1]

        CSS Grid 包含與 Flexbox 幾乎相同的對(duì)齊選項(xiàng),因此我們可以在 grid-container 上優(yōu)雅的實(shí)現(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)雅的實(shí)現(xiàn)

        具體代碼如下:

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

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

        具體效果如下:

        image-20210729233458535.png

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

        附贈(zèng) grid 兼容性圖片一張

        image-20210725020025034.png

        總結(jié)

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

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

        寫(xiě)在最后

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

        周末已結(jié)束,周一還會(huì)遠(yuǎn)嗎,祝大家新的一周新的開(kāi)始 ~

        帥氣美麗的你給個(gè)贊唄再走唄 ~

        參考資料

        [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)于本文

        來(lái)自:易師傅

        https://juejin.cn/post/6991465721565806605


        The End

        瀏覽 50
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        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>
            日本色色网 | 年轻的老师大胸4 | 超嫩俩小younv合集 | 很黄很污的网站 | 插妹子综合网 | 国产男生操女生 | 国产免费福利小视频 | 欧美视频日韩视频 | 国产日韩久久免费福利网站 | 国产精品色情A级毛片 |