CSS实现居中的7种方法_html/css_WEB-ITnose
实现HTML元素的居中 看似简单,实则不然
水平居中比如容易,垂直居中比较难搞定,水平垂直都居中更不容易。在这个响应式布局的年代,很难固定元素的宽高,俺统计了一下,目前的几种方法。本文由浅入深逐个介绍,使用了同一段HTML代码:
<div class="center">
<img src="/static/imghw/default1.png" data-src="jimmy-choo-shoe.jpg" class="lazy" alt="">
</div>
下面鞋子图片会变化但原始大小始终是500px × 500px,主题背景颜色使用了HSL colors
1.水平居中?使用 text-align
有些场景下 简单的方法就是最好的方法
div.center { text-align: center; background: hsl(0, 100%, 97%); }
div.center img { width: 33%; height: auto; }
但该方法不能让图片垂直居中:需要给 div 添加 padding 或 给 div 中的元素添加 margin-top 和 margin-bottom
2. margin: auto 居中
同样也是水平居中,局限性跟第1种方法一样:
div.center { background: hsl(60, 100%, 97%); }
div.center img { display: block; width: 33%; height: auto; margin: 0 auto; }
注意 display: block, 这种情况下必须有 margin: 0 auto.
3. table-cell 居中
使用 display: table-cell, 可以实现水平垂直都居中。通常需要添加一个额外的空元素。
<div class="center-aligned">
<div class="center-core">
<img src="/static/imghw/default1.png" data-src="jimmy-choo-shoe.jpg" class="lazy" alt="CSS实现居中的7种方法_html/css_WEB-ITnose" >
</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; }
注意 width: 100% 是为了防止 div 折叠,外面的容器需要一个高度才能垂直居中。 如果垂直居中的元素是放在 . body 中的话,需要给 html 和 body 设置 height. 在所有浏览器中均有效,包括 IE8+.
4. Absolute 居中
最近 Stephen Shaw 推广的一项新技术可以很好地兼容各种浏览器。唯一的缺点是外部容器必须声明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;
Stephen 在 他的文章 中验证了这段代码的许多版本
5. 使用 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; }
有以下缺点:
6. 使用 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是一个可以接受的方案). 完整的 CSS代码:
.center { background: hsl(240, 100%, 97%);
display: -webkit-box; /* OLD: Safari, iOS 6 and earlier; Android browser, older WebKit */
display: -moz-box; /* OLD: Firefox (can be buggy) */
display: -ms-flexbox; /* OLD: IE 10 */
display: -webkit-flex; /* FINAL, PREFIXED, Chrome 21+ */
display: flex; /* FINAL: Opera 12.1+, Firefox 22+ */
-webkit-box-align: center;
-moz-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
-webkit-box-pack: center;
-moz-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
现在规范已经形成,浏览器也支持, I have written extensively on flexbox layout and its uses.
7. 使用 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% 是容器中元素的中间点,但是单独使用会使得image的左上角位于
top: calc(50% - (40% / 2)); left: calc(50% - (40% / 2));
在目前的浏览器中,你可以发现:当内容 fixed 且大小已知的时候,该技术效果最佳:
.center img { width: 500px; height: 500px; position: absolute; top:calc(50% - (300px / 2)); left: calc(50% - (300px ? 2)); }
calc 这种方法跟 flexbox 一样也有很多潜在的缺点: 支持Firefox 4 及更高版本浏览器,对于更早版本的浏览器,需要添加前缀,不支持IE8。图片居中的完整代码:
.center img { width: 40%; height: auto; position: absolute;
top: -webkit-calc(50% - 20%); left: -webkit-calc(50% - 20%);
top: -moz-calc(50% - 20%); left: -moz-calc(50% - 20%);
top: calc(50% - 20%); left: calc(50% - 20%); }
当然还有很多其他的方法,例如 使用伪元素来垂直居中 , 理解这些技术可以让web开发人员在处理居中问题的时候不麻爪!
原文在这里

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

本文討論了HTML&lt; Progress&gt;元素,其目的,樣式和與&lt; meter&gt;元素。主要重點是使用&lt; progress&gt;為了完成任務和LT;儀表&gt;對於stati

本文討論了html&lt; datalist&gt;元素,通過提供自動完整建議,改善用戶體驗並減少錯誤來增強表格。Character計數:159

本文討論了使用HTML5表單驗證屬性,例如必需的,圖案,最小,最大和長度限制,以直接在瀏覽器中驗證用戶輸入。

本文討論了HTML&lt; meter&gt;元素,用於在一個範圍內顯示標量或分數值及其在Web開發中的常見應用。它區分了&lt; meter&gt;從&lt; progress&gt;和前

本文討論了視口元標籤,這對於移動設備上的響應式Web設計至關重要。它解釋瞭如何正確使用確保最佳的內容縮放和用戶交互,而濫用可能會導致設計和可訪問性問題。

本文討論了&lt; iframe&gt;將外部內容嵌入網頁,其常見用途,安全風險以及諸如對象標籤和API等替代方案的目的。

HTML適合初學者學習,因為它簡單易學且能快速看到成果。 1)HTML的學習曲線平緩,易於上手。 2)只需掌握基本標籤即可開始創建網頁。 3)靈活性高,可與CSS和JavaScript結合使用。 4)豐富的學習資源和現代工具支持學習過程。
