CSS如何居中元素
How to center in CSS
一步步拆解你的需求,是水平居中还是垂直居中?还是水平垂直居中?父容器是inline还是block,高度知不知,宽度造不造?一个子元素还是多个子元素?一行还是多行?文字?图片?

理论知识
属性:text-align
用法:text-align CSS 属性定义行内内容(例如文字)如何相对它的块父元素对齐。text-align并不控制块级元素自己的对齐,只控制它的行内内容的对齐→→(demo展示text-align对行内元素与块元素的区别)
除了默认的行内元素(没有标签包围的文本默认是匿名行内元素)外,我们可以通过设置元素的display:inline、 inline-block、 inline-table、 inline-flex等一切inline 或 inline-*elements值。
用法:CSS 的属性 vertical-align 用来指定行内元素(inline)或表格单元格(table-cell)元素的垂直对齐方式。
1.水平居中
content: inline or inline-* elements (like text or links)
.container {
text-align: center;
}
content: block-level element
多个块级元素一行展示
.container {
text-align: center;
}
.content-center {
display: inline-block;
}
可实现多个块级多行展示(前提:知道width值px/em/%)
.content-center {
width: 400px;
margin: 0 auto;
}
2.垂直居中
content: for inline or inline-* elements (like text or links)
- 单行
- padding-top = padding-bottom
- line-height = height(需要注意的是,必须先设置字号
font-size,再设置line-height,否则文本居中。因为字号的改变,会使行高发生改变。)
- 多行
padding-top = padding-bottom
Table cell(前提:知道container高度
height值px/em/%).container {
display: table;
}
.content-center {
display: table-cell;
verical-align: middle;
}
flexbox(前提:知道container高度
height值px/em/%).container {
display: flex;
justify-content: center;
flex-direction: column;
height: 400px;
}
给父容器加一个伪类元素作为子内容的
vertical:middle校准内容。.container {
position: relative;
}
.container::before {
content: " ";
display: inline-block;
height: 100%;
width: 1%;
vertical-align: middle;
}
.container content-center {
display: inline-block;
vertical-align: middle;
}
content: block-level element
known: 元素的高度
height值使用定位
position与负外边距margin.container {
position: relative;
}
.content-center {
position: absolute;
top: 50%;
height: 100px;
margin-top: -50px; /* account for padding and border if not using box-sizing: border-box; */
}
unknown: 元素的高度
height值使用定位
position与transform.container {
position: relative;
}
.content-center {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
flexbox(高度知不知都行,缺点是IE11+才能用)
.container {
display: flex;
flex-direction: column;
justify-content: center;
}
3.水平垂直居中
known: 元素的高度
height值和width值使用定位
position与负外边距margin
.container {
position: relative;
}.content-center {
position: absolute;
top: 50%;
left: 50%; width: 300px;
height: 100px;
padding: 20px;
margin: -70px 0 0 -170px; /* 此处元素的高度、宽度要加上padding的大小 */
}
unknown: 元素的高度
height值width值.container {
position: relative;
}
.content-center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
flexbox
.parent {
display: flex;
justify-content: center;
align-items: center;
}
浏览器兼容性
主要考虑IE浏览器,其他现代浏览器支持较好(chrome、firefox)
| Method | IE |
|---|---|
| text-align | IE3+ |
| vertical-aglign | IE4+ |
| line-height | IE4+ |
| table-cell | IE8+ |
| position | IE9+ |
| flexbox | IE11 |
如何选择最优方案
- 如果不用考虑兼容性的话,使用flexbox代码最简洁
- 考虑兼容性的话,margin,padding,text-align,vertical-align,line-height > table-cell > positon > flexbox,再结合具体需求选择最合适的方案,代码越清晰简洁越好。
- 通过上面的方案可知,这些属性很多时候都是配合着使用的,从而得到兼容性更好的方法,具体可见于 HOW TO CENTER IN CSS
参考资料
- Centering in CSS: A Complete Guide
- HOW TO CENTER IN CSS
- css行高line-height的一些深入理解及应用
- vertical-align:CSS-TRICKS每个value均有实例
- HTML和CSS高级指南之二——定位详解:大漠老师手把手教你,这次彻底搞懂定位问题
CSS如何居中元素的更多相关文章
- CSS行内元素和块级元素的居中
一.水平居中 行内元素和块级元素不同,对于行内元素,只需在父元素中设置text-align=center即可; 对于块级元素有以下几种居中方式: 1.将元素放置在table中,再将table的marg ...
- css使子元素在父元素居中的各种方法
html结构: <div class="parent"> <div class="child"></div> </di ...
- 浅谈css的伪元素::after和::before
css中的::after和::before已经被大量地使用在我们日常开发中了,使用他们可以使我们的文档结构更加简洁.但是很多人对::after和::before仍不是特别了解,究竟他们是做什么的?如何 ...
- 【转】css布局居中和CSS内容居中区别和对应DIV CSS代码
原文地址:http://www.divcss5.com/jiqiao/j771.shtml css布局居中和CSS内容居中区别和对应DIV CSS代码教程与图文代码案例篇 对于新手来说DIV CSS布 ...
- 使用Flexbox实现CSS竖向居中
竖向居中需要一个父元素和一个子元素合作完成. <div class="flexbox-container"> <div>Blah blah</div& ...
- css常用居中
对一个已知大小的元素上下左右居中(已知大小了,直接margin也就行了): css如下:.parent{height:100px;width:100px;background:grey;positio ...
- css设置居中的方案总结
回想一下,自己平时项目里遇到的比较多的就是css如何让元素居中显示,其实差不多每种情况都遇到过,所采用的方法也都各有利弊,下面对这些方法来做个概括,对其中的坑点,也会一一指出来,希望能给遇到问题的同学 ...
- CSS布局---居中方法
在web页面布局中居中是我们常遇到的情况,而居中分为水平居中与垂直居中 文本的居中 CSS中对文本的居中做的非常友好,我们是需要text-align, line-height 两个属性就可以控制文本的 ...
- amazeui学习笔记--css(HTML元素5)--表格Table
amazeui学习笔记--css(HTML元素5)--表格Table 一.总结 1.基本样式:am-table:直接模块名 <table class="am-table"& ...
随机推荐
- javascript判断访问终端,手机端自动跳转
在网页的顶部加入javascript判断代码: function checkserAgent(){ var userAgentInfo=navigator.userAgent; var userAge ...
- Xml Helper
类的完整代码: using System;using System.Collections;using System.Xml; namespace Keleyi.Com.XmlDAL{public c ...
- Django个人博客开发 | 前言
本渣渣不专注技术,只专注使用技术,不是一个资深的coder,是一个不折不扣的copier 1.前言 自学 Python,始于 Django 框架,Scrapy 框架,elasticsearch搜索引擎 ...
- Python中使用Beautiful Soup库的超详细教程
[参考文献] http://www.jb51.net/article/65287.htm
- Windows中报错:Fatal error in launcher: Unable to create process using '"' 的解决方案
Windows 下同时存在 Python2 和 Python3 使用 pip 时系统报错:Fatal error in launcher: Unable to create process using ...
- ubuntu下vnc部署安装
ubuntu下vnc部署安装,参考如下博客:https://www.cnblogs.com/xuliangxing/p/7642650.html https://jingyan.baidu.com/a ...
- java程序员的从0到1:@Resource与@Autowired的比较
目录: 1.@Resource与@Autowired的源码分析 2.@Resource与@Autowired的相同点 3.@Resource与@Autowired的不同点 正文: 1.@Resourc ...
- Kibana问题记录:yarn test 运行报错 error Trailing spaces not allowed no-trailing-spaces
这个错误就是说,在指定的代码后面有太多无用空格了,你只要把那些空格删掉就行了. 如果你用的是vscode,推荐安装一个ESLint插件,格式话一下你的代码就可以了.
- Ubuntu 安装 phpredis扩展
官网 https://github.com/phpredis/phpredis 下载->然后解压->上传服务器 /etc/phpredis 进行 cd /etc/phpredisphpiz ...
- 配置sudo访问
具体操作步骤 1.首先我们建立一个账户,设置密码 [root@VM_0_13_centos home]# useradd 123 [root@VM_0_13_centos home]# passwd ...