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"& ...
随机推荐
- v-touch使用方法以及在项目中遇到的问题
上篇博客中我记得还有一个坑没有解决好,在这篇博客中详细说明一下. 在 https://github.com/dreamITGirl/vuepageturn 我的这个代码库里,更新到2.1版本. 目前解 ...
- jmeter - 录制web网页
1. 打开JMeter工具 创建一个线程组(右键点击“测试计划”--->“添加”---->“线程组”) 创建一个http代理服务器(右键点击“工作台”--->“添加”-- ...
- 勤哲excel服务器WEB网页环境搭建问题解决
因为客户希望在浏览器上使用勤哲的功能,因此希望大家勤哲excel服务器的web环境. 他们用的是勤哲2010版,需要装到64位环境下.在搭建的时候,遇到2个主要问题. 问题1:编译器错误消息: BC3 ...
- apache 压缩 gzip
配置 编辑httpd.conf文件 去掉 #LoadModule headers_module modules/mod_headers.so 前面的注释# 去掉 #LoadModule deflate ...
- HttpServletResponse 解决中文乱码
response.setHeader("Content-type", "text/html;charset=UTF-8"); response.setChara ...
- 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_运行时解析类型引用
public sealed class Program{ public static void Main() { System.Console.WriteLine("Hi"); } ...
- day18 约束 异常
1. 类的约束 方案一:抛出异常,子类必须重写父类方法,否则会报错 class Base: def login(self): #强制xxx做XXX事 raise NotImplementedError ...
- day17 isinstance type issubclass 反射
1. issubclass,type,isinstance 1.issubclass 判断xxx是否yyy的子类 例: class Foo: pass class Bar(Foo): pass cla ...
- python 多线程概念
######多线程##### 什么是线程: 线程是操作系统能够进行运算调度的最小单位(程序执行流的最小单元).它被包含在进程之中,是进程中的实际运作单位. 一个进程中可以并发多个线程,每条线程并行执行 ...
- 使用vlookup函数下拉全部相同解决
菜单“公式”-“计算选项”改为自动 ref: https://zhidao.baidu.com/question/561971299094821004.html