本文是从简书复制的, markdown语法可能有些出入, 想看"正版"和更多内容请关注 简书: 小贤笔记

情况一: 父子容器宽高已知

方法一

  • html
<div class="father">
<div class="son"></div>
</div>
  • css
.father {
position: relative;
width: 1000px;
height: 600px;
background: lightblue;
}
.son {
position: absolute;
width: 100px;
height: 100px;
background: yellow;
top: 50%;
margin-top: -50px; /* 高度的一半 */
left: 50%;
margin-left: -50px; /* 宽度的一半 */
}

方法二

利用 margin: auto; 自动分配多余空间

  • html
<div class="father">
<div class="son"></div>
</div>
  • css
.father {
position: relative;
width: 1000px;
height: 600px;
background: lightblue;
}
.son {
position: absolute;
width: 100px;
height: 100px;
background: yellow;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}

top、left、right、bottom 的值相等即可,不一定要都是0

方法三

用 Flex 布局

  • html
<div class="father">
<div class="son"></div>
</div>
  • css
.father {
width: 1000px;
height: 600px;
background: lightblue;
display: flex;
align-items: center; /* 垂直居中 */
justify-content: center; /* 水平居中 */
}
.son {
width: 100px;
height: 100px;
background: yellow;
}

情况二: 父子容器宽高未知

方法一

  • html
<div class="father">
<div class="son"></div>
</div>
  • css
.father {
position: relative;
width: 1000px;
height: 600px;
background: lightblue;
}
.son {
position: absolute;
width: 100px;
height: 100px;
background: yellow;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

方法二

用 Flex 布局

  • html
<div class="father">
<div class="son"></div>
</div>
  • css
.father {
width: 1000px;
height: 600px;
background: lightblue;
display: flex;
align-items: center; /* 垂直居中 */
justify-content: center; /* 水平居中 */
}
.son {
width: 100px;
height: 100px;
background: yellow;
}

还有其他方法的小伙伴们欢迎补充 谢谢!

div 居中方法汇总的更多相关文章

  1. div居中方法总结

    在日常开发过程中,我们会经常使用到div居中来处理布局,今天我就把我在开发过程中,遇到的div居中处理方法总结一下,方便日后查看!        1. 水平居中:给div设置一个宽度,然后添加marg ...

  2. css 文本和div垂直居中方法汇总

    https://blog.csdn.net/u014607184/article/details/51820508 https://blog.csdn.net/liuying1802028915/ar ...

  3. div居中方法

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. 实现DIV居中的几种方法

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. div居中和垂直居中的最简单方法

    div居中方法: 1)对父盒子添加 text-align="center": 2)子盒子添加 margin:0 auto; 例子: body{text-align:center} ...

  6. DIV居中的经典方法

    1. 实现DIV水平居中 设置DIV的宽高,使用margin设置边距0 auto,CSS自动算出左右边距,使得DIV居中. 1 div{ 2 width: 100px; 3 height: 100px ...

  7. 纯css使div垂直居中,div垂直,div居中的方法

    首先编写一个简单的html代码,设置一个父div类名为boxFather,再设置一个子div类名为box1.html代码如下: <div class="boxFather"& ...

  8. 让几个横向排列的浮动子div居中显示的方法

    div设置成float之后,就无法使子div居中显示了,那么如何让几个横向排列的浮动的div居中显示呢,下面有个不错的方法,希望对大家有所帮助 div设置成float之后,在父div中设置text-a ...

  9. div居中的三种方法

    方法1: #div1{ width:200px; height:200px; background:green; position:absolute; left:0; top:0; right:0; ...

随机推荐

  1. leetcode-680-Valid Palindrome II

    题目描述: Given a non-empty string s, you may delete at most one character. Judge whether you can make i ...

  2. sudo: /usr/bin/sudo must be owned by uid 0 and have the setuid bit set

    使用root 登录,然后执行: chown root:root /usr/bin/sudo chmod 4755 /usr/bin/sudo reboot

  3. 前端统计图 echarts 实现简单柱状图

    前端统计图   echarts实现简单柱状图 1. 引入 ECharts <!DOCTYPE html> <html> <head> <meta charse ...

  4. Maximum call stack size exceeded

    写vue时报了如下错误 Maximum call stack size exceeded 栈溢出,因为在调用函数时使用了递归调用,而且没有写跳出条件,导致了该错误

  5. flask开发笔记

    目录 虚拟环境 Debug模式 配置文件 url传入参数 url反转 重定义向 模板 创建 jinjia2语法 模板继承 flash 加载静态文件 MySQL数据库命令 配置 更新.提交.删除 模型操 ...

  6. Chrome打开网页都提示Flash Player因过期而遭到阻止

    1. 运行Chrome浏览器,地址栏输入:chrome://plugins/,找到pepflashplayer.dll安装位置, 如:D:\Program Files\GoogleChromePort ...

  7. KafKa 启动

    Zookeeper 运行kafka需要使用Zookeeper,所以要先启动Zookeeper,如果没有Zookeeper,可以使用kafka自带打包和配置好的Zookeeper 1.进入kafka的b ...

  8. Train Problem II(卡特兰数 组合数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1023 Train Problem II Time Limit: 2000/1000 MS (Java/ ...

  9. System.Security.Cryptography.CryptographicException 微信支付中公众号发红包时候碰到的错误。

    转 留记录.我是第二个错误原因 我总结了一下出现证书无法加载的原因有以下三个 1.证书密码不正确,微信证书密码就是商户号 解决办法:请检查证书密码是不是和商户号一致 2.IIS设置错误,未加载用户配置 ...

  10. (转)error while loading shared libraries:libmysqlclient.so.18 错误

    error while loading shared libraries:libmysqlclient.so.18错误 新手安装php的时候如果出现这种问题,解决办法很简单,就是查看你的mysql安装 ...