首頁  >  文章  >  web前端  >  總結css居中問題的解決方法

總結css居中問題的解決方法

高洛峰
高洛峰原創
2017-03-13 15:02:021358瀏覽

CSS居中遇到的問題總結

水平居中

  • #【行內元素】適用inline,inline-block,inline-table ,inline-flex元素

    .center {
      text-align: center;
    }
  • 【區塊級元素】適用於block level元素

    ①一個區塊級元素

       .center {
     margin: 0 auto;
       }

    ②多個區塊級元素

    方法一:将块级元素变为行内块级元素
    
    html部分:
    <main class="inline-block-center">
      <p>1</p>
      <p>2</p>
      <p>3</p>
    </main>
    css部分:
    .inline-block-center {
      text-align: center;
    }
    .inline-block-center p {
      display: inline-block;
      text-align: left;
    }
    
    
    方法二:flex布局
    
    html部分:
    <main class="flex-center">
      <p>1</p>
      <p>2</p>
      <p>3</p>
    </main>
    css部分:
    .flex-center{
        display:flex;
      justify-content:center;
    }

垂直居中

  • #【行內元素】

    ①單一行內元素:

    #情況一:當link或文字有包裹元素時,設定相等的上下padding

    .link {
      padding-top: 30px;
      padding-bottom: 30px;
    }

    情況二:當link或文字沒有包裹時,設定行高和高度相等

    .center-text-trick {
      height: 100px;
      line-height: 100px
    }

    ②多個行內元素:

    方法一:将多个行内元素分别置于table-cell中
    
    html部分:
    <table>
      <tr>
    <td>
      1
    </td>
      </tr>
    </table>
    css部分:
    table td {
      background: black;
      color: white;
      padding: 20px;
      border: 10px solid white;
      /* default is vertical-align: middle; */
    }
    
    
    方法二:将父元素设置为display:table,将自身设置为display:table-cell
    
    html部分:
    <p class="center-table">
      <p>1</p>
    </p>
    css部分:
    .center-table {
      display: table;
      height: 250px;
      width: 240px;
    }
    .center-table p {
      display: table-cell;
      vertical-align: middle;
    }
    
    
    方法三:使用felex
    
    html部分:
    <p class="flex-center">
      <p>1</p>
    </p>
    css部分:
    .flex-center{
      display: flex;
      justify-content: center;
      flex-direction: column;
      height: 400px;/*父容器必须有固定高度*/
    }
    
    
    方法四:当以上代码均不可用时,可尝试此奇淫巧技
    
    html部分:
    <p class="ghost-center">
      <p>1</p>
    </p>
    css部分:
    .ghost-center {
      position: relative;
    }
    .ghost-center::before {
      content: " ";
      display: inline-block;
      height: 100%;
      width: 1%;
      vertical-align: middle;
    }
    .ghost-center p {
      display: inline-block;
      vertical-align: middle;
    }
  • 【區塊級元素】

    #①已知元素高度(絕對定位+負的margin)

    .parent {
      position: relative;
    }
    .child {
      position: absolute;
      top: 50%;
      height: 100px;
      margin-top: -50px; /* 为元素高度的一半,没有box-sizing时,为height+padding+border的一半*/
    }

    ②不知元素高度(與上一方法,大同小異)

    .parent {
      position: relative;
    }
    .child {
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
    }

    ③flex佈局

    .parent {
      display: flex;
      flex-direction: column;
      justify-content: center;
    }

水平垂直均居中

①有固定寬高的元素

.parent {
  position: relative;
}

.child {
  width: 300px;
  height: 100px;
  padding: 20px;

  position: absolute;
  top: 50%;
  left: 50%;

  margin: -70px 0 0 -170px;/* 注意此处为height+padding+的一半*/
}

②沒有固定寬高的元素(同之前沒有固定寬高元素一樣,用transform解決)

.parent {
  position: relative;
}
.child {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

③使用flexbox佈局

.parent {
  display: flex;
  justify-content: center;
  align-items: center;
}

以上是總結css居中問題的解決方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn