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

          共 8653字,需瀏覽 18分鐘

           ·

          2021-08-10 01:46


          點擊上方 三分鐘學前端,關(guān)注公眾號

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


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

          水平居中

          1. 行內(nèi)元素

          若是行內(nèi)元素,給其父元素設(shè)置 text-align:center 即可實現(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. 塊級元素

          2.1 寬高固定

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

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

          <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 屬性新加的一個屬性值,它配合 margin 可以輕松實現(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>

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

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

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

          為了使用 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é)點

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

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

          參照 水平居中的塊級元素布局 ,同樣把對水平方向的轉(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 寬高不定

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

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

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

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

          需要設(shè)定 parent 為相對定位( 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)注「三分鐘學前端」,回復「交流」自動加入前端三分鐘進階群,每日一道編程算法面試題(含解答),助力你成為更優(yōu)秀的前端開發(fā)!

          號內(nèi)回復:

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


          瀏覽 26
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

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

          手機掃一掃分享

          分享
          舉報
          <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>
                  国产se视频 | 日韩欧美91 | www.青青草原视频国产 | 2019中文字幕在线视频 | 精品国产视频在线观看 |