下面是几种常见的水平垂直居中方式,可在不同情形下方便采用不同的方式

html

 <body><div class="box"><div class="content"></div></div></body>

共同的css

     .content{width: 50%;height: 50%;margin: 0 auto;background-color: tomato;}

1.最简单的margin:auto水平居中

这里box里的overflow:hidden的作用及具体原因见   http://blog.csdn.net/oiu1010110/article/details/53192048

        .box{width: 400px;height: 400px;margin: 50px auto;background-color: palegreen;overflow: hidden;/*这里是简单的防止边界叠加,若不加,box的margin-top将变成70px*//*padding-top: 1px;*//*border: 1px solid transparent;*//*float: left;*/}.content{width: 50%;height: 50%;margin: 70px auto;background-color: tomato;}    

2.absolute+margin  水平垂直居中

     .box {width: 400px;height: 400px;margin: 50px auto;background-color: palegreen;position: relative;}.content{width: 80%;/*宽高可以随便变*/height: 80%;margin: auto; /*水平居中*/position: absolute;/*垂直居中*/left: 0;right: 0;bottom: 0;top: 0;background-color: tomato;}

3. left:50%+top:50%  margin为自己宽高的一半 垂直水平居中

情况1:

        .box{width: 400px;height: 400px;margin: 50px auto;background-color: palegreen;overflow: hidden;/*同1中的,这种情况下不可以省*//*padding-top: 1px;*//*border: 1px solid transparent;*//*float: left;*/}.content{position: relative;  width:200px; height:100px;left: 50%;margin-left: -100px;top: 50%;margin-top: -50px;background-color: tomato;}    

     .box{width: 400px;height: 400px;margin: 50px auto;background-color: palegreen;overflow: hidden;/*padding-top: 1px;*//*border: 1px solid transparent;*//*float: left;*/}/*margin-top 这里有两个坑:1.百分比表示的时候参照的是父元素的宽;2.当一个元素包含在另一个元素中时(假设没有填充或边框将边界分隔开),它们的顶和/或底边界会发生叠加3.只有普通文档流中块框的垂直边界才会发生边界叠加。行内框、浮动框或绝对定位框之间的边界不会叠加。*/.content {width: 50%;height: 50%; /*参照 父元素的height*/margin-top: -25%;/*参照 父元素的width*/margin-left: -25%;position: relative;top: 50%;left: 50%;background-color: tomato;}

情况2:

     .box{width: 400px;height: 400px;margin: 50px auto;background-color: palegreen;position: relative;}.content {position: absolute;  width:200px; height:100px;left: 50%;margin-left: -100px;top: 50%;margin-top: -50px;background-color: tomato;}

4.absolute与translate 垂直水平居中

     .box{width: 400px;height: 400px;margin: 50px auto;background-color: palegreen;position: relative;}.content {width: 50%;height: 50%;position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);background-color: tomato;}    

5.display:flex 垂直水平居中

     .box{width: 400px;height: 400px;margin: 50px auto;background-color: palegreen;display: flex;justify-content: center;/*水平居中*/align-items: center;/*垂直居中*/}.content {width: 50%;height: 50%;background-color: tomato;}

6.line-height+height 垂直水平居中

 <body><div class="content">我要垂直水平居住</div></body>
     div,body{padding: 0;margin: 0;}body{background-color: darkgrey;}.content{width: 200px;height: 200px;margin: 50px auto;background-color: palegreen;text-align: center;line-height: 200px;vertical-align: middle;}

注意:用line-height等于height垂直居中  只能用固定的px ,不可以用百分比,因为line-height参照的自身字体的大小,而不是height

效果图:

7.display:flex 嵌套使用垂直水平居中

<body><div class="box"><div class="content">我要垂直居中</div></div>
</body>
     div,body{padding: 0;margin: 0;}body{background-color: darkgrey;}.box{width: 600px;height: 400px;margin: 50px auto;background-color: palegreen;display: flex;/*这里用了css3的flex新特性垂直水平居中*/justify-content: center;/*水平居中*/align-items: center;/*垂直居中*/}.content {width: 50%;height: 20%;display: flex;/*嵌套使用flex*/justify-content: center;align-items: center;background-color: tomato;}

效果图:

说明:红色块在绿色块中居中,红色块中的文字垂直水平居中

css 水平垂直居中的几种常见方式相关推荐

  1. html 水平垂直居中,css水平垂直居中有几种实现方式?

    项目中经常碰到需要实现水平垂直居中的样式.下面就总结几种常用的方法 css水平垂直居中有几种实现方式? 1.水平对齐+行高 [思路一]text-align + line-height实现单行文本水平垂 ...

  2. CSS水平垂直居中的几种实现方式

    水平垂直居中 1.利用 `position:absolute` 2. 利用`margin:auto` 3. 利用弹性盒子 4. 利用水平对齐和行高 5. 最简便的方法 1.利用 position:ab ...

  3. CSS水平垂直居中的五种方式

    CSS水平垂直居中的几种方式 absolute and -margin 需要设置盒子的宽度和高度 .father{border:1px solid red;width: 80vh;height: 90 ...

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

    我们都知道,固定宽高的 div 在网页中水平垂直居中很简单,相信大家也很容易的写出来,但是不固定宽高的 div 如何水平垂直居中呢?我们在网页布局,特别是手机等 web 端网页经常是不固定宽高的 di ...

  5. css居中怎么移动,移动端css水平垂直居中

    水平垂直居中 1.margin 负值调整偏移实现 兼容性: 当前流行的使用方法. .box{ width: 100%; height: 100%; } .content{ position: abso ...

  6. css设置子盒子水平垂直居中(四种方式)

    css设置子盒子水平垂直居中(四种方式) 1. 使用绝对定位 <!DOCTYPE html> <html><head><meta charset=" ...

  7. CSS | 水平垂直居中都有哪几种方式

    我把一个子元素在父元素中水平垂直居中的实现方式分为三类,第一类是宽度已知,第二类是宽度未知,第三类是图片水平垂直居中. 第一类 宽度已知 第一种方式:采用绝对定位 原理 元素开启绝对定位后,水平方向和 ...

  8. css实现水平垂直居中的七种方式

    css实现水平垂直居中的七种方式 一.使用grid布局 二.使用flex布局 三.使用定位+外边距 四.使用定位+平移 五.使用外边距 + 平移 六.使用文本对齐 + 行高 七.使用表格单元 一.使用 ...

  9. css水平垂直居中四种常用方式

    css水平垂直居中 第一种:flex布局水平垂直居中 思路: 给父盒子display属性设置flex值,然后使用justify-content与align-item属性进行居中. 参考:阮一峰的fle ...

最新文章

  1. 局部敏感哈希(Locality sensitive hash) [3]—— 代码篇
  2. 安装Windows Vista
  3. 基于 Ubuntu 系统安装 CUDA 和 cuDNN
  4. Android开发之--Preferences的使用
  5. reverseajax(comet) socket 杂记
  6. python队列是线程安全的吗_python – 为什么我的多进程队列看起来不是线程安全的?...
  7. 简易拨号器iCall
  8. 小学计算机课每周几节,小学信息技术课时多少
  9. No identifier specified for entity没有为实体指定标识符
  10. SLF4J:Failed to load class org.slf4j.impl.StaticLoggerBinder
  11. 优化基于ExtJS 4.1的应用
  12. Java学习笔记1.1.2 搭建Java开发环境 - 安装配置JDK
  13. 总结1-深度学习-基础知识学习
  14. SAP License:制造企业信息化新动向
  15. ACL2020 | 词向量性别偏见
  16. 药店零售管理php系统,医药POS零售管理系统
  17. X86架构基本汇编指令详解
  18. Mikrotik ROS软路由设置上网方式(三)
  19. 案例丨GW-PBM-PN网关将Profibus-DP从站设备集成入PROFINET网络
  20. DOSBox 0.74 汇编 out of memery test.asm(2):out of memory

热门文章

  1. linux和xp区别,请教:谁能简单的说一下,linux系统与xp系统的主要区别吗?
  2. 使用向量的方法计算点到直线的距离
  3. 模板合集TOP50!覆盖8大行业领域,20+业务场景,打包好了直接送
  4. REMIND Your Neural Network to Prevent Catastrophic Forgetting 翻译
  5. 互联网寒潮,特送来 “App测试面试题“ 暖汤一份
  6. 无头结点单链表的逆置_无头节点的链表.PPT
  7. STC 51单片机42——汇编 定时器 舵机
  8. 达梦数据库导入导出dmp文件的常见方式
  9. 用unity做游戏用java_Unity游戏开始崩溃
  10. Excel VBA:删除行、列或单元格