例如 一个父div(w:100%;h:400px)中有一个子div(w:100px;100px;)。让其上下左右居中。

方法一(varticle-align

理念

利用表格单元格的居中属性。

步骤

  • 父div外层配置一个div,同时设置为表格元素 (display: table),宽度为100%
  • 父div配置为表格单元格元素 (display: table-cell)
  • 父div配置居中属性(vertical-align: middle),使子div上下居中
  • 子div通过margin配置左右居中(margin-left:auto; margin-right:auto

例子

<style>
* {margin: 0; padding: 0; box-sizing: border-box;}
.table {display: table; width: 100%;}
.father {display: table-cell; vertical-align: middle;}
.son {margin: auto;}
</style>
<body>
<div class="table" >
<div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;">
<div class="son" style="width: 100px; height: 100px;background: palegreen;"></div>
</div>
</div>
</body>

注:

  • 表格单元格比较特殊,如果只有一个单元格时,它的宽度默认会占父级(table|tr)宽度的100%;
  • table默认宽度不会撑开,需要手动配置width:100%;

方法二(line-height

理念

当父div的行高等于自身高度时,内部的行内元素会上下居中显示。行内块没有固定高度时也会上下居中显示。通过文本居中属性text-align:center,可以使内部行内元素或行内块元素左右居中显示。

步骤

  • 子div设定为行内块元素(display:inline-block);
  • 父div设置行高(line-height)使子div上下居中
  • 父div设置文本居中(text-align:center)使子div左右居中。

例子

<style>
* {margin: 0; padding: 0; box-sizing: border-box;}
.father {line-height: 500px; text-align: center; font-size: 0;}
.son { display: inline-block;
/* display: inline-flex;
display: inline-grid;
display: inline-table; */
}
</style>
<body>
<div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;">
<div class="son" style="width: 100px; height: 100px;background: palegreen;"></div>
</div>
</body>

注: 行高如果设置为当前父div的高度(400px)的话,有固定高度的子div并不会居中显示的,问题出在浏览器默认将其当做文本居中的,即把它当做了一段文本(chrome默认font-size:16px;hight:21px)进行居中,没把它当做高度100px进行居中。所以需要对父div的line-height进行调整。以font-size:0(对应的字体高度为0)为例子,则需要line-height增加一个子div的高度(400px + 100px;)。

方法三(绝对定位

理念

利用定位属性(top、left、right、bottom)百分比的模式。若为100%,则代表偏移的长度为父div的高度(宽度)的100%。

步骤

  • 父div标记下定位(position:relative|absolute|fixed);子div绝对定位(position:absolute
  • 子div上下居中:top:50%;margin-top:-h/2; 或是 bottom:50%;margin-bottom:-h/2;
  • 子div左右居中: left:50%;margin-left:-w/2 或是 right:50%;margin-right:-w/2

例子

<style>
* {margin: 0; padding: 0; box-sizing: border-box;}
.father {position: relative;}
.son {position: absolute;bottom:50%;margin-bottom: -50px;left: 50%;margin-left: -50px;
}
</style>
<body>
<div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;">
<div class="son" style="width: 100px; height: 100px;background: palegreen;"></div>
</div>
</body>

方法四(绝对定位)

理念:

定位属性top和bottom(或是left和right)值分别设置为0,但子div有固定高度(宽度),并不能达到上下(左右)间距为0,此时给子div设置margin:auto会使它居中显示。

步骤:

  • 父div标记下定位(position:relative|absolute|fixed|sticky);子div绝对定位(position:absolute
  • 子div上下居中:top:0;bottom:0;margin-top:auto;margin-bottom:auto
  • 子div左右居中:left:0;right:0;margin-left:auto;margin-right:auto

例子

<style>
* {margin: 0; padding: 0; box-sizing: border-box;}
.father {position: relative;}
.son {position: absolute; top: 0; bottom:0; left: 0; right: 0; margin: auto}
</style>
<body>
<div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;">
<div class="son" style="width: 100px; height: 100px;background: palegreen;"></div>
</div>
</body>

方法五(flex)

理念

弹性盒子中只要给弹性子元素设置 margin: auto; 可以使得弹性子元素上下左右居中。

例子

<style>
* {margin: 0; padding: 0; box-sizing: border-box;}
.father {display: flex;}
.son {margin: auto}
</style>
<body>
<div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;">
<div class="son" style="width: 100px; height: 100px;background: palegreen;"></div>
</div>
</body>

flex兼容性,以及存在的已知问题

方法六(相对定位)

理念

利用定位属性(top、left、right、bottom)百分比的模式。若为100%,则代表偏移的长度为父div的高度(宽度)的100%。

步骤

  • 父div标记下定位(position:relative|absolute|fixed);子div相对定位(position:relative
  • 子div上下居中:top:50%;margin-top:-h/2; 或是 bottom:-50%;margin-top:-h/2;
  • 子div左右居中: left:50%;margin-left:-w/2 或是 right:-50%;margin-left:-w/2

例子

<style>
* {margin: 0; padding: 0; box-sizing: border-box;}
.father {position: relative;}
.son {position: relative; top:50%;margin-top:-50px;left:50%;margin-left:-50px}
</style>
<body>
<div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;">
<div class="son" style="width: 100px; height: 100px;background: palegreen;"></div>
</div>
</body>

注:

  • 与绝对定位不同的是,定位属性相对的点是自身元素的左上角。(所以bottom是负值,和方法三绝对定位写法有不一样的地方)
  • 如果有同级元素,可能会受到影响。
  • 如果子div是浮动元素,也有居中效果。

结尾

方法二和方法三兼容性要比其它好些。最简单的是弹性盒子。

补充

margin负值可以用2D转换代替。优点是不用计算子div的宽高。

margin-top: -h/2 => transform: translateY(-50%)

margin-bottom: -h/2 => transform: translateY(50%)

margin-left: -h/2 => transform: translateX(-50%)

margin-right: -h/2 => transform: translateX(50%)

参考资料

Can I use

css-vertical-center-solution

CSS实现垂直居中的5种方法--前端观察

相关指导

盼先生

用CSS让DIV上下左右居中的方法的更多相关文章

  1. CSS 控制元素 上下左右居中

    不说废话,直接 搞起..... 首先,我们将题目 <css控制元素上下左右居中> 分析一下哈,我是将其分成了4部分信息: 1.CSS控制: 只用 CSS 来达成目的 2.元素:  不只是d ...

  2. css控制div显示/隐藏方法及2种方法比较原码 - czf164的专栏 - 博客频道 - CSDN.NET

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  3. css:子元素div 上下左右居中方法总结

    最近在面试,不停地收到了知识冲击,尤其是对于一些基础的css.html.js问题居多,所以自我也在做反思,今天就css问题,如何让一个子元素div块元素上下左右居中 (以下总结方法,都已得到验证). ...

  4. css div上下左右居中

    相信大家都会遇到这样的问题,要求一个块上下左右居中,在这里我总结了几个好用的方法 1.已知要居中的块width height 假设  content 要在f里上下左右居中 <div class= ...

  5. HTML CSS 中DIV内容居中汇总

    转载博客(http://www.cnblogs.com/dearxinli/p/3865099.html) (备注:DIV居中情况,网上谈到也比较多,但是这篇文字,相对还是挺全面,现转载,如果冒犯,还 ...

  6. 关于一个div上下左右居中的css方法

    1:通过position:absolute定位,上下左右的值都设为0,margin:auto:需要知道div的宽高 { width: 64px; height: 64px; border: 1px s ...

  7. [html][css]让文字在div中居中的方法[转]

    转至:http://dreamweaver.abang.com/od/divcss/a/vertical-align.htm 一.行高(line-height)法 如果要垂直居中的只有一行或几个文字, ...

  8. div上下左右居中方法

    方法一:在浏览器中只有一个div的情况 { position:fixed; position:fixed; ; ; ; ; margin:auto; } 方法一 方法二:一个父元素div和一个已知宽度 ...

  9. 让图片在div 中居中的方法

    方法一: 思路:利用text-align属性将图片水平居中,然后设置padding-top的值使其垂直居中. 结构如下: <div> <img src="images/tt ...

随机推荐

  1. sqlalchemy通过ssh连接远程mysql服务器

    首先需要一个模块sshtunnel,如果没有直接pip install sshtunnel from sshtunnel import SSHTunnelForwarder from sqlalche ...

  2. AtCoder Beginner Contest 073

    D - joisino's travel Time Limit: 2 sec / Memory Limit: 256 MB Score : 400400 points Problem Statemen ...

  3. Linux下C语言socket通信实现发送读取的文件内容--简单实现代码

    本次代码涉及到的内容:socket通讯,文件读取 读取的文件以及文件位置: 要读取的文件和c文件在同一个目录下.客户端(client)读取的是123.xml,服务端(server)读取的是23.xml ...

  4. [js]关于call()和apply()的理解

    call 和 apply 都是为了改变某个函数运行时的 context 即上下文而存在的,换句话说,就是为了改变函数体内部 this 的指向. 因为 JavaScript 的函数存在「定义时上下文」和 ...

  5. jacascript DOM变动事件

    前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! DOM变动事件 变动事件(MutationEvent)能在DOM中的某一部分发生变化时给出提示,这类事件非 ...

  6. 微信小程序:wx.request之post请求后端无法获取数据的问题

    前言:小程序的开发中总是踩到各种坑,看文档也不知所云: 例如当我们在写微信小程序接口时,method请求方式有POST和GET两种,为了数据安全,我们会偏向于使用POST请求方式访问服务器端: 问题: ...

  7. 【Android】Android Studio3.1 Mac版本设置项目桌面icon

    近来项目处于测试阶段,工作少了许多,就装了个最新的Android Studio,想写一下安卓.新建好项目,想设置个桌面的icon.我先准备好自己的icon图片,然后复制粘贴到res/mipmap-hd ...

  8. 网易云安全两篇论文入选计算机视觉顶级会议ICCV

    本文由  网易云发布. 10月22日至29日,全球计算机视觉顶尖专家们共聚威尼斯,参加ICCV2017国际计算机视觉大会,就领域内最新成果展开集中研讨,大会论文集也代表了计算机视觉领域最新的发展方向和 ...

  9. Virtual Box下虚拟机复制后ip地址重复

    通过桥接模式上网的虚拟机在复制之后,出现三台机器的ip地址都是一样的,还都可以上网, 主要是因为在复制的时候,把网卡信息啥的都一起复制了, 为了设置为不同的ip,需要修改复制后的机器的mac地址. 首 ...

  10. Centos常用命令之:压缩与解压缩

    在Linux中,压缩文件的扩展名主要是:[*.tar,*.tar.gz,*.tgz,*.gz,*.Z,*.bz2],虽然,我们知道,在LInux中,文件的扩展名没有什么作用,但是由于在Linux中支持 ...