CSS实现居中的7种方法

30 篇文章 0 订阅

实现HTML元素的居中 看似简单,实则不然

水平居中比如容易,垂直居中比较难搞定,水平垂直都居中更不容易。在这个响应式布局的年代,很难固定元素的宽高,俺统计了一下,目前的几种方法。本文由浅入深逐个介绍,使用了同一段HTML代码:

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

下面鞋子图片会变化但原始大小始终是500px × 500px,主题背景颜色使用了HSL colors 

1.水平居中—使用 text-align 

Photograph of a classic Chuck Converse shoe

有些场景下 简单的方法就是最好的方法

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 居中

Photograph of a white classic Nike sneaker

同样也是水平居中,局限性跟第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 居中

Photograph of black Oxford calfskin shoe designed by Vivienne Westwood

使用 display: table-cell,  可以实现水平垂直都居中。通常需要添加一个额外的空元素。

<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; }

注意 width: 100% 是为了防止 div 折叠,外面的容器需要一个高度才能垂直居中。 如果垂直居中的元素是放在 .  body 中的话,需要给 html 和 body 设置 height. 在所有浏览器中均有效,包括 IE8+.

4. Absolute 居中

Photograph of an Under Armour Micro G Toxic Six shoe

最近  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 居中

Photograph of a Jimmy Choo shoe

 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 需要针对不同的浏览器使用 特定的前缀  (-moz  或  -o  或  -webkit
  • 在低版本的IE (IE8 及以下 )中无效
  • 外部容器需要设置高度 height (or gain it in some other way)  因为它不能从它的absolutely-positioned 内容上获得高度.
  • 如果内容包含 text, 当前浏览器合成技术对文本解释得很模糊.

6. 使用 Flexbox 居中

Photograph of a Manolo Blahnik shoe

一旦属性变量和特定前缀消失的话,这种方法很可能成为首选的居中方案.

.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 居中

Photograph of a Christian Louboutin shoe

在某些场景下比 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的左上角位于<div>中间。我们需要把width 和height 再往回拉一下,大小分别是图片width 和height 的一半。表达式如下:

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开发人员在处理居中问题的时候不麻爪!

原文在这里

  • 10
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值