详解 CSS 居中布局技巧

来源:互联网 发布:数据库设计与开发 下载 编辑:程序博客网 时间:2024/06/05 00:18

水平居中元素:

方式一:CSS3 transform

  1. .parent {

  2.    position: relative;

  3. }

  4. .child {

  5.    position: absolute;

  6.    left: 50%:

  7.    transform: translateX(-50%);

  8. }

方式二:flex 布局

  1. .parent {

  2.    display: flex;

  3.    justify-content: center;

  4. }

适用于子元素为浮动,绝对定位,内联元素,均可水平居中。

  • 居中的元素为常规文档流中的内联元素(display: inline)

常见的内联元素有:span, a, img, input, label 等等

  1. .parent {

  2.    text-align: center;

  3. }

  • 居中的元素为常规文档流中的块元素(display: block)

常见的块元素:div, h1~h6, table, p, ul, li 等等

方式一:设置 margin

  1. .parent {

  2.    width: 100%;

  3. }

  4. .child {

  5.    width: 600px;

  6.    height: 50px;

  7.    margin: 0 auto;

  8.    background: #999;

  9. }

方式二:修改为 inline-block 属性

  1. .parent {

  2.    text-align: center;

  3. }

  4. .child {

  5.    display: inline-block;

  6. }

  1. .child {

  2.    width: 100px;

  3.    float: left;

  4.    position: relative;

  5.    left: 50%;

  6.    margin-left: -50px;

  7. }

方式一:

  1. .parent {

  2.    position: relative;

  3. }

  4. .child {

  5.    position: absolute;

  6.    width: 100px;

  7.    left: 50%;

  8.    margin-left: -50px;

  9. }

方式二:

  1. .parent {

  2.    position: relative;

  3. }

  4. .child {

  5.    position: absolute;

  6.    width: 100px;

  7.    left: 0;

  8.    right: 0;

  9.    margin: 0 auto;

  10. }

垂直居中元素:

方式一:CSS3 transform

  1. .parent {

  2.    position: relative;

  3. }

  4. .child {

  5.    position: absolute;

  6.    top: 50%;

  7.    transform: translateY(-50%);

  8. }

方式二:flex 布局

  1. .parent {

  2.    display: flex;

  3.    align-items: center;

  4. }

适用于子元素为浮动,绝对定位,内联元素,均可垂直居中。

  1. .text {

  2.    line-height: 200px;

  3.    height: 200px;

  4. }

方式一:

  1. .parent {

  2.    position: relative;

  3. }

  4. .child{

  5.    position: absolute;

  6.    top: 50%;

  7.    height: 100px;

  8.    margin-top: -50px;

  9. }

方式二:

  1. .parent {

  2.    position: relative;

  3. }

  4. .child{

  5.    position: absolute;

  6.    top: 0;

  7.    bottom: 0;

  8.    height: 100px;

  9.    margin: auto 0;

  10. }

垂直居中元素:

  1. div {

  2.    width: 100px;

  3.    height: 100px;

  4.    margin: auto;

  5.    position: fixed;

  6.    //absolute is ok

  7.    top: 0;

  8.    right: 0;

  9.    bottom: 0;

  10.    left: 0;

  11. }

优点:

  1. 不仅可以实现在正中间,还可以在正左方,正右方

  2. 元素的宽高支持百分比 % 属性值和 min-/max- 属性

  3. 可以封装为一个公共类,可做弹出层

  4. 浏览器支持性好

  1. .child {

  2.    width: 100px;

  3.    height: 100px;

  4.    position: absolute;

  5.    top: 50%;

  6.    left: 50%;

  7.    margin-left: -50px;

  8.    margin-top: -50px;

  9. }

特点:

  1. 良好的跨浏览器特性,兼容 IE6 - IE7

  2. 灵活性差,不能自适应,宽高不支持百分比尺寸和 min-/max- 属性

  1. .child {

  2.    width: 100px;

  3.    height: 100px;

  4.    position: absolute;

  5.    top: 50%;

  6.    left: 50%;

  7.    transform: translate(-50%, -50%);  

  8. }

特点:

  1. 内容可自适应,可以封装为一个公共类,可做弹出层

  2. 可能干扰其他 transform 效果

  1. .parent {

  2.    display: flex;

  3.    justify-content: center;

  4.    align-items: center;

  5. }

这是 CSS 布局未来的趋势。Flexbox 是 CSS3 新增属性,设计初衷是为了解决像垂直居中这样的常见布局问题。

  1. text {

  2.    height: 100px;

  3.    line-height: 100px;

  4.    text-align: center;

  5. }


原文:https://zhuanlan.zhihu.com/p/25068655


原创粉丝点击