亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

css布局教程之如何實(shí)現(xiàn)垂直居中

  發(fā)布時(shí)間:2019-07-12 16:51:50   作者:吳法祥   我要評(píng)論
這篇文章主要給大家介紹了關(guān)于css布局教程之如何實(shí)現(xiàn)垂直居中的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用css具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前言

最近在總結(jié)前端知識(shí),也參加了一些面試,面試中遇到一道題要求垂直居中,現(xiàn)在對(duì)這進(jìn)行一下總結(jié),也好鞏固一下知識(shí)。

css實(shí)現(xiàn)垂直居中

1.利用line-height實(shí)現(xiàn)居中,這種方法適合純文字類(lèi)的;

<!-- css -->
<style>
.parents {
  height: 400px;
  line-height: 400px;
  width: 400px;
  border: 1px solid red;
  text-align: center;
}

.child {
  background-color: blue;
  color: #fff;
}
 </style>
</head>

<body>
<!-- html -->
<div class="parents">
   <span class="child">css布局,實(shí)現(xiàn)垂直居中</span>
</div>
</body>

2.通過(guò)設(shè)置父容器相對(duì)定位,子級(jí)設(shè)置絕對(duì)定位,標(biāo)簽通過(guò)margin實(shí)現(xiàn)自適應(yīng)居中;

<!-- css -->
<style>
.parents {
  height: 400px;
  width: 400px;
  border: 1px solid red;
  position: relative;
}

.child {
  width: 200px;
  height: 100px;
  line-height: 100px;
  text-align: center;
  color: #fff;
  background-color: blue;
  /* 四個(gè)方向設(shè)置為0, 然后通過(guò)margin為auto自適應(yīng)居中 */
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
}
 </style>
</head>

<body>
<!-- html -->
<div class="parents">
  <span class="child">css布局,實(shí)現(xiàn)垂直居中</span>
</div>
</body>

3.彈性布局flex 父級(jí)設(shè)置display: flex; 子級(jí)設(shè)置margin為auto實(shí)現(xiàn)自適應(yīng)居中;

  <!-- css -->
  <style>
    .parents {
      height: 400px;
      width: 400px;
      border: 1px solid red;
      display: flex;
    }

    .child {
      width: 200px;
      height: 100px;
      line-height: 100px;
      text-align: center;
      color: #333;
      background-color: yellow;
      margin: auto;
  }
 </style>
</head>

<body>
 <!-- html -->
  <div class="parents">
    <span class="child">css布局,實(shí)現(xiàn)垂直居中</span>
  </div>
</body>

4. 父級(jí)設(shè)置相對(duì)定位,子級(jí)設(shè)置絕對(duì)定位,并且通過(guò)位移transform實(shí)現(xiàn);

  <!-- css -->
  <style>
    .parents {
      height: 400px;
      width: 400px;
      border: 1px solid red;
      position: relative;
    }

    .child {
      width: 200px;
      height: 100px;
      line-height: 100px;
      text-align: center;
      color: #fff;
      background-color: green;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
  </style>
</head>

<body>
  <!-- html -->
  <div class="parents">
    <span class="child">css布局,實(shí)現(xiàn)垂直居中</span>
  </div>
</body>

5. 父級(jí)設(shè)置彈性盒子,并設(shè)置彈性盒子相關(guān)屬性;

 <!-- css -->
 <style>
    .parents {
      height: 400px;
      width: 400px;
      border: 1px solid red;
      display: flex;
      justify-content: center; /* 水平 */
      align-items: center; /* 垂直 */
    }

    .child {
      width: 200px;
      height: 100px;
      color: black;
      background-color: orange;
    }
  </style>
</head>

<body>
  <!-- html -->
  <div class="parents">
    <span class="child"></span>
  </div>
</body>

6. 網(wǎng)格布局,父級(jí)通過(guò)轉(zhuǎn)換成表格形式,然后子級(jí)設(shè)置行內(nèi)或行內(nèi)塊實(shí)現(xiàn)。(需要注意的是:vertical-align: middle使用的前提條件是內(nèi)聯(lián)元素以及display值為table-cell的元素)。

 

 <!-- css -->
 <style>
    .parents {
      height: 400px;
      width: 400px;
      border: 1px solid red;
      display: table-cell;
      text-align: center;
      vertical-align: middle;
    }

    .child {
      width: 200px;
      height: 100px;
      color: #fff;
      background-color: blue;
      display: inline-block; /* 子元素設(shè)置行內(nèi)或行內(nèi)塊 */
    }
  </style>
</head>

<body>
  <!-- html -->
  <div class="parents">
    <span class="child"></span>
  </div>
</body>

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • CSS3 不定高寬垂直水平居中的幾種方式

    這篇文章主要介紹了CSS3 不定高寬垂直水平居中的幾種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起
    2020-03-26
  • 利用css樣式實(shí)現(xiàn)表格中字體垂直居中的方法

    這篇文章主要介紹了利用css樣式實(shí)現(xiàn)表格中字體垂直居中的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-16
  • CSS3實(shí)現(xiàn)水平居中、垂直居中、水平垂直居中的實(shí)例代碼

    在前端面試中經(jīng)常會(huì)遇到css居中效果的實(shí)現(xiàn),今天小編給大家分享幾種css垂直水平居中的方法,通過(guò)實(shí)例代碼給大家講解,需要的朋友參考下吧
    2020-02-27
  • 手把手教你CSS水平、垂直居中的10種方式(小結(jié))

    這篇文章主要介紹了手把手教你CSS水平、垂直居中的10種方式(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著
    2019-11-07
  • css常用元素水平垂直居中方案

    這篇文章主要介紹了css常用元素水平垂直居中方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)
    2019-08-09
  • CSS實(shí)現(xiàn)垂直居中的幾種方法小結(jié)

    在前端布局過(guò)程中,我們實(shí)現(xiàn)水平居中比較簡(jiǎn)單,一般通過(guò)margin:0 auto;和父元素 text-align: center;就能實(shí)現(xiàn)。今天小編給大家?guī)?lái)了CSS實(shí)現(xiàn)垂直居中的幾種方法小結(jié),感興
    2019-05-14
  • CSS垂直居中的另類(lèi)實(shí)現(xiàn)代碼詳解(不走尋常路)

    這篇文章主要介紹了CSS垂直居中的另類(lèi)實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-02

最新評(píng)論