CSS实现居中的7种方法

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

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

<div class="center">
<img src="jimmy-choo-shoe.jpg" 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="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 居中

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

有以下缺点:

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

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

原文在这里

时间: 2024-08-28 02:25:56

CSS实现居中的7种方法的相关文章

顽石系列:CSS实现垂直居中的五种方法

顽石系列:CSS实现垂直居中的五种方法 在开发过程中,我们可能沿用或者试探性地去使用某种方法实现元素居中,但是对各种居中方法的以及使用场景很不清晰.参考的内容链接大概如下: 行内元素:https://blog.csdn.net/bwf_erg/article/details/69844527 MDN文档:https://developer.mozilla.org/zh-CN/docs/Web/CSS/vertical-align 六种方法:https://www.jianshu.com/p/08

DIV居中的几种方法

DIV居中的几种方法 1. 1 body{ 2 text-align:center; 3 } 缺点:body内所有内容一并居中 2. .center{ position: fixed; left: 50%; } 缺点:需要设置position属性,网页复杂时容易扰乱页面布局,而且只是元素的起始位置居中 3. 1 .center{ 2 width:500px; 3 margin: 0 auto; 4 } 缺点:需要设置div宽度 4. 1 .center { 2 display: -webkit-

CSS 清除浮动的四种方法

在实际项目中,我们经常会用到float属性来对页面进行布局.当使用float时,意味着该元素已经脱离了文档流,相当于浮于文档之上,不占据空间.但是针对兄弟元素为文字内容时,会占据一定空间,从而产生文字环绕的效果.如果不清除浮动,会导致父元素的高度撑不开等一系列问题. 那如何清除浮动呢?给出以下几种方法,一些不常用的就不说明了. 1.对浮动元素的父容器添加after伪类,并给伪类设置clear属性(给浮动元素的周围元素添加clear属性,清除该浮动元素)在浮动元素的后面增加一个块元素,对块元素设置

css清除浮动的几种方法整理

此为未清除浮动源代码,运行代码无法查看到父级元素浅黄色背景. <style type="text/css"> <!– *{margin:0;padding:0;} body{font:36px bold; color:#F00; text-align:center;} #layout{background:#FF9;} #left{float:left;width:20%;height:200px;background:#DDD;line-height:200px;}

css清除浮动的几种方法以及对应规范说明

css清除浮动的几种方法以及对应规范说明 1.{clear:both;}设置了clear 属性的元素,其上边框位置会紧贴浮动元素的 margin-bottom 边界位置渲染,使包含浮动元素的容器高度正常.所以适用于浮动元素后面容器之内有个非浮动元素,或是额外添加一个新的空元素. 2..after- clear-float :after{content:""; display:block; clear:both;}利用伪类添加新元素,原理同上,所以只适用于父容器最后一级子元素是浮动的,即

【转】浅谈 CSS 清除浮动的 6 种方法

转载:浅谈 CSS 清除浮动的 6 种方法 在开发网页的时候经常需要用到各种浮动,此时便需要及时的清除浮动,否则将会导致布局出现问题 引出问题: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style> .outer{ border: 1px solid black;

CSS 隐藏元素的八种方法

前言 总括: 本文详细讲述了在网页中用CSS隐藏元素的七种方法. 原文博客地址:从隐藏元素谈起 知乎专栏&&简书专题:前端进击者(知乎)&&前端进击者(简书) 博主博客地址:Damonare的个人博客 念念不忘,必有回响;有一口气,点一盏灯. 正文 说起隐藏元素我想每一个前端er都能说起几种,但能说全的我想就不是很多了.博主总结了几种隐藏元素的方法,总结如下: 1 div{ 2 3 overflow:hidden 4 opacity:0: 5 visibility:hidd

元素居中的几种方法

元素居中的几种方法: ① 块级元素margin:0 auto; 针对嵌套的盒子元素,子元素在父元素中居中,只在水平方向上有效,浮动流排版中不可用 ② 内联元素text-align:center; 对块级元素设置此属性,使得盒子内的内联元素居中,一般包括文字/图片等 ③ 背景background-position:center top; 主要实现背景图片的居中 ④ 文字行内垂直居中 一行文字垂直居中:文字行高line-height的值与盒子的高度一致 多行文字垂直居中:文字内边距设置padding

css中元素居中的几种方法

对于在网页端布局,垂直居中难于水平居中,同时实现水平和垂直居中是最难的.在移动端,因为设备的宽高是可变的,故一些方案很难实现.以下使用几种方案对下面的html去实现居中,如有不足,可以提出宝贵的意见: <div class="center"> <img src="1.jpg" alt> </div> 1. 使用text-align水平居中 这种方案只能使水平居中,要想垂直居中的话,要给div添加padding或给内容添加margi