css对齐方案总结

垂直居中

通用布局方式(内敛元素和块状元素都适用)

  1. 利用flex:
    核心代码:

    1
    2
    3
    4
    5
    .container{
    display:flex;
    flex-direction:column;
    justify:center
    }
  2. 利用transformX(-50%):
    核心代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    .container{
    width: 300px;
    height: 300px;
    background: red;
    position:relative;
    }
    .child{
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    -webkit-transform: translateY(-50%);
    }

内敛元素的垂直居中

单行内敛元素:设置内敛元素的高度和行高相等
核心代码:

1
2
3
4
.container {
height: 120px;
line-height: 120px;
}

块状元素

  1. 固定元素高度的块状元素
    核心代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    .container{
    position: relative;
    }
    .child{
    position: absolute;
    top: 50%;
    height: 100px;
    margin-top: -50px;
    }
  2. 未知高度的块状元素
    当垂直居中的元素的高度和宽度未知时,我们可以借助CSS3中的transform属性向Y轴反向偏移50%的方法实现垂直居中。但是部分浏览器存在兼容性的问题。
    核心代码:

    1
    2
    3
    4
    5
    6
    7
    8
    .container {
    position: relative;
    }
    .child {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    }

水平居中

通用布局方式

  1. flex布局
    核心代码:

    1
    2
    3
    4
    .container{
    display: flex;
    justify-content: center;
    }
  2. absoulte+transform
    核心代码:

    1
    2
    3
    4
    5
    6
    7
    8
    .container{
    position:relative;
    }
    .child{
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    }

内敛元素水平居中

  1. text-align:center
    核心代码:

    1
    2
    3
    .container{
    text-align:center
    }

块状元素水平居中

  1. 使用 margin:0 auto 必须注明子元素和父元素的宽度
    核心代码:

    1
    2
    3
    .container{
    margin:0 auto
    }
  2. 多块状元素:
    利用内敛元素布局方式container属性为text-align:center;
    核心代码:

    1
    2
    3
    4
    5
    6
    .container{
    text-align: center;
    }
    .child{
    display: inline-block;
    }

水平垂直居中

固定宽高元素水平垂直居中

通过margin平移元素整体宽度的一半,使元素水平垂直居中。
核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
.container {
position: relative;
}
.child {
width: 300px;
height: 100px;
padding: 20px;
position: absolute;
top: 50%;
left: 50%;
margin: -70px 0 0 -170px;
}

未知宽高元素水平垂直居中

  1. 利用2D变换,在水平和垂直两个方向都向反向平移宽高的一半,从而使元素水平垂直居中。
    核心代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    .parent {
    position: relative;
    }
    .child {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    }
  2. 利用flex布局
    利用flex布局,其中justify-content 用于设置或检索弹性盒子元素在主轴(横轴)方向上的对齐方式;而align-items属性定义flex子项在flex容器的当前行的侧轴(纵轴)方向上的对齐方式。
    核心代码:

    1
    2
    3
    4
    5
    .container {
    display: flex;
    justify-content: center;
    align-items: center;
    }

相对于 body 的水平垂直居中

  1. 列表布局(兼容性好)
    核心代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    .outer {
    display: table;
    position: absolute;
    height: 100%;
    width: 100%;
    } .middle {
    display: table-cell;
    vertical-align: middle;
    } .inner {
    margin-left: auto;
    margin-right: auto;
    width: 400px;
    }
  2. position 布局
    核心代码

    1
    2
    3
    4
    5
    6
    7
    8
    .container{
    position: absolute;
    margin: auto;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    }

css对齐方案总结的更多相关文章

  1. webpack css压缩方案

    css-loader原有的minimize选项在1.0.0版本已经移除,不能使用其进行css压缩. 目前可行的css压缩方案有: 1. postcss-loader with cssnano or u ...

  2. 最新CSS兼容方案

    CSS兼容方案,让你的样式完美兼容各大浏览器,这个方案主要是用来解决Safari的,Opear是在测试的时候顺带试了一下,结果发现竟然也行,所以顺便也解决了. .e {/*FF OP*/ color: ...

  3. css对齐

    2016-10-25 <css入门经典>第15章 1.text-align属性: 块属性内部的文本对齐方式.该属性只对块盒子有意义,内联盒子的内容没有对齐方式.(注意:只是盒子内部的内容对 ...

  4. CSS预编译与PostCSS以及Webpack构建CSS综合方案

    CSS全称Cascading Style Sheets(层叠样式表),用来为HTML添加样式,本质上是一种标记类语言.CSS前期发展非常迅速,1994年哈肯·维姆·莱首次提出CSS,1996年12月W ...

  5. css垂直居中方案

    先介绍几种常见的垂直布局方式: 已知盒子具体宽度(宽度可以为百分比)(适用于居中浮动元素) 第一种: 给父元素相对定位,给子元素绝对定位 父布局 { position: relative; } 子布局 ...

  6. 【css对齐】块内或者行内图片与文字居中对齐最靠谱的方式!

    块内或者行内图片与文字居中对齐最靠谱的方式! 做图片与文字在一行的按钮时候最常用到,总结了一个靠谱的方法,终于可以完美的对齐下面给个代码 首先是html: <p class="btnU ...

  7. 聊一聊 React 中的 CSS 样式方案

    和 Angular,Vue 不同,React 并没有如何在 React 中书写样式的官方方案,依靠的是社区众多的方案.社区中提供的方案有很多,例如 CSS Modules,styled-compone ...

  8. 全场景效能平台猪齿鱼常用的前端css实现方案

    ​ 居中 最常用的height + line-height,以及margin:0 auto的居中方式就不再阐述,以下介绍两种容错性高的实现方案. flex布局实现 ​ 猪齿鱼前端日常开发中,我们多以f ...

  9. 猪齿鱼平台常用前端css实现方案

    居中 最常用的height + line-height,以及margin:0 auto的居中方式就不再阐述,以下介绍两种容错性高的实现方案. flex布局实现 ​ 猪齿鱼前端日常开发中,我们多以fle ...

随机推荐

  1. Map的四种遍历

    //Map的四种遍历方法 public static void main(String[] args) { Map<String, String> map = new HashMap< ...

  2. github routine

    1. 从官方库fork 自己的分支库后,git clone到local. 2. local的remotes/origin默认是自己的分支库.可以添加remotes/upstream指向官方库. 3. ...

  3. DX11 Without DirectX SDK--04 使用DirectX Tool Kit帮助开发

    回到 DirectX11--使用Windows SDK来进行开发 DirectX Tool Kit下载 DirectX Tool Kit是一个包含许多类的集合,用于为公共Windows平台编写Dire ...

  4. Unity文档阅读 第三章 依赖注入与Unity

    Introduction 简介In previous chapters, you saw some of the reasons to use dependency injection and lea ...

  5. SSM-SpringMVC-17:SpringMVC中深度剖析HandlerAdapter处理器适配器底层

     ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 先放一张图 很熟悉啊,之前就看过,我们之前已经把handlerMapping剖了个底朝天,顺着上次的进度,继 ...

  6. 搭建Hadoop完全分布式

    Centos7搭建hadoop完全分布式 虽然说是完全分布式,但三个节点也都是在一台机器上.拿来练手也只能这样咯,将就下.效果是一样滴.这个我自己都忘了步骤,一起来回顾下吧. 必备知识: Linux基 ...

  7. Thread部分总结以及小例子

    Thread总结:一直以来用thread比较多,一般会在同步以及ui中用到.以下对于经常用作为简单介绍.一 实现方法: 一种直接new thread,另外一种是实现Runnable接口,在创建thre ...

  8. ReactNative(三)——WebStorm的基本配置

    设置.js文件默认以jsx的语法打开 在没有进行设置的情况下,每次打开WebStorm的时候打开包含jsx语法的.js文件都会有以下提示: 当然我们点击转换后就可以了,但是每次都会提示,所以还是来一个 ...

  9. 你不知道的JavaScript--Item4 基本类型和基本包装类型(引用类型)

    1.基本类型和引用类型 基本的数据类型有5个:undefined,boolean,number,string,null typeof null; //"object" typeof ...

  10. Windows下python3和python2同时安装python2.exe、python3.exe和pip2、pip3设置

    1.添加python2到系统环境变量 打开,控制面板\系统和安全\系统,选择高级系统设置,环境变量,选择Path,点击编辑,新建,分别添加D:\Python\python27和D:\Python\py ...