在布局的时候经常能用到居中,在此总结一下

html结构:

<div class="wrap">
<div class="content"></div>
</div>

1.水平居中:margin:auto;

.wrap{
width: 400px;
height: 400px;
background-color: lightblue;
}
.content{
width: 200px;
height: 200px;
background-color: lightcoral;
/*margin:auto只能水平居中*/
margin: auto;
}

此方法简单只需一行代码,但是只能水平居中,至于为什么垂直居中不起作用呢:默认宽度继承父级宽度,而高度不能继承可以由内容撑开,居中计算时是相对父级计算因此垂直居中不起作用

效果如下:

2.垂直居中:table-cell

.wrap{
   width: 400px;
   height: 400px;
   background-color: lightblue;
   /* 将父元素的display设置为table-cell*/
   display: table-cell;                
   vertical-align: middle;
}
.content{
   width: 200px;
   height: 200px;
   background-color: lightcoral;
}

这里要强调一点就是display:table-cell是对父元素设置的,在表格单元中,vertical-align属性会设置单元格框中的单元格内容的对齐方式。这里设置为middle居中对齐,有兴趣的小伙伴也可以试试其他值如:top,bottom等。

效果如下:

3.一个水平居中,一个垂直居中放在一起是不是就可以垂直水平居中了

.wrap{
width: 400px;
height: 400px;
background-color: lightblue;
display: table-cell;
vertical-align: middle;
}
.content{
width: 200px;
height: 200px;
background-color: lightcoral;
margin: auto;
}

效果图:

bingo就是这么完美

4.绝对定位(高度未知)

.wrap{
width: 400px;
height: 400px;
background-color: lightblue;
/*父元素相对定位*/
position: relative;
}
.content{
width: 200px;
height: 200px;
background-color: lightcoral;
/*子元素绝对定位*/
position: absolute;
/*子元素分别相对top,left偏移50%*/
top:50%;
left: 50%;
/*子元素相对自身top,left分别移动-50%*/
transform: translate(-50%,-50%);
}

效果图:

上面两种方法优点在于可以在高度未知的情况下使用,下面这种方法就要知道子元素高度

5.绝对定位(已知子元素宽高的情况下) 调整负margin值

.wrap{
width: 400px;
height: 400px;
background-color: lightblue;
/*父元素相对定位*/
position: relative;
}
.content{
width: 200px;
height: 200px;
background-color: lightcoral;
/*子元素绝对定位*/
position: absolute;
/*子元素分别相对top,left偏移50%*/
top:50%;
left: 50%;
/*子元素相对自身top,left分别移动负100像素*/
margin-top:-100px;
margin-left:-100px;
}

效果图与上面一种方法一样就不在放了。

6.弹性盒模型:display:flex;

.wrap{
width: 400px;
height: 400px;
background-color: lightblue;
display:flex;
}
.content{
width: 200px;
height: 200px;
background-color: lightcoral;
margin:auto;
}

flex兼容性问题可参考下图:

7.单行文本居中:line-height

html结构:

<div class="content">一切都是最好的安排</div>

css样式:

.content{
width: 200px;
height: 200px;
background-color: lightcoral;
line-height: 200px;
}

效果图:

8.多行文本就不能用line-height了,还可以用上面table-cell方法来使多行文本居中

.content{
width: 200px;
height: 200px;
background-color: lightcoral;
display:table-cell;
vertical-align:middle;
}

可以直接在div(.content)中输入文本或者文本输入在一个标签里

更新与2016-12-30

9.text-align:center+inline-block文本居中

text-align属性用于控制文字的对其与显示,从其渲染与解析上来看,其主要是用来控制inline水平元素或inline-block元素的对齐与显示的,例如嵌套行内标签的文字、图片、input表单控件等;而对block水平的元素是没有作用的。

  *{
margin:;
padding:;
}
div{
width:400px;
height:400px;
background-color: #00AA88;
}
ul{
font-size:;
/*因为li标签inline-block后li之间默认有3px间隙,font-size设为0去除间隙*/
text-align: center;
}
li{
font-size: 16px;
display: inline-block;
border:1px solid #000;
}

效果如下:

稍加修改可以作为分页标签来使用。

CSS垂直水平居中方法总结的更多相关文章

  1. CSS垂直水平居中方法整理

    CSS定位中常常用到垂直居中,比如覆盖层上的弹框. 兼容性比较好的方法: <!DOCTYPE html PUBliC "-//W3C//DTD XHTML 1.0 Transition ...

  2. 关于css垂直水平居中的几种方式

    css中元素的垂直水平居中是比较常见及较常使用的,在这里向大家介绍一下几种方式. 1.水平居中 margin: 0 auto; 效果图: 而文字的垂直水平居中也比较简单,加上line-height: ...

  3. css 垂直+水平居中

    垂直+水平居中是一个老生常谈的问题了,现在就固定高度和不固定高度两种情况去讨论 1.父盒子固定高度[定位] 实现1: father-box: position:relative child-box:p ...

  4. CSS之垂直水平居中方法

    //居中方法1 position: absolute; top: 0; left: 0; right: 0; bottom: 0; margin: auto; width: 346px; height ...

  5. css 垂直水平居中总结

    一.前言: 垂直居中有很多方式,我们要做的不是写出完美代码,而是在合适的情况下根据需求选择合适方式. 主要方式: line-height 绝对定位 表格 display:table-cell 主要需求 ...

  6. css垂直水平居中方案

    1. 水平居中 如果是inline元素:在父元素上面设置text-align:center; 如果是block元素:设置宽度和margin:0 auto; 如果是多块级元素:在父元素上面设置text- ...

  7. (转载)css垂直水平居中的整理

    方法一 .demo1 { width:180px; height:180px; line-height:180px; *font-size:160px; border:1px solid #ddd; ...

  8. CSS垂直水平居中

    小小的总结一下:行内元素水平居中用text-align: center;块级元素水平居中用margin-left: auto; margin-right: auto; 首先讨论一下单行时的情况. 毫无 ...

  9. div块元素垂直水平居中方法总结

    1.已知块级元素的宽和高,使用绝对定位+外边距设定水平垂直居中. 父元素position:relative,子元素position:absolute;top:50%;left:50%;margin-t ...

随机推荐

  1. check time period

    /**     * @author etao     * @description check last time selected     * @param timePeriod     * @pa ...

  2. php 导出excle的.csv格式的数据时乱码问题

    1.header('Content-Encoding: XXXX'); 有可能是编码问题:可以尝试UTF-8,GBK,GB2312,等编码格式 2.有可能是文件编码问题,虽然UTF-8不建议带BOM, ...

  3. 协议分析TMP

    最近闲来有事, 分析了一个非常低端(非常低端的意思是说你不应该对她是否能取代你现有的QQ客户端作任何可能的奢望,她只是一个实验性的东西)的手机QQ的协议, 是手机QQ3.0,      所用到的TCP ...

  4. 修改MS SQL忽略大小写 分类: SQL Server 数据库 2015-06-19 09:18 34人阅读 评论(0) 收藏

    第一步:数据库->属性->选项->限制访问:SINGLE_USER 第二步:ALTER DATABASE [数据库名称] collate Chinese_PRC_CI_AI 第三步: ...

  5. 【leetcode】Pascal's Triangle II

    题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...

  6. Android 登录界面与首页的设计

    全屏效果 //取消标题,取消状态栏 this.requestWindowFeature(Window.FEATURE_NO_TITLE); this.getWindow().setFlags(Wind ...

  7. shell(一)

    #服务器之间拷贝数据 scp  -r   本地文件目录    系统用户名@IP:目标文件夹路径

  8. Nginx %00空字节执行php漏洞

    Nginx如下版本:0.5.*, 0.6.*, 0.7 <= 0.7.65, 0.8 <= 0.8.37在使用PHP-FastCGI执行php的时候,URL里面在遇到%00空字节时与Fas ...

  9. redmine整合GIT版本库

    redmine整合GIT版本库   服务器的环境: Ubuntu 11.10 64位 Redmine 1.4.5.stable.10943 git version 1.7.5.4 + gitolite ...

  10. Android进阶系列之源码分析Activity的启动流程

    美女镇楼,辟邪! 源码,是一个程序猿前进路上一个大的而又不得不去翻越障碍,我讨厌源码,看着一大堆.5000多行,要看完得啥时候去了啊.不过做安卓的总有这一天,自从踏上这条不归路,我就认命了.好吧,我慢 ...