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

一、水平居中

01 行内元素 text-align: center;

.parent {
text-align: center;
}

02 块级元素 margin: auto;

注意:低版本的浏览器还需要设置text-align:center;

.parent {
text-align: center;
}
.child {
margin: auto;
border: 1px solid blue;
}

二、垂直居中

01 行内元素(单行文字垂直居中):设置 line-height = height;

.parent {
height: 200px;
line-height: 200px;
border: 1px solid red;
}

02 块级元素:绝对定位(需要提前知道尺寸)

优点:兼容性不错

缺点:需要提前知道尺寸,可拓展性和自适应性不好

.parent {
position: relative;
height: 200px;
}
.child {
width: 80px;
height: 40px;
background: blue;
position: absolute;
left: 50%;
top: 50%;
margin-top: -20px;
margin-left: -40px;
}

03 块级元素:绝对定位 + transform

优点:不需要提前知道尺寸
缺点:兼容性不好,只支持ie9+浏览器,而且还会引发一些其他的奇怪的兼容问题

.parent {
position: relative;
height: 200px;
}
.child {
width: 80px;
height: 40px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: blue;
}

04 块级元素:绝对定位 + margin: auto;

优点:不需要提前知道尺寸,兼容性好
缺点:我目前还没有遇到。
此方法出自张鑫旭老师的博客,我也是了解了之后,才运用到实际开发中的

.parent {
position: relative;
height: 200px;
}
.child {
width: 80px;
height: 40px;
position: absolute;
left:;
top:;
right:;
bottom:;
margin: auto;
background: blue;
}

05 块级元素:padding

缺点:如果高度固定,需要提前计算尺寸(只在某些特定情况适用)。

.parent {
padding: 5% 0;
}
.child {
padding: 10% 0;
background: blue;
}

06 块级元素:display: table-cell

父元素一定要设置display:table,这样子元素的table-cell才能生效

.parent {
width: 600px;
height: 200px;
border: 1px solid red;
display: table;
}
.child {
display: table-cell;
vertical-align: middle;
}

07 块级元素:display: flex(移动端页面推荐)

缺点:兼容性不好

.parent {
width: 600px;
height: 200px;
border: 1px solid red;
display: flex;
align-items: center;
justify-content: center; /*水平居中*/
}
.child {
background: blue;
}

08 块级元素:伪元素

这个方案是从张鑫旭大神的新书:《css世界》里读到的

vertical-align这个属性需要与同元素内的行内元素的基线为参考,高度100%自然就以高度50%处即平常所说的中线为基线,middle默认对齐其基线,自然也就垂直居中对齐了

.parent {
width: 300px;
height: 300px;
border: 1px solid red;
text-align: center;
}
.child {
background: blue;
width: 100px;
height: 40px;
display: inline-block;
vertical-align: middle;
}
.parent::before {
content: '';
height: 100%;
display: inline-block;
vertical-align: middle;
}

09 块级元素:inline-block

HTML代码:

<div class="parent">
<div class="child">child</div>
<div class="brother">brother</div>
</div>

CSS代码:

.parent {
width: 400px;
height: 400px;
border: 1px solid red;
position: relative;
}
.child, .brother {
display: inline-block;
vertical-align: middle;
}
.child {
background: blue;
font-size: 12px;
}
.brother {
height: 400px;
font-size:;
}

其他

table布局是十年前前端最常用的布局方式,最省心但是冗余也很多,结构嵌套也比较深。现在前端变化日新月异,不推荐使用

<table>
<tr>
<td align="center" valign="middle">content</td>
</tr>
</table>

希望上述的总结对相关的同学能起到参考性的作用。

css设置居中的方案总结的更多相关文章

  1. 6 种CSS设置居中的方法

    原文 Demos of each of the methods below by clicking here. Horizontal centering with css is rather easy ...

  2. 关于HTML+CSS设置图片居中的方法

    有的时候我们会遇到这样一个问题,就是当我们按下F12进行使用firebug调试的时候,我们发现图像没有居中,页面底下有横向的滑动条出现,图片没能够居中,默认状态下只是紧靠在页面最左侧,而我们对图像缩小 ...

  3. 几个简单的css设置问题:div居中,ul li不换行 ,内容超出自动变省略号等

    1  div在页面居中的问题 1)position值为relative时(相对定位),css设置属性margin:0 auto;(0 auto,表示上下边界为0,左右则根据宽度自适应相同值,即居中)即 ...

  4. css设置图片居中、居左、居右

      CreateTime--2017年12月8日14:25:09 Author:Marydon css设置图片居中.居左.居右 图片一般默认是居左显示的,如何更改它的水平位置呢? <div st ...

  5. CSS水平和垂直居中方案

    我们在网页布局的时候,经常会碰到需要居中的情况,那下面就来讲一下有哪几种目前比较常用的居中方案,它们的优点和缺点分别又是什么. 一.水平居中   方法①:(父元素)text-align,(子元素)in ...

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

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

  7. CSS设置技巧

    一.单位和值 1.1 颜色值 在网页中的颜色设置是非常重要,有字体颜色(color).背景颜色(background-color).边框颜色(border)等,设置颜色的方法也有很多种: 1.英文命令 ...

  8. css常用居中

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

  9. CSS 图像居中对齐

    CSS  图像居中对齐 我们在<CSS 内外边距>学过内容居中,它的原理是将外边左右设置为auto.图像居中也是这个原理. 示例 <!DOCTYPE html> <htm ...

随机推荐

  1. poj1265&&2954 [皮克定理 格点多边形]【学习笔记】

    Q:皮克定理这种一句话的东西为什么还要写学习笔记啊? A:多好玩啊... PS:除了蓝色字体之外都是废话啊...  Part I 1.顶点全在格点上的多边形叫做格点多边形(坐标全是整数) 2.维基百科 ...

  2. Java流机制学习

    基本概念 BaseStream 基础流是一个可行并行或者串行的汇聚操作的元素序列.可以进行顺序遍历,也可以进行并发遍历.通过它也可以得到一个并行流或者串行流. Stream 是Java中流的表现接口, ...

  3. qt事件机制---事件范例

    在笔记qt课程04笔记中

  4. Python基础——输出[print()]与输入[input()]

    Python版本:3.6.2  操作系统:Windows  作者:SmallWZQ Python是一种面向对象的解释型计算机程序设计语言,Python的特点是"简单"." ...

  5. MySQL 存储过程的简单使用

    首先创建一张 students 表 SQL脚本如下: create table students( id int primary key auto_increment, age int, name v ...

  6. Swift百万线程攻破单例(Singleton)模式

    一.不安全的单例实现 在上一篇文章我们给出了单例的设计模式,直接给出了线程安全的实现方法.单例的实现有多种方法,如下面: class SwiftSingleton { class var shared ...

  7. 配置可以通过http协议访问的svn服务器

    通过HTTP协议访问版本库是Subversion的亮点之一,这种方式具备许多svnserve服务器所没有的特性,使用上更加灵活. 关于mode_day_svn模块: 由于Subversion需要版本化 ...

  8. OpenCV角点检测goodFeaturesToTrack()源代码分析

    上面一篇博客分析了HARRIS和ShiTomasi角点检测的源代码.而为了提取更准确的角点,OpenCV中提供了goodFeaturesToTrack()这个API函数,来获取更加准确的角点位置.这篇 ...

  9. CENTOS6.6下zabbix2.4.7搭建

    本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 安装依赖 安装Perl .apr / apr-util yum -y ...

  10. Redis基础及入门

    一. 什么是 Redis            Redis 是一个可基于内存,有着完备的持久化机制并以 Key-Value 形式存储的非关系型数据库.也称为数据结构服务器.    二. Redis 的 ...