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. 剑指offer —— 从尾到头打印链表

    1.问题:输入一个链表,从尾到头打印链表每个节点的值. /** * public class ListNode { * int val; * ListNode next = null; * * Lis ...

  2. DP【洛谷P2363】马农

    [洛谷P2363]马农 题目描述 在观看完战马检阅之后,来自大草原的两兄弟决心成为超级"马农",专门饲养战马. 兄弟两回到草原,将可以养马的区域,分为N*N的单位面积的正方形,并实 ...

  3. Leetcode 746. Min Cost Climbing Stairs 最小成本爬楼梯 (动态规划)

    题目翻译 有一个楼梯,第i阶用cost[i](非负)表示成本.现在你需要支付这些成本,可以一次走两阶也可以走一阶. 问从地面或者第一阶出发,怎么走成本最小. 测试样例 Input: cost = [1 ...

  4. [ZJOI2008]生日聚会 BZOJ1037 dp

    题目描述 今天是hidadz小朋友的生日,她邀请了许多朋友来参加她的生日party. hidadz带着朋友们来到花园中,打算坐成一排玩游戏.为了游戏不至于无聊,就座的方案应满足如下条件: 对于任意连续 ...

  5. 统计元音(stringstream的-应用)

    Problem Description 统计每个元音字母在字符串中出现的次数.   Input 输入数据首先包括一个整数n,表示测试实例的个数,然后是n行长度不超过100的字符串.   Output ...

  6. [转]cron语法

    最近在搞whenever时看到可以用cron语法设置定时任务.所以研究了下cron 语法. every '0 0 27-31 * *'do command "echo 'you can us ...

  7. [Leetcode]014. Longest Common Prefix

    public class Solution { public String longestCommonPrefix(String[] strs) { if(strs == null || strs.l ...

  8. Luogu P1120 小木棍 [数据加强版] 来来来我们一起来剪枝,剪枝,剪枝、、、

    好啊...太棒了... dfs(拼到第几根木棍,这根木棍剩余长度,上一根木棍的位置) len是木棍的长度,cnt是木棍的个数 震撼人心的剪枝: 1.枚举长度从最大的木棍开始,直到sum/2,因为之后只 ...

  9. 2015苏州大学ACM-ICPC集训队选拔赛(1) 1006

    取金币 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submissio ...

  10. linux 中iscsi服务

      ###############第一步: 创建一个2G的分区第二步: yum install targetcli -y 第三步:创建一个2G的分区,并同步  第四步:  执行tagetclils查看 ...