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值。

属性:verical-align

用法: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

    • 使用定位positiontransform

        .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: 元素的高度heightwidth

      .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

参考资料

CSS如何居中元素的更多相关文章

  1. CSS行内元素和块级元素的居中

    一.水平居中 行内元素和块级元素不同,对于行内元素,只需在父元素中设置text-align=center即可; 对于块级元素有以下几种居中方式: 1.将元素放置在table中,再将table的marg ...

  2. css使子元素在父元素居中的各种方法

    html结构: <div class="parent"> <div class="child"></div> </di ...

  3. 浅谈css的伪元素::after和::before

    css中的::after和::before已经被大量地使用在我们日常开发中了,使用他们可以使我们的文档结构更加简洁.但是很多人对::after和::before仍不是特别了解,究竟他们是做什么的?如何 ...

  4. 【转】css布局居中和CSS内容居中区别和对应DIV CSS代码

    原文地址:http://www.divcss5.com/jiqiao/j771.shtml css布局居中和CSS内容居中区别和对应DIV CSS代码教程与图文代码案例篇 对于新手来说DIV CSS布 ...

  5. 使用Flexbox实现CSS竖向居中

    竖向居中需要一个父元素和一个子元素合作完成. <div class="flexbox-container"> <div>Blah blah</div& ...

  6. css常用居中

    对一个已知大小的元素上下左右居中(已知大小了,直接margin也就行了): css如下:.parent{height:100px;width:100px;background:grey;positio ...

  7. css设置居中的方案总结

    回想一下,自己平时项目里遇到的比较多的就是css如何让元素居中显示,其实差不多每种情况都遇到过,所采用的方法也都各有利弊,下面对这些方法来做个概括,对其中的坑点,也会一一指出来,希望能给遇到问题的同学 ...

  8. CSS布局---居中方法

    在web页面布局中居中是我们常遇到的情况,而居中分为水平居中与垂直居中 文本的居中 CSS中对文本的居中做的非常友好,我们是需要text-align, line-height 两个属性就可以控制文本的 ...

  9. amazeui学习笔记--css(HTML元素5)--表格Table

    amazeui学习笔记--css(HTML元素5)--表格Table 一.总结 1.基本样式:am-table:直接模块名  <table class="am-table"& ...

随机推荐

  1. python学习过程(四)

    上节我们说了怎么从一个网页中获取所有的a标签,包括a标签的文本和a标签的url,以及最后经过整理,直接从网页中获取key-value键值对,也就是标签:url这种形式. 例如 : 百度: http:/ ...

  2. luogu2522 [HAOI2011]Problem b

    luogu2522[HAOI2011]Problem b 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公 ...

  3. Codeforces Round #533 (Div. 2) 部分题解A~D

    A. Salem and Sticks 题目描述 Salem gave you n n n sticks with integer positive lengths a1,a2,…,an a_1, a ...

  4. kuangbin专题十二 HDU1176 免费馅饼 (dp)

    免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  5. Vuex基础-Module

    官方API地址:https://vuex.vuejs.org/zh/guide/modules.html 前面几节课写的user.js就称为一个module,这样做的原因是:由于使用单一状态树,应用的 ...

  6. springdataRedis连接redis集群

    配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...

  7. beego 连接postgres

    package main import ( "fmt" "github.com/astaxie/beego/orm" "mybee/models&qu ...

  8. 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_IL和验证

    1.IL 基于栈——所有指令压入一个执行栈,并从栈弹出结果. 2.IL 指令无类型——指令会判断栈中操作数的类型,并执行恰当的操作. 3.IL 最大优势——应用程序的健壮性和安全性. 将 IL 编译成 ...

  9. hdu6223 Infinite Fraction Path 2017沈阳区域赛G题 bfs加剪枝(好题)

    题目传送门 题目大意:给出n座城市,每个城市都有一个0到9的val,城市的编号是从0到n-1,从i位置出发,只能走到(i*i+1)%n这个位置,从任意起点开始,每走一步都会得到一个数字,走n-1步,会 ...

  10. 服务器报nginx: [warn] conflicting server name "blog.xueyi.com" on 0.0.0.0:80, ignored

    nginx: [warn] conflicting server name "blog.xueyi.com" on 0.0.0.0:80, ignored 修改nginx配置参数后 ...