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%;
}

  

最新文章

  1. 【新手出发】从搭虚拟机开始,一步一步在CentOS上跑起来.Net Core程序
  2. 拒绝了对对象 &#39;base_config&#39; (数据库 &#39;****&#39;,架构 &#39;dbo&#39;)的 SELECT 权限
  3. Android 中onSaveInstanceState和onRestoreInstanceState学习
  4. 系统级I/O 第八周11.9~11.15
  5. 《黄聪:手机移动站SEO优化教程》2、PC端和手机移动端SEO优化区别
  6. Java基础知识强化102:线程间共享数据
  7. ios开发——实战OC篇&amp;SQLite3的实际应用
  8. 创建第一个freemarker
  9. hive premanent udf 发布...
  10. cmd命令行
  11. SqlServer变量
  12. 五、xadmin自定义插件2
  13. Web.config配置customErrors mode为Off后依然不显示具体错误的可能原因。
  14. Java Application和Java Applet的区别
  15. makemigrations migrate
  16. 【DeepLearning】Exercise:Learning color features with Sparse Autoencoders
  17. Python2.7环境下安装pydbg
  18. 【BZOJ4231】回忆树 离线+fail树+KMP
  19. Mybatis Dao开发的两种方式(一)
  20. Java 连接 Oracle 数据库

热门文章

  1. AAC DRC
  2. 关于Vector3.forward和Transform.forward
  3. springboot12(rabbitmq)
  4. 如何通过源码包的方式在linux安装python36
  5. 【HTTP与HTTPS的区别】
  6. 根据IP地址查找MAC地址
  7. Python学习之函数篇
  8. 企业级Docker镜像仓库Harbor部署与使用
  9. 吴裕雄 python 神经网络——TensorFlow实现搭建基础神经网络
  10. 【PAT甲级】1084 Broken Keyboard (20 分)