使用CSS完成元素置中的七種方法

來源:互聯網
上載者:User

標籤:

在網頁布局中元素水平置中比元素垂直置中要簡單不少,同時實現水平置中和垂直置中往往是最難的。現在是響應式設計的時代,我們很難確切的知道元素的準確高度和寬度,所以一些方案不大適用。據我所知, 在CSS中至少有六種實現置中的方法。我將使用下面的HTML結構從簡單到複雜開始講解:

<div class="center">  <img src="jimmy-choo-shoe.jpg" alt>
</div>

鞋子圖片會改變,但是他們都會保持500pxX500px的大小。 HSL colors 用於使背景顏色保持一致。

 使用text-align水平置中

有時顯而易見的方案是最佳的選擇:

div.center {  text-align: center;  background: hsl(0, 100%, 97%);
}
div.center img {  width: 33%; height: auto;
}

這種方案沒有使圖片垂直置中:你需要給<div> 添加 padding 或者給內容添加margin-top 和 margin-bottom使容器與內容之間有一定的距離。

使用 margin: auto 置中

這種方式實現水平置中和上面使用text-align的方法有相同局限性。

div.center {  background: hsl(60, 100%, 97%);
}
div.center img {  display: block;  width: 33%;  height: auto;  margin: 0 auto;}

注意: 必須使用display: block使 margin: 0 auto對img元素生效。

 使用table-cell置中

使用 display: table-cell, 而不是使用table標籤; 可以實現水平置中和垂直置中,但是這種方法需要添加額外的元素作為外部容器。

<div class="center-aligned">    <div class="center-core">        <img src="jimmy-choo-shoe.jpg">    </div>
</div>

CSS:

.center-aligned {    display: table;    background: hsl(120, 100%, 97%);    width: 100%;
}
.center-core {    display: table-cell;    text-align: center;    vertical-align: middle;
}
.center-core img {    width: 33%;    height: auto;
}

注意:為了使div 不摺疊必須加上 width: 100%,外部容器元素也需要加上一定高度使得內容垂直置中。給html和body設定高度後,也可以使元素在body垂直置中。此方法在IE8+瀏覽器上生效。

 使用absolute定位置中

這種 方案 有非常好的跨瀏覽器支援。有一個缺點就是必須顯式聲明外部容器元素的height:

.absolute-aligned {    position: relative;    min-height: 500px;    background: hsl(200, 100%, 97%);}
.absolute-aligned img {    width: 50%;    min-width: 200px;    height: auto;    overflow: auto;    margin: auto;    position: absolute;    top: 0; left: 0;    bottom: 0; right: 0;}

 

使用translate置中

Chris Coiyer 提出了一個使用 CSS transforms 的新方案。 同樣支援水平置中和垂直置中:

.center {    background: hsl(180, 100%, 97%);    position: relative;    min-height: 500px;}
.center img {    position: absolute;    top: 50%; left: 50%;    transform: translate(-50%, -50%);    width: 30%; height: auto;
}

但是有以下幾種缺點:

  • CSS transform 在部分就瀏覽器上需要使用 首碼。

  • 不支援 IE9 以下的瀏覽器。

  • 外部容器需要設定height (或者用其他方式設定),因為不能擷取 絕對位置 的內容的高度。

  • 如果內容包含文字,現在的瀏覽器合成技術會使文字模糊不清。

使用Flexbox置中

當新舊文法差異和瀏覽器首碼消失時,這種方法會成為主流的置中方案。

.center {     background: hsl(240, 100%, 97%);    display: flex;    justify-content: center;    align-items: center;
}
.center img {    width: 30%; height: auto;}

在很多方面 flexbox 是一種簡單的方案, 但是它有新舊兩種文法以及早期版本的IE缺乏支援 (儘管可以使用 display: table-cell作為降級方案)。

現在規範已經最終確定,現代瀏覽器也大都支援。

使用calc置中

在某些情況下比flexbox更全面:

.center {    background: hsl(300, 100%, 97%);    min-height: 600px;    position: relative;}
.center img {    width: 40%;    height: auto;    position: absolute;    top: calc(50% - 20%);    left: calc(50% - 20%);
}

很簡單,calc 允許你基於當前的頁面配置計算尺寸。在上面的簡單計算中, 50% 是容器元素的中心點,但是如果只設定50%會使圖片的左上方對齊div的中心位置。 我們需要把圖片向左和向上各移動圖片寬高的一半。計算公式為:

top: calc(50% - (40% / 2));
left: calc(50% - (40% / 2));

在現在的瀏覽其中你會發現,這種方法更適用於當內容的寬高為固定尺寸:

.center img {    width: 500px; height: 500px;    position: absolute;    top: calc(50% - (300px / 2));    left: calc(50% - (300px – 2)); 
}

我在 這篇文章 中詳細講解了calc。

這種方案和flex一樣有許多相同的缺點: 雖然在現代瀏覽器中有良好的支援,但是在較早的版本中仍然需要瀏覽器首碼,並且不支援IE8。

.center img {    width: 40%; 
     height: auto;    position: absolute;    top: calc(50% - 20%);    left: calc(50% - 20%);}

當然還有 其他更多的方案。理解這六種方案之後,web開發人員在面對元素置中的時候會有更多的選擇。

出自:http://www.zcfy.cc/article/439

使用CSS完成元素置中的七種方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.