<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 實(shí)現(xiàn)元素水平垂直居中的 N 種方式

          共 8153字,需瀏覽 17分鐘

           ·

          2021-08-13 16:13

          點(diǎn)擊上方 三分鐘學(xué)前端,關(guān)注公眾號(hào)

          回復(fù)交流,加入前端編程面試算法每日一題群


          面試官也在看的前端面試資料

          水平居中

          1. 行內(nèi)元素

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

          <div class="parent">
            <span class="child">測試</span>
          </div>
          <style>
            .parent {
              background-color: aqua;
              text-align: center; /* 水平居中 */
            }
            .child {
              color#fff;
              background-color: blueviolet;
            }
          </style>

          2. 塊級(jí)元素

          2.1 寬高固定

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

          <div class="parent">
            <div class="child"></div>
          </div>
          <style>
            .parent {
              height100px;
              background-color: aqua;
            }
            .child {
              width100px;
              height100px;
              background-color: blueviolet;
            }
          </style>

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

          方式一:margin:0 auto
          <style>
            .child {
              margin:0 auto;
            }
          </style>
          方式二:absolute + margin-left
          <style>
            .child {
              position: absolute;
              margin-left: -50px;
              left50%;
            }
          </style>
          方式三:absolute + calc
          <style>
            .child {
              position: absolute;
              leftcalc(50% - 50px);
            }
          </style>
          方式四:absolute + left + right + margin-left
          <style>
            .child {
              position: absolute;
              left0;
              right0;
              margin0 auto;
            }
          </style>

          2.2 寬高不定

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

          <div class="parent">
            <div class="child">測試示例</div>
          </div>
          <style>
            .parent {
              height100px;
              background-color: aqua;
            }
            .child {
              background-color: blueviolet;
              color#fff;
            }
          </style>

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

          方式一:使用 CSS3 中新增的 transform 屬性
          <style>
            .child {
              position: absolute;
              left50%;
              transform:translate(-50%0);
            }
          </style>
          方式二:flex 布局
          <style>
            .child {
              display: flex;
              justify-content: center;
            }
          </style>
          方式三:width: fit-content
          <style>
            .child {
              width: fit-content;
              margin0 auto;
            }
          </style>

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

          垂直居中

          1. 行內(nèi)元素

          代碼示例:

          <div class="parent">
            <span class="child">測試示例</span>
          </div>
          <style>
            .parent {
              height100px;
              background-color: aqua;
              text-align: center; /* 水平居中 */
            }
            .child {
              color#fff;
              background-color: blueviolet;
            }
          </style>

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

          <style>
            .child {
              line-height100px;
            }
          </style>

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

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

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

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

          <style>
            .parent {
              display: table;
            }
            .child {
              display: table-cell;
              vertical-align: middle;
            }
          </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; 居中

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

          方式四:display: grid 布局

          <style>
            .parent {
              display: grid;
            }
            .child {
              margin: auto;
            }
          </style>

          效果如上

          方式五:writing-mode 布局

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

          <style>
            .parent {
              writing-mode: vertical-lr;
            }
            .child {
              writing-mode: horizontal-tb;
            }
          </style>

          效果如上

          2. 塊級(jí)元素

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

          2.1 寬高固定

          示例代碼:

          <div class="parent">
            <div class="child"></div>
          </div>
          <style>
            body {
              background-color: aqua;
            }
            .child {
              width50px;
              height50px;
              background-color: blueviolet;
            }
          </style>

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

          方式一:absolute + margin-top
          <style>
            .child {
              position: absolute;
              margin-left: -25px;
              left50%;
              margin-top: -25px;
              top50%;
            }
          </style>
          方式二:absolute + calc
          <style>
            .child {
              position: absolute;
              leftcalc(50% - 25px);
              topcalc(50% - 25px);
            }
          </style>
          方式三:absolute + left + right + top + bottom
          <style>
            .child {
              position: absolute;
              left0;
              right0;
              top0;
              bottom0;
              margin: auto;
            }
          </style>

          2.2 寬高不定

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

          <div class="parent">
            <div class="child">測試示例</div>
          </div>
          <style>
            .parent {
              height100px;
              background-color: aqua;
            }
            .child {
              background-color: blueviolet;
            }
          </style>

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

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

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

          <style>
            .parent {
              position: relative;
            }
            .child {
              position: absolute;
              left50%;
              top50px;
              transformtranslate(-50%, -50%);
            }
          </style>
          方式二:flex 布局
          <style>
            .parent {
              display: flex;
              height100%;
              justify-content: center;  /* 水平居中 */
              align-items: center; /* 垂直居中 */
            }
          </style>

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

          最后

          歡迎關(guān)注「三分鐘學(xué)前端」,回復(fù)「交流」自動(dòng)加入前端三分鐘進(jìn)階群,每日一道編程算法面試題(含解答),助力你成為更優(yōu)秀的前端開發(fā)!

          號(hào)內(nèi)回復(fù):

          網(wǎng)絡(luò)」,自動(dòng)獲取三分鐘學(xué)前端網(wǎng)絡(luò)篇小書(90+頁)
          JS」,自動(dòng)獲取三分鐘學(xué)前端 JS 篇小書(120+頁)
          算法」,自動(dòng)獲取 github 2.9k+ 的前端算法小書
          面試」,自動(dòng)獲取 github 23.2k+ 的前端面試小書
          簡歷」,自動(dòng)獲取程序員系列的 120 套模版
          》》面試官也在看的前端面試資料《《
          “在看和轉(zhuǎn)發(fā)”就是最大的


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

          手機(jī)掃一掃分享

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

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <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>
                  成人导航网站 | 久久r| 国产精品一区二区人成电影网 | 国产成人av 高清在线 | 在线一视频 |