1、元素水平居中

当然最好使的是:

margin: 0 auto;

居中不好使的原因:
1、元素没有设置宽度,没有宽度怎么居中嘛!
2、设置了宽度依然不好使,你设置的是行内元素吧,行内元素和块元素的区别以及如何将行内元素转换为块元素请看我的另一篇文章!
示例 1:

<div class="box">
<div class="content">
哇!居中了
</div>
</div> <style type="text/css">
.box {
background-color: #FF8C00;
width: 300px;
height: 300px;
margin: auto;
}
.content {
background-color: #F00;
width: 100px;
height: 100px;
line-height: 100px;//文字在块内垂直居中
text-align: center;//文字居中
margin: 0 auto;
}
</style>

、元素水平垂直居中

方案1:position 元素已知宽度 
父元素设置为:position: relative; 
子元素设置为:position: absolute; 
距上50%,据左50%,然后减去元素自身宽度的距离就可以实现

<div class="box">
<div class="content">
</div>
</div> .box {
background-color: #FF8C00;
width: 300px;
height: 300px;
position: relative;
}
.content {
background-color: #F00;
width: 100px;
height: 100px;
position: absolute;
left: 50%;
top: 50%;
margin: -50px 0 0 -50px;
}

方案2:position transform 元素未知宽度 
如果元素未知宽度,只需将上面例子中的margin: -50px 0 0 -50px;替换为:transform: translate(-50%,-50%); 
效果如上! 
示例 3:

<div class="box">
<div class="content">
</div>
</div> .box {
background-color: #FF8C00;
width: 300px;
height: 300px;
position: relative;
}
.content {
background-color: #F00;
width: 100px;
height: 100px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}

方案3:flex布局 :父元素加

  1. display: flex;
  2. justify-content: center;
  3. align-items: center;
<div class="box">
<div class="content">
</div>
</div> .box {
background-color: #FF8C00;
width: 300px;
height: 300px;
display: flex;        //flex布局
justify-content: center;  //使子项目水平居中
align-items: center;    //使子项目垂直居中
}
.content {
background-color: #F00;
width: 100px;
height: 100px;
}

 

方案4:table-cell布局 
示例 5: 
因为table-cell相当与表格的td,td为行内元素,无法设置宽和高,所以嵌套一层,嵌套一层必须设置display: inline-block;td的背景覆盖了橘黄色,不推荐使用

<div class="box">
<div class="content">
<div class="inner">
</div>
</div>
</div> .box {
background-color: #FF8C00;//橘黄色
width: 300px;
height: 300px;
display: table;
}
.content {
background-color: #F00;//红色
display: table-cell;
vertical-align: middle;//使子元素垂直居中
text-align: center;//使子元素水平居中
}
.inner {
background-color: #000;//黑色
display: inline-block;
width: 20%;
height: 20%;
}

  

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

  1. CSS水平垂直居中常见方法总结2

    1.文本水平居中line-height,text-align:center(文字)元素水平居中 margin:0 auo 方案1:position 元素已知宽度 父元素设置为:position: re ...

  2. 53.CSS---CSS水平垂直居中常见方法总结

    CSS水平垂直居中常见方法总结 1.元素水平居中 当然最好使的是: margin: 0 auto; 居中不好使的原因: 1.元素没有设置宽度,没有宽度怎么居中嘛! 2.设置了宽度依然不好使,你设置的是 ...

  3. CSS水平垂直居中的方法

    原文链接:http://caibaojian.com/370.html 水平垂直居中,特别是使用在列表的时候经常会用到的,以前有需求的时候我也做过类似的代码,是使用display:table-cell ...

  4. 介绍一种css水平垂直居中的方法(非常好用!)

    这次介绍一下一个水平垂直居中的css方法,这个方法可以说是百试百灵,废话不多说,直接附上代码: html,body{ width:100%; height:100%; } 你需要居中的元素{ posi ...

  5. css水平垂直居中的方法与 vertical-align 的用法

    前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! 1. 已知元素宽度 方法一:已知宽高,可以用position定位 + margin负值的方法 : 绝对定位 ...

  6. css 水平垂直居中的方法总结

    在项目中经常会遇到设置元素水平垂直居中的需求.而且具体的场景也不同,所以将个人总结的方法做个汇总,希望对浏览者有用. 以下所举的例子都以一个html为准,这里规定好一些公用样式. body { bac ...

  7. [css]水平垂直居中的方法

    1.top:cale(50% - 2rem); left:cale(50% - 2rem);

  8. CSS水平垂直居中总结

    行内元素水平居中 把行内元素包裹在块级父元素中,且父元素中的css设置text-align:center; <!DOCTYPE html> <html> <head> ...

  9. CSS水平垂直居中的几种方法2

    直接进入主题! 一.脱离文档流元素的居中 方法一:margin:auto法 CSS代码: div{ width: 400px; height: 400px; position: relative; b ...

随机推荐

  1. 解决linux 中文乱码

    解决办法是在文件/etc/profile末尾添加一行 echo 'export LC_ALL="en_US.UTF-8"' >> /etc/profile source ...

  2. Custom LED Keychain, Small And Surefire Gifts

    The    LED Keychain    makes it easy for people to carry their keys with them and carry them with th ...

  3. Linux服务器时间设置及同步

    闲余:夏日将到,园区计划五一期间进行大面积的电网停电检修,运维同学因此将公司测试服务器提前关闭了.收假后,测试告诉我,他发现一个bug--一段定时任务程序未执行,我的第一反应就是--会不会是假期测试服 ...

  4. js实现左右自动滚动

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 删除数据高级用法:delete,truncate

    1.语法: delete 允许使用条件(删除符合条件的数据) 允许使用limit,限制删除的记录数.limit N     常见的是,limit配合order by来使用:先将结果排序,再删除固定数量 ...

  6. saas的资料

    互联网时代的软件革命-SaaS架构设计.叶伟.扫描版 提取码:jd5c 让云落地  云计算服务模式(SAAS.PAAS和IAAS)设计决策--Michael J.Kavis著提取码:ut24

  7. 干了这杯Java,让你的Idea比eclipse好用

    1.Idea基本配置 1.1 Idea简介 Idea是一个专门针对Java的集成开发工具(IDE),由Java语言编写.所以,需要有JRE运行环境并配置好环境变量.简单的说,Idea是写代码用的工具. ...

  8. php和redis怎么实现消息队列

    把瞬间服务器的请求处理换成异步处理,缓解服务器的压力,实现数据顺序排列获取.本文主要和大家分享php和redis如何实现消息队列,希望能帮助到大家. redis实现消息队列步骤如下: 1).redis ...

  9. AT24C02芯片学习记录

    1.首先看AT24C02芯片的引脚说明 2.芯片的型号与存储容量(bit)的对应关系: 3.总线时序 我对时序的理解: 时钟线分两种:一种是外部时钟源控制时钟线低电平持续多久高电平持续多久,就像串口: ...

  10. LAMP源码编译

    ---恢复内容开始--- 1.LAMP源码编译的基础环境 安装组建包:yum groupinstall "Development Tools" "Development ...