CSS 6种完全居中最佳实践(整理)
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;
}
在普通文档流里,margin: auto; 的意思是设置元素的margin-top和margin-bottom为0。
W3.org:?If ‘margin-top’, or ‘margin-bottom’ are ‘auto’, their used value is 0.设置了position: absolute; 的元素会变成块元素,并脱离普通文档流。而文档的其余部分照常渲染,元素像是不在原来的位置一样。
Developer.mozilla.org:?…an element that is positioned absolutely is taken out of the flow and thus takes up no space设置了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).给元素设置了宽高以后,浏览器会阻止元素填满所有的空间,根据margin: auto; 的要求,重新计算,并包裹一层新的盒子。
Developer.mozilla.org:?The margin of the [absolutely positioned] element is then positioned inside these offsets.既然块元素是绝对定位的,又脱离了普通文档流,因此浏览器在包裹盒子之前会给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:
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:
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;
}
参考出处:
关注公众号
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
实时同步部署(inotify,sersync)
使用inotify现实,实时同步的前提条件 a 先部署好rsync服务 b 其次部署好inotify服务,并对需要实时的数据进行监控 c 建立rsync与inotify服务的关联,对变化的数据进行实时备份传输 测试最少需要两台主机 cat /etc/hosts 192.168.40.26 nfs01 # rsync客户机+inotify,把监控的数据实时传输到备份服务器 192.168.40.27 backup01 # rsync服务器,作为备份服务器 1 修改ip 主机名 查看rsync是否安装 hostnamectl set-hostname nfs01 rpm -qa inotify-tools nmcli c mod eth1 ipv4.addr "192.168.40.26/24" nmcli d reapply eth1 ifconfig # 系统优化,请参考:https://blog.51cto.com/lehappy/2781516 2 rsync服务器部署参考 https://blog.51cto.com/lehappy/2759036 3 inotify服务部...
-
下一篇
Java 在Word中添加数学公式(Latex/MathML)
本文介绍通过Java程序在Word文档中添加数学公式的方法。添加时,可添加latex数学公式或者MathML数学公式。详细内容见下文。 1. 程序环境 Word测试文档:.docx 2013 Word jar包:free spire.doc.jar 3.9.0 代码编译环境:IntelliJ IDEA Jdk版本:1.8.0 其中,jar导入可分手动导入或者maven仓库下载导入。 1.1 手动导入:需下载jar包,解压并将lib文件夹下的jar文件导入程序,如图1; 图1 1.2 Maven导入:需配置pom.xml,如下, <repositories> <repository> <id>com.e-iceblue</id> <url>http://repo.e-iceblue.cn/repository/maven-public/</url> </repository> </repositories> <dependencies> <dependency> <...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- Docker容器配置,解决镜像无法拉取问题
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- Crontab安装和使用
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- Red5直播服务器,属于Java语言的直播服务器
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作

微信收款码
支付宝收款码