div 居中方法汇总
本文是从简书复制的, 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 居中方法汇总的更多相关文章
- div居中方法总结
在日常开发过程中,我们会经常使用到div居中来处理布局,今天我就把我在开发过程中,遇到的div居中处理方法总结一下,方便日后查看! 1. 水平居中:给div设置一个宽度,然后添加marg ...
- css 文本和div垂直居中方法汇总
https://blog.csdn.net/u014607184/article/details/51820508 https://blog.csdn.net/liuying1802028915/ar ...
- div居中方法
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 实现DIV居中的几种方法
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- div居中和垂直居中的最简单方法
div居中方法: 1)对父盒子添加 text-align="center": 2)子盒子添加 margin:0 auto; 例子: body{text-align:center} ...
- DIV居中的经典方法
1. 实现DIV水平居中 设置DIV的宽高,使用margin设置边距0 auto,CSS自动算出左右边距,使得DIV居中. 1 div{ 2 width: 100px; 3 height: 100px ...
- 纯css使div垂直居中,div垂直,div居中的方法
首先编写一个简单的html代码,设置一个父div类名为boxFather,再设置一个子div类名为box1.html代码如下: <div class="boxFather"& ...
- 让几个横向排列的浮动子div居中显示的方法
div设置成float之后,就无法使子div居中显示了,那么如何让几个横向排列的浮动的div居中显示呢,下面有个不错的方法,希望对大家有所帮助 div设置成float之后,在父div中设置text-a ...
- div居中的三种方法
方法1: #div1{ width:200px; height:200px; background:green; position:absolute; left:0; top:0; right:0; ...
随机推荐
- I01-通过查询资料库方式来监控Informatica调度情况
--登陆INFA资料库,运行下面的SQL --想要更加个性化查询的话注意看SQL倒数第二第三行的备注 SELECT RUN_DATE, START_TIME , END_TIME, FOLIDER , ...
- 01. css sprite是什么,有什么优缺点?
1.css sprite是什么,有什么优缺点? 通常被意译为“CSS图像拼合”或“CSS贴图定位” 1)CSS Sprites的优点 利用CSS Sprites能很好地减少网页的http请求,从而大大 ...
- DEM反应添加顺序注意问题
在含有DEM反应的dat中,均相反应的block要在DEM反应之前,例如: @(RXNS) (some reaction equations) @(END) @(DES_RXNS) (some rea ...
- Tr A(矩阵快速幂)
A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input 数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n(2 <= n < ...
- linux 系统管理(二) 磁盘分区
LINUX下分区命令Parted详解 通常划分分区工具我们用的比较多是fdisk命令,但是现在由于磁盘越来越廉价,而且磁盘空间越来越大. 而fdisk工具他对分区是有大小限制的,它只能划分小于2T的磁 ...
- .net core webapi 在原有基础上修改。
using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Lin ...
- java--浅谈线程
一.线程基础: 1.CPU核心数和线程数的关系 线程数:是同一时刻设备能并行执行的程序个数,线程数=cpu个数 * 核数,及程数=cpu个数(2) * 核数(2)=4: 使用了超线程技术后---> ...
- element-ui多层嵌套表格数据删除
很多表格都要一个移除的功能,所谓移除,就是前端把表格的数据删除,普通的表格删除很简单,调用数据的删除方法就行.但是当表格是多层的嵌套类型时,就不能再使用普通的删除方法了.下面介绍一种自己在项目中用的方 ...
- javascript通过class获取元素
1.getElementsByClassName 非IE6,7,8可以直接用自带的属性 getElementsByClassName,如果需要兼容 function getElementsByClas ...
- unity 解决与永久解决行尾不一致报警
虽然不影响使用,但一堆警告信息着实让人不爽,继续往下看. 用Notepad2的“查看->显示换行编码"查看发现通过Unity3D编辑器创建的脚本文件是以"LF"结尾 ...