如何将一个div水平垂直居中?6种方法做推荐
方案一:
div绝对定位水平垂直居中【margin:auto实现绝对定位元素的居中】,
兼容性:,IE7及之前版本不支持

div{
width: 200px;
height: 200px;
background: green;
position:absolute;
left:0;
top: 0;
bottom: 0;
right: 0;
margin: auto;
}

方案二:
div绝对定位水平垂直居中【margin 负间距】 这或许是当前最流行的使用方法。

div{
width:200px;
height: 200px;
background:green;
position: absolute;
left:50%;
top:50%;
margin-left:-100px;
margin-top:-100px;
}

方案三:
div绝对定位水平垂直居中【Transforms 变形】
兼容性:IE8不支持;

div{
width: 200px;
height: 200px;
background: green;
position:absolute;
left:50%; /* 定位父级的50% */
top:50%;
transform: translate(-50%,-50%); /*自己的50% */
}

方案四:
css不定宽高水平垂直居中

.box{
height:600px;
display:flex;
justify-content:center;
align-items:center;
/* aa只要三句话就可以实现不定宽高水平垂直居中。 */
}
.box>div{
background: green;
width: 200px;
height: 200px;
}

方案五:
将父盒子设置为table-cell元素,可以使用text-align:center和vertical-align:middle实现水平、垂直居中。比较完美的解决方案是利用三层结构模拟父子结构
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<p class="outerBox tableCell"> </p><p class="ok"> </p><p class="innerBox">tableCell</p> <p></p><p></p>/*table-cell实现居中将父盒子设置为table-cell元素,设置text-align:center,vertical-align: middle;子盒子设置为inline-block元素*/.tableCell{ display: table;}.tableCell .ok{ display: table-cell; text-align: center; vertical-align: middle;}.tableCell .innerBox{ display: inline-block;} |
方案六:
对子盒子实现绝对定位,利用calc计算位置

<p class="outerBox calc">
</p><p class="innerBox">calc</p>
<p></p> /*绝对定位,clac计算位置*/
.calc{
position: relative;
}
.calc .innerBox{
position: absolute;
left:calc(50% - 宽度/2);
top:calc(50% - 高度/2);
}

如何将一个div水平垂直居中?6种方法做推荐的更多相关文章
- 未知宽高div水平垂直居中3种方法
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> </head&g ...
- 如何将一个div水平垂直居中?4种方法做推荐
方案一: div绝对定位水平垂直居中[margin:auto实现绝对定位元素的居中], 兼容性:,IE7及之前版本不支持 div{ width: 200px; height: 200px; backg ...
- css中元素水平垂直居中4种方法介绍
table-cell轻松设置文本图片水平垂直居中 让一个元素垂直居中的思路:把这个元素的容器设置为table-cell,也就是具有表格单元格的特性,再使用vertical-align(这个属性对blo ...
- 如何将一个div水平垂直居中
方案一: div绝对定位水平垂直居中[margin:auto实现绝对定位元素的居中], 兼容性:,IE7及之前版本不支持 div{ width: 200px; height: 200px; backg ...
- div水平垂直居中的六种方法
在平时,我们经常会碰到让一个div框针对某个模块上下左右都居中(水平垂直居中),其实针对这种情况,我们有多种方法实现. 方法一: 绝对定位方法:不确定当前div的宽度和高度,采用 transform: ...
- 【笔记】让DIV水平垂直居中的两种方法
今天写的了百度前端学院春季班的任务:定位和居中问题 由于距离上次学习CSS有点久远了,加上以前木有记笔记的习惯,方法忘得只剩下一种,今天通过网上查阅资料总结了以下两种简单的方法让DIV水平垂直居中. ...
- Div水平垂直居中的三种方法
百度了很多种方法,很多是不起作用的.下面这些方法是我亲自试过的.希望对大家有帮助! 下面是一波代码水军. <!DOCTYPE html> <html lang="en&qu ...
- CSS:使用CSS3将一个div水平和垂直居中显示
使用css3将一个div水平和垂直居中显示 方案一: div绝对定位水平垂直居中[margin:auto实现绝对定位元素的居中], 代码两个关键点:1.上下左右均0位置定位: 2.margin: au ...
- 如何让div水平垂直居中
引子 我们经常遇到需要把div中的内容进行水平和垂直居中.所以,这里介绍一种方法,可以使div水平居中和垂直居中. 代码: <!DOCTYPE html> <html lang=&q ...
随机推荐
- Spring MVC系列-(3) Bean的装配
3. 高级装配Bean 3.1 Bean的作用域 默认情况下,Spring中的bean都是以单例的形式存在的,无论注入多少次,每次注入的都是同一个实例. 考虑到某些bean可能是可变的,Spring定 ...
- hdu3695 AC自动机优化
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/3695/ 不加last指针的AC自动机会T,原因是他费了很多功夫在跳转上,而last指针是直接直到跳转的终止位置, ...
- [线段树]Codeforces 339D Xenia and Bit Operations
Xenia and Bit Operations time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- leetcode 签到 面试题40. 最小的k个数
题目 输入整数数组 arr ,找出其中最小的 k 个数.例如,输入4.5.1.6.2.7.3.8这8个数字,则最小的4个数字是1.2.3.4. 示例 1: 输入:arr = [3,2,1], k = ...
- mac下 yarn Stack trace: ExitCodeException exitCode=127
问题出在hadoop 为mac系统配置的读取java_home处. 更改 /Users/shaofengfeng/apache/hadoop/libexec/hadoop-config.sh 如下 # ...
- windows10环境下QtCreator中出现skipping incompatible xxx when searching for xxx 问题解决办法
windows10环境下QtCreator中出现skipping incompatible xxx when searching for xxx 我再QtCreator中想导入一个外部库时,他提示不匹 ...
- Building Applications with Force.com and VisualForce (DEV401) (二五):Visualforce Controller
Dev401-026:Visualforce Pages: Visualforce Controller Module Objectives1.Identify the functionality ...
- Django-rest-framework源码分析(二)
四.Serializer对象的data属性 在<Django-rest-framework源码分析(一)>中我分析了Serializer对象实例化的过程,而Serializer类的其他方法 ...
- 热点 | github近期热点项目汇总
本文是近期Github热点项目的汇总,如果你想了解更多优秀的github项目,请关注我们公众号的github系列文章. 推荐 | 7个你最应该知道的机器学习相关github项目 热点 | 六月Gith ...
- python文件调用方法
文件输入输出 open函数可以对文本文件进行读写的操作 基本形式: open(filename,mode) filename是文件名,可以写为绝对路径也可以是相对路径 mode是打开模式. open函 ...