2016年4月28日

1.最佳法:

.Absolute-Center {
width: 50%;
height: 50%;
overflow: auto;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
background-color: red;
}

在线演示

  1. 在普通文档流里,margin: auto; 的意思是设置元素的margin-top和margin-bottom为0。

    W3.org:?If ‘margin-top’, or ‘margin-bottom’ are ‘auto’, their used value is 0.

  2. 设置了position: absolute; 的元素会变成块元素,并脱离普通文档流。而文档的其余部分照常渲染,元素像是不在原来的位置一样。

    Developer.mozilla.org:?…an element that is positioned absolutely is taken out of the flow and thus takes up no space

  3. 设置了top: 0; left: 0; bottom: 0; right: 0; 样式的块元素会让浏览器为它包裹一层新的盒子,因此这个元素会填满它相对父元素的内部空间,这个相对父元素可以是是body标签,或者是一个设置了position: relative; 样式的容器。

    Developer.mozilla.org:?For absolutely positioned elements, the top, right, bottom, and left properties specify offsets from the edge of the element’s containing block (what the element is positioned relative to).

  4. 给元素设置了宽高以后,浏览器会阻止元素填满所有的空间,根据margin: auto; 的要求,重新计算,并包裹一层新的盒子。
    Developer.mozilla.org:?The margin of the [absolutely positioned] element is then positioned inside these offsets.

  5. 既然块元素是绝对定位的,又脱离了普通文档流,因此浏览器在包裹盒子之前会给margin-top和margin-bottom设置一个相等的值。
    W3.org:?If none of the three [top, bottom, height] are ‘auto’: If both ‘margin-top’ and ‘margin-bottom’ are ‘auto’, solve the equation under the extra constraint that the two margins get equal values.?AKA: center the block vertically

使用“完全居中”,有意遵照了标准margin: auto; 样式渲染的规定,所以应当在与标准兼容的各种浏览器中起作用。

优点:

  • 跨浏览器,兼容性好(无需hack,可兼顾IE8~IE10)
  • 无特殊标记,样式更精简
  • 自适应布局,可以使用百分比和最大最小高宽等样式
  • 居中时不考虑元素的padding值(也不需要使用box-sizing样式)
  • 布局块可以自由调节大小
  • img的图像也可以使用

同时注意:

  • 必须声明元素高度
  • 推荐设置overflow:auto;样式避免元素溢出,显示不正常的问题
  • 这种方法在Windows Phone上不起作用

2.负margin法:

.negative-margin {
width: 300px;
height: 200px;
padding: 20px;
position: absolute;
top: 50%; left: 50%;
margin-left: -170px; /* (width + padding)/2 */
margin-top: -120px; /* (height + padding)/2 */
}

在线演示

3.transform法:

.transform {
width: 50%;
margin: auto;
position: absolute;
top: 50%; left: 50%;
-webkit-transform: translate(-50%,-50%);
-ms-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
}

在线演示

4.inner-block法:

HTML:

<div class="Center-Container is-Inline">
<div class="Center-Block">
<!-- CONTENT -->
</div>
</div>

CSS:

.Center-Container.is-Inline {
text-align: center;
overflow: auto;
} .Center-Container.is-Inline:after,
.is-Inline .Center-Block {
display: inline-block;
vertical-align: middle;
} .Center-Container.is-Inline:after {
content: '';
height: 100%;
margin-left: -0.25em; /* To offset spacing. May vary by font */
} .is-Inline .Center-Block {
max-width: 99%; /* Prevents issues with long content causes the content block to be pushed to the top */
/* max-width: calc(100% - 0.25em) /* Only for IE9+ */
}

在线演示

5.Flexbox法:

.Center-Container.is-Flexbox {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-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;
}

在线演示

优点:

  • 内容可以是任意高宽,溢出也能表现良好
  • 可以用于各种高级布局技巧
  • 同时注意: 不支持IE8-9

同时注意:

  • 需要在body上写样式,或者需要额外容器
  • 需要各种厂商前缀兼容现代浏览器
  • 可能有潜在的性能问题

6.Table-cell法:

HTML:

<div class="Center-Container is-Table">
<div class="Table-Cell">
<div class="Center-Block">
<!-- CONTENT -->
</div>
</div>
</div>

CSS:

.Center-Container.is-Table { display: table; }
.is-Table .Table-Cell {
display: table-cell;
vertical-align: middle;
}
.is-Table .Center-Block {
width: 50%;
margin: 0 auto;
}

在线演示

参考出处:

最新文章

  1. 用SignalR 2.0开发客服系统[系列1:实现群发通讯]
  2. Css 备忘知识点
  3. Windows台的FailOver群集简介
  4. Mysql常用的一些技巧命令
  5. nginx 日志切割
  6. json转csv
  7. 0xc0000428 winload.exe无法验证其数字签名的解决方法
  8. Two kinds of Quaternion SlerpImp (Unity)
  9. Unity3D动态加载外部资源
  10. hitTest:withEvent:方法(此方法可实现点击穿透、点击下层视图功能)
  11. [Android] FileInputStream跟踪
  12. 通过file文件选择图片预览功能
  13. Flask -- 会话
  14. 阿里云 Centos7.3安装mysql5.7.18 rpm安装
  15. 【转】C缺陷和陷阱学习笔记
  16. [nginx] 对UA为空的请求返回403
  17. Java 接口 新特性(Java8)
  18. 说说Android项目中的armeabi,armeabi-v7a和x86
  19. 一.javascript核心部分:1.词法结构
  20. project5 大数据

热门文章

  1. Netty之SubPage级别的内存分配
  2. Python 根据入栈顺利判定出栈顺序
  3. Codeforces 1119C(思维)
  4. LCT题单(自己的做题情况反馈)(转自Flash)
  5. 164-基于TI DSP TMS320C6455和Altera FPGA EP2S130的Full CameraLink PDS150接口板卡
  6. 微信小程序(4)--二维码窗口
  7. css: IE没法调整那些使用px作为单位的字体大小
  8. linux 性能测试之基准测试工具
  9. 【串线篇】Mybatis之SSM整合
  10. docker 报错端口被占用 sqlserver 占用80端口