2018-05-22 CSS如何居中

本文翻譯自 CSS Tricks 中關於居中的相關介紹。

1、水平居中

1.1 內聯元素的水平居中(如鏈接或文本)

可以在一個塊級別的父元素中水平地居中內聯元素,只需:

.center-children {
  text-align: center;
}

注:這種寫法適用於inline, inline-block, inline-table, inline-flex等等。
代碼示例:
=> html代碼

<header>
  This text is centered.
</header>

<nav role='navigation'>
  <a href="#0">One</a>
  <a href="#0">Two</a>
  <a href="#0">Three</a>
  <a href="#0">Four</a>
</nav>  

=> css代碼

body {
  background: #f06d06;
}

header, nav {
  text-align: center;
  background: white;
  margin: 20px 0;
  padding: 10px;
}

nav a {
  text-decoration: none;
  background: #333;
  border-radius: 5px;
  color: white;
  padding: 3px 8px;
}

=> 效果圖

內聯元素水平居中

1.2 塊級元素水平居中

可以通過給它設置margin-leftmargin-rightauto (並且該元素需有width屬性,否則它將佔滿整行,無法居中)來居中一個塊級元素。通常用這樣的速記法來做:

.center-me {
  margin: 0 auto;
}

注:無論您所處的塊級別元素的寬度是多少,還是父節點,這都是可行的。
注意:你不能浮動一個元素到水平居中的位置 ,不過有一個小技巧(本文不再詳述,可點擊鏈接自行查看)。
代碼示例:

=> html代碼

<main>
  <div class="center">
    I'm a block level element and am centered.
  </div>
</main>

=> css代碼

body {
  background: #f06d06;
}

main {
  background: white;
  margin: 20px 0;
  padding: 10px;
}

.center {
  margin: 0 auto;
  width: 200px;
  background: black;
  padding: 20px;
  color: white;
}

=> 效果圖
塊級元素居中

1.3 兩個及以上的多個塊級元素水平居中

如果您有兩個或多個塊級別的元素,它們需要在一行中水平居中,那麼最好將它們作爲不同的display。比如設置其爲display:inline-block

代碼示例1:
注:下面的示例中,inline-block-center的<main>標籤中,是把父元素設置text-align:center,子元素設置display:inline-block
flex-center的<main>標籤中,使用了CSS3中新增的彈性盒子(附帶MDN相關介紹)是把父元素設置display: flex;justify-content: center;
=>html代碼

<main class="inline-block-center">
  <div>
    I'm an element that is block-like with my siblings and we're centered in a row.
  </div>
  <div>
    I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
  </div>
  <div>
    I'm an element that is block-like with my siblings and we're centered in a row.
  </div>
</main>

<main class="flex-center">
  <div>
    I'm an element that is block-like with my siblings and we're centered in a row.
  </div>
  <div>
    I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
  </div>
  <div>
    I'm an element that is block-like with my siblings and we're centered in a row.
  </div>
</main>

=>css代碼

body {
  background: #f06d06;
  font-size: 80%;
}

main {
  background: white;
  margin: 20px 0;
  padding: 10px;
}

main div {
  background: black;
  color: white;
  padding: 15px;
  max-width: 125px;
  margin: 5px;
}

.inline-block-center {
  text-align: center;
}
.inline-block-center div {
  display: inline-block;
  text-align: left;
}

.flex-center {
  display: flex;
  justify-content: center;
}

=>效果圖
多個塊級元素水平居中

代碼示例2:
注:下面的例子中,表現的是你有多個塊級元素堆疊在一起,在這種情況下,自動邊距(即margin:0 auto)仍然很好。即多個塊級元素不需要在一行顯示,居中的方法仍使用margin:0 auto
=>html代碼

<main>
  <div>
    I'm an element that is block-like with my siblings and we're centered in a row.
  </div>
  <div>
    I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
  </div>
  <div>
    I'm an element that is block-like with my siblings and we're centered in a row.
  </div>
</main>

=>css代碼

body {
  background: #f06d06;
  font-size: 80%;
}

main {
  background: white;
  margin: 20px 0;
  padding: 10px;
}

main div {
  background: black;
  margin: 0 auto;
  color: white;
  padding: 15px;
  margin: 5px auto;
}

main div:nth-child(1) {
  width: 200px;
}
main div:nth-child(2) {
  width: 400px;
}
main div:nth-child(3) {
  width: 125px;
}

=>效果圖

2、垂直居中

2.1 內聯元素垂直居中

2.1.1 單行內聯元素垂直居中
代碼示例1:
單行內聯元素使用上下相同的padding造成垂直居中的效果

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

=>效果圖
使用padding垂直居中

代碼示例2:
當某些情況下,不適合使用padding填充時,可以讓其文本的行高=父元素的高度達到垂直居中的效果。
=>html代碼

<main>
  <div>
    I'm a centered line.
  </div>
</main>

=>css代碼

body {
  background: #f06d06;
  font-size: 80%;
}

main {
  background: white;
  margin: 20px 0;
  padding: 40px;
}

main div {
  background: black;
  color: white;
  height: 100px;
  line-height: 100px;
  padding: 20px;
  width: 50%;
  white-space: nowrap;
}

=>效果圖
使用行高垂直居中

2.1.2 多行內聯元素垂直居中
代碼示例1:
使用表格佈局居中(基本過時的用法)
注:下面的例子中,第一個效果圖展示的是直接使用<table>標籤(其<td>標籤默認垂直居中),第二個效果圖展示的是父元素<div>設置display:table,子元素<p>設置display: table-cell;vertical-align: middle;
=>html代碼

<table>
  <tr>
    <td>
      I'm vertically centered multiple lines of text in a real table cell.
    </td>
  </tr>
</table>

<div class="center-table">
  <p>I'm vertically centered multiple lines of text in a CSS-created table layout.</p>
</div>

=>css代碼

body {
  background: #f06d06;
  font-size: 80%;
}

table {
  background: white;
  width: 240px;
  border-collapse: separate;
  margin: 20px;
  height: 250px;
}

table td {
  background: black;
  color: white;
  padding: 20px;
  border: 10px solid white;
  /* default is vertical-align: middle; */
}

.center-table {
  display: table;
  height: 250px;
  background: white;
  width: 240px;
  margin: 20px;
}
.center-table p {
  display: table-cell;
  margin: 0;
  background: black;
  color: white;
  padding: 20px;
  border: 10px solid white;
  vertical-align: middle;
}

=>效果圖
table佈局

display:table佈局

代碼示例2:
使用flexbox來垂直居中。
精簡CSS代碼如下:

.flex-center-vertically {
  display: flex;
  justify-content: center;
  flex-direction: column;
  height: 400px;
}
具體示例:

=>html代碼

<div class="flex-center">
  <p>I'm vertically centered multiple lines of text in a flexbox container.</p>
</div>

=>css代碼

body {
  background: #f06d06;
  font-size: 80%;
}

div {
  background: white;
  width: 240px;
  margin: 20px;
}

.flex-center {
  background: black;
  color: white;
  border: 10px solid white;
  display: flex;
  flex-direction: column;
  justify-content: center;
  height: 200px;
  resize: vertical;
  overflow: auto;
}
.flex-center p {
  margin: 0;
  padding: 20px;
}

=>效果圖
flexbox垂直居中

代碼示例3:
使用僞元素來垂直居中。
精簡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;
}

=>html代碼

<div class="ghost-center">
  <p>I'm vertically centered multiple lines of text in a container. Centered with a ghost pseudo element</p>
</div>

=>css代碼

body {
  background: #f06d06;
  font-size: 80%;
}

div {
  background: white;
  width: 240px;
  height: 200px;
  margin: 20px;
  color: white;
  resize: vertical;
  overflow: auto;
  padding: 20px;
}

.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;
  width: 190px;
  margin: 0;
  padding: 20px;
  background: black;
}

=>效果圖
僞元素垂直居中

2.2 塊級元素垂直居中

不知道網頁佈局的高度是很常見的,原因有很多:如果寬度改變,文本流會改變高度。文本樣式的差異可以改變高度。文本數量的差異可以改變高度。具有固定寬寬比的元素,如圖像,可以在重置寬高時改變高度等等。
但是如果你已經知道高度,就可以這樣:

2.2.1 塊級元素的高度已知/確定
即設置子元素相對於父元素絕對定位,子元素設置top:50%,並設置margin-top爲負的子元素自身高度的一半。

.parent {
  position: relative;
}
.child {
  position: absolute;
  top: 50%;
  height: 100px;
  margin-top: -50px; /* account for padding and border if not using box-sizing: border-box; */
}

代碼示例:
注:
=>html代碼

<main>

  <div>
     I'm a block-level element with a fixed height, centered vertically within my parent.
  </div>

</main>

=>css代碼

body {
  background: #f06d06;
  font-size: 80%;
}

main {
  background: white;
  height: 300px;
  margin: 20px;
  width: 300px;
  position: relative;
  resize: vertical;
  overflow: auto;
}

main div {
  position: absolute;
  top: 50%;
  left: 20px;
  right: 20px;
  height: 100px;
  margin-top: -70px;
  background: black;
  color: white;
  padding: 20px;
}

=>效果圖

2.2.2 塊級元素的高度未知/不確定
它仍然可以通過將它的一半高度推到一半的高度來居中,即設置子元素transform: translateY(-50%); ,即:

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

=>html代碼

<main>

  <div>
     I'm a block-level element with an unknown height, centered vertically within my parent.
  </div>

</main>

=>css代碼

body {
  background: #f06d06;
  font-size: 80%;
}

main {
  background: white;
  height: 300px;
  margin: 20px;
  width: 300px;
  position: relative;
  resize: vertical;
  overflow: auto;
}

main div {
  position: absolute;
  top: 50%;
  left: 20px;
  right: 20px;
  background: black;
  color: white;
  padding: 20px;
  transform: translateY(-50%);
  resize: vertical;
  overflow: auto;
}

=>效果圖

2.2.3 使用display:flex佈局
即:

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

=>html代碼

<main>

  <div>
     I'm a block-level element with an unknown height, centered vertically within my parent.
  </div>

</main>

=>css代碼

body {
  background: #f06d06;
  font-size: 80%;
}

main {
  background: white;
  height: 300px;
  width: 200px;
  padding: 20px;
  margin: 20px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  resize: vertical;
  overflow: auto;
}

main div {
  background: black;
  color: white;
  padding: 20px;
  resize: vertical;
  overflow: auto;
}

=>效果圖

3、水平居中+垂直居中

3.1 固定寬高的元素

使用負的margin等於寬度和高度的一半,在你完全定位50%/50%之後,它會以巨大的跨瀏覽器支持爲中心,即:

.parent {
  position: relative;
}

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

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

  margin: -70px 0 0 -170px;
}

代碼示例:
=>html代碼

<main>

  <div>
     I'm a block-level element a fixed height and width, centered vertically within my parent.
  </div>

</main>

=>css代碼

body {
  background: #f06d06;
  font-size: 80%;
  padding: 20px;
}

main {
  position: relative;
  background: white;
  height: 200px;
  width: 60%;
  margin: 0 auto;
  padding: 20px;
  resize: both;
  overflow: auto;
}

main div {
  background: black;
  color: white;
  width: 200px;
  height: 100px;
  margin: -70px 0 0 -120px;
  position: absolute;
  top: 50%;
  left: 50%;
  padding: 20px;
}

=>效果圖

3.2 寬高未知的元素

如果你不知道寬度和高度,你可以使用transform屬性和在兩個方向上的負translate(它基於當前元素的寬度/高度)到中心,即:

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

代碼示例:
=>html代碼

<main>

  <div>
     I'm a block-level element of an unknown height and width, centered vertically within my parent.
  </div>

</main>

=>css代碼

body {
  background: #f06d06;
  font-size: 80%;
  padding: 20px;
}

main {
  position: relative;
  background: white;
  height: 200px;
  width: 60%;
  margin: 0 auto;
  padding: 20px;
  resize: both;
  overflow: auto;
}

main div {
  background: black;
  color: white;
  width: 50%;
  transform: translate(-50%, -50%);
  position: absolute;
  top: 50%;
  left: 50%;
  padding: 20px;
  resize: both;
  overflow: auto;
}

=>效果圖

  • #### 使用flexbox居中
    要用flexbox向兩個方向居中,你需要使用兩個中心屬性,即:
.parent {
  display: flex;
  justify-content: center;
  align-items: center;
}

=>html代碼

<main>

  <div>
     I'm a block-level-ish element of an unknown width and height, centered vertically within my parent.
  </div>

</main>

=>css代碼

body {
  background: #f06d06;
  font-size: 80%;
  padding: 20px;
}

main {
  background: white;
  height: 200px;
  width: 60%;
  margin: 0 auto;
  padding: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  resize: both;
  overflow: auto;
}

main div {
  background: black;
  color: white;
  width: 50%;
  padding: 20px;
  resize: both;
  overflow: auto;
}

=>效果圖

3.3 使用grid居中

這只是一個非常有效的小技巧(由Lance Janssen上傳)
=>html代碼

<div class="wrapper">
    <span> I'm centered!</span>
</div>

=>css代碼

.wrapper{
  height:300px;
  width:300px;
  border:1px solid red;
  display: grid;
}
span {
  margin: auto;
}

=>效果圖

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章