PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

CSS实现居中的7种方法_html/css_WEB-ITnose

原创
2016-06-24 11:53:16 929浏览

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

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

下面鞋子图片会变化但原始大小始终是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,  可以实现水平垂直都居中。通常需要添加一个额外的空元素。

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的左上角位于

    中间。我们需要把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开发人员在处理居中问题的时候不麻爪!

    原文在这里

    PHP课程HTML视频教程CSS视频JS视频教程Vue视频教程
    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一条:(一)CSS3动画应用 下一条:CSS高效开发实战:CSS 3、LESS、SASS、Bootstrap、Foundation 读书笔记(1)设定背景图_html/css_WEB-ITnose

    相关文章

    查看更多
    打开APP,随时随地在线学习!