整合營銷服務(wù)商

          電腦端+手機(jī)端+微信端=數(shù)據(jù)同步管理

          免費(fèi)咨詢熱線:

          css篇-前端面試中常問的div居中方法

          上又要到秋招的時(shí)候了,又有不少人打算換工作了。前端在面試中總會(huì)被問到的一道基礎(chǔ)題div居中方法,這里給大家總結(jié)一下都有哪些常用的方法。

          絕對(duì)定位

          • 父級(jí)元素(parent)采用相對(duì)定位(relative),需要居中的元素(demo)采用絕對(duì)定位(absolute)。
          • 居中元素向上偏移50%,向左偏移50%,此時(shí)左頂點(diǎn)位于中心,不過我們需要的是整體居中,所以在偏移自身一半的寬高。(如下圖)

          還未偏移一半自身寬高

          <style>
              .parent {
                position: relative;
                width: 500px;
                height: 500px;
                border: solid red 1px;
              }
              .demo {
                position: absolute;
                width: 100px;
                height: 100px;
                border: solid blue 1px;
                top: 50%;
                left: 50%;
                margin-top: -50px;
                margin-left: -50px;
              }
            </style>
            <body>
              <div class="parent">
                <div class="demo"></div>
              </div>
            </body>

          彈性方法居中

          通過flex彈性布局設(shè)置垂直居中和水平居中

            <style>
              .parent {
                width: 500px;
                height: 500px;
                border: solid red 1px;
                display: flex;
                // 垂直,水平居中
                align-items: center;
                justify-content: center;
              }
              .demo {
                width: 100px;
                height: 100px;
                border: solid blue 1px;
              }
            </style>
            <body>
              <div class="parent">
                <div class="demo"></div>
              </div>
            </body>

          使用transform居中

          在子元素不知道自身寬高情況,使用transform進(jìn)行比偏移。

            <style>
              .parent {
                position: relative;
                width: 500px;
                height: 500px;
                border: solid red 1px;
              }
              .demo {
                position: absolute;
                border: solid blue 1px;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
              }
            </style>
            <body>
              <div class="parent">
                <div class="demo">居中</div>
              </div>
            </body>

          以上3種是常用的方法,當(dāng)然還有其他居中方法比如grid布局,table-cell布局等。

          iv中的內(nèi)容居中顯示,包括水平和垂直2個(gè)方向。

          <html>
          <head>
              <style type="text/css">
                  .box{
                      height:400px;
                      width:600px;
                      background-color: #f2dede;
                      display: flex;
                      flex-direction: row;
                      align-items: center;
                      justify-content: center;
                  }
              </style>
          </head>
          <body>
          <div class="box">
              <div style="background-color: #00a4e6">居中</div>
              <div style="background-color: #00b33c">居中</div>
          </div>
          </body>
          </html>

          說明:

          align-items:center,控制垂直方向

          justify-content:center,控制水平方向

          彈性布局介紹:

          Flex 彈性盒子布局是很強(qiáng)大的布局,它可以很方便的控制元素在垂直和水平方向上的行為。

          使用display:flex

          主軸和交叉軸

          容器存在兩個(gè)軸,水平叫主軸(Main-Axis),垂直叫交叉軸(Cross-Axis)。主軸左邊是開始位置(main start)右邊是結(jié)束位置(main end),交叉軸上是開始位置(cross start)下是結(jié)束位置(cross end)。用 justify 屬性控制主軸項(xiàng)目的空隙,使用 align 屬性控制交叉軸項(xiàng)目之間的垂直行為。

          容器一共有 6 個(gè)屬性:

          flex-direction

          flex-wrap

          flex-flow

          justify-content

          align-items

          align-content

          #記錄我的2024#

          SS 是前端里面的基礎(chǔ)之一,也是非常重要的一部分,它往往決定了你所做出來的網(wǎng)頁頁面是否美觀。在設(shè)計(jì)網(wǎng)頁頁面的過程中,總會(huì)有將元素或者文字進(jìn)行水平垂直居中的要求。下面w3cschool編程獅就為大家介紹 CSS 中幾種常用到的水平垂直居中的方法。


          一、使用 margin:auto

          當(dāng)元素有給定的高度以及寬度的時(shí)候,使用 margin: auto; 元素僅會(huì)水平居中,并不會(huì)進(jìn)行垂直居中。此時(shí)就需要設(shè)置元素的 position 為 absolute,父級(jí)元素的 position 為 relative,同時(shí)元素的上下左右都需要設(shè)置為 0。

          HTML 代碼

          <div class="box">
            <div class="center1"></div>
          </div>

          CSS 代碼

          .box{
            width: 200px;
            height: 200px;
            background-color: #eee;
            position: relative;
            margin-top: 20px;
          }
          .center1{
            width: 50px;
            height: 50px;
            background-color: #00ACED;
            margin: auto;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
          }

          效果展示:



          二、使用 position:absolute

          當(dāng)已經(jīng)知道了要進(jìn)行水平垂直居中的元素的寬高時(shí),就可以通過設(shè)置 position: absolute 來實(shí)現(xiàn)。但是,使用的同時(shí)還需要結(jié)合其他屬性才完整實(shí)現(xiàn)。因?yàn)?,單是設(shè)置 absolute,上左距離均為一半,就會(huì)出現(xiàn)下面這種情況。很顯然可以看到,元素并不是完全居中,僅只有左上角的位置在中心點(diǎn)

          概念圖:

          因此想要實(shí)現(xiàn)元素完全水平垂直居中,在設(shè)置了 absolute 定位后,可以設(shè)置 margin 值為負(fù),或者使用 calc 來計(jì)算,上左距離在 50% 的基礎(chǔ)上還要減去元素本身一半的寬高。

          margin 值為負(fù)或者 calc 計(jì)算均是在已知元素寬高的情況下,假設(shè)不知道元素的寬高,那么怎么實(shí)現(xiàn)水平垂直居中呢?這里就可以使用 transform 屬性,通過坐標(biāo)位移來實(shí)現(xiàn)居中。

          CSS 代碼

          /* 結(jié)合 margin */
          .center2{
            width: 50px;
            height: 50px;
            background-color: #7FFFD4;
            position: absolute;
            left: 50%;
            top: 50%;
            margin-left: -25px;
            margin-top: -25px;
          }
          /* 結(jié)合 calc 計(jì)算*/
          .center2{
            width: 50px;
            height: 50px;
            background-color: #7FFFD4;
            position: absolute;
            left: calc(50% - 25px)
            top: calc(50% - 25px);
          }
          /* 結(jié)合 transform */
          .center2{
          width: 50px;
          height: 50px;
          background-color: #7FFFD4;
          position: absolute;
          left: 50%;
          top: 50%;
          transform: translate(-50%, -50%);
          }

          效果展示



          03

          PART

          三、使用彈性布局

          可以通過彈性布局來設(shè)置水平垂直居中,這里需要設(shè)置父級(jí)元素 display:flex; 還需要設(shè)置兩個(gè)屬性,水平布局 justify-content 以及垂直布局 align-items。

          HTML代碼

          <div class="box2">
            <div class="center4"></div>
          </div>

          CSS代碼:

          .box2{
            background-color: #eee;
            width: 200px;
            height: 200px;
            position: relative;
            margin-top: 20px ;
            display: flex;
            justify-content: center;
            align-items: center;
          }
          .center4{
            width: 50px;
            height: 50px;
            background-color: #B39873;
          }

          效果展示:


          四、文本水平對(duì)齊和行高

          前面介紹的是元素如何實(shí)現(xiàn)水平垂直居中,下面介紹的是如何將文字進(jìn)行水平垂直居中。這第一個(gè)方法也是最經(jīng)常用的,使用文本水平對(duì)齊 text-align 和行高 line-height 來實(shí)現(xiàn)的。

          HTML 代碼

          <div class="box3">
            <div class="center5">文字居中</div>
          </div>

          CSS 代碼

          .box3{
            background-color: #eee;
            width: 200px;
            height: 200px;
            margin-top: 20px;
          }
          .center5{
            text-align: center;
            line-height: 200px;
          }

          效果展示


          05

          PART

          五、使用網(wǎng)格布局

          第二個(gè)方法可以通過網(wǎng)格布局 grid 來實(shí)現(xiàn)。而這里通過 grid 有兩種方式實(shí)現(xiàn),一種對(duì)元素本身屬性進(jìn)行設(shè)置,另一種在元素的父級(jí)元素中設(shè)置。兩者看上去內(nèi)容似乎差不多,不同的是在元素中設(shè)置的是 align-self 還要多了一個(gè) margin,父級(jí)元素中是 align-items。

          相關(guān)代碼:

          /* grid 元素中設(shè)置 */
          .box4{
            background-color: #eee;
            width: 200px;
            height: 200px;
            margin-top: 20px;
            display: grid;
          }
          .center6{
            align-self: center;
            justify-content: center;
            margin: auto;
          }
          /* grid 父級(jí)元素中設(shè)置 */
          .box5{
            background-color: #eee;
            width: 200px;
            height: 200px;
            margin-top: 20px;
            display: grid;
            align-items: center;
            justify-content: center;
          }
          
          

          效果展示:


          六、總結(jié)

          以上就是關(guān)于 CSS 如何將元素或者文字進(jìn)行水平垂直居中的幾種常用方法,大家還其他關(guān)于 CSS 實(shí)現(xiàn)水平垂直居中的方法嗎?請(qǐng)?jiān)谠u(píng)論區(qū)留下你的想法。

          關(guān)注w3cschool編程獅訂閱更多IT資訊、技術(shù)干貨~


          主站蜘蛛池模板: 国产91精品一区二区麻豆网站| 亚洲高清毛片一区二区| 精品国产一区二区三区不卡| 北岛玲在线一区二区| 精品福利视频一区二区三区 | 日韩精品久久一区二区三区| 中文字幕无码一区二区免费 | 国产SUV精品一区二区四| 国产福利一区二区三区在线视频 | 成人免费视频一区二区三区| 亚洲国产av一区二区三区| 2020天堂中文字幕一区在线观| 国产伦精品一区二区三区在线观看 | 日韩精品人妻av一区二区三区| 99精品国产高清一区二区| 国产一区二区三区精品视频| 国产精品亚洲一区二区三区久久| 久久精品一区二区东京热| 曰韩精品无码一区二区三区| 视频一区在线免费观看| 国产一区二区三区在线免费| 在线精品视频一区二区| 国产精品视频一区二区猎奇| 国产精品视频无圣光一区| chinese国产一区二区| tom影院亚洲国产一区二区 | 久久精品无码一区二区app| 人妻无码久久一区二区三区免费 | 一区二区三区在线播放| 国产成人精品一区在线| 精品亚洲一区二区三区在线观看 | 男插女高潮一区二区| 亚洲熟女综合色一区二区三区| 无码人妻一区二区三区免费手机| 国产乱码一区二区三区| 精品久久综合一区二区| 国产精品污WWW一区二区三区| 99国产精品一区二区| 中文字幕乱码一区久久麻豆樱花| 天堂一区二区三区精品| www一区二区www免费|