如何让一个DIV居中,这应该是一个简单的问题吧?
难道有很多不同的应用场景,以及不同的写法吗?
登录
1、采用 margin 水平居中。
在元素的 宽度已知 的情况下,并且 小于父容器的宽度 时,能很直观的看到它居中了。
// 普通左右居中 margin: 0 auto;
.layour-center { margin-left: auto; margin-right: auto; }
常见写法
2、采用 text-align:center 加 display:inline-block 实现水平居中。
<style type="text/css">
.parent {
background-color: coral;
height: 200px;
padding-top:15px;
text-align: center;;
}
.child {
background-color: white;
width:100px;
height: 50px;
display: inline-block;
/* text-align:left;*/ /* 重置为默认 */
}
</style>
3、采用 绝对定位 方式,实现 水平和垂直居中 。一般常见于登录框、弹窗等应用场景。
使用 absolute 属性时,元素的定位是相对于其最近的被定位(position 属性设置为非 static 的元素)的父级元素或祖先元素进行计算的。如果没有找到被定位的父级元素或祖先元素,那么相对于文档的 body 元素进行计算。
使用 fixed 属性时,元素的定位是相对于浏览器窗口的。
所以应该根据实际情况选择具体的属性。
<style type="text/css">
.container-login {
background-color: coral;
position: absolute;
/* position:fixed */
/**
水平居中
左边距离50%,同时
减去自身的 宽度 的一半。
*/
width:130px;
left:50%;
margin-left:-65px;
/**
垂直居中
上边距离50%,同时
减去自身的 高度 的一半。
*/
height:140px;
top: 50%;
margin-top:-70px;
}
</style>
相对定位/绝对定位
4、采用 flex 布局,实现 水平和垂直居中 。父容器设置轴线以实现子容器的居中。
<style type="text/css">
.parent {
background-color: coral;
display: flex;
/* 沿着主轴水平居中 */
justify-content: center;
/* 沿着交叉轴垂直居中 */
align-items: center;
/* 父容器的高度不能为auto */
height: 300px;
}
.child {
background-color: white;
/* 确定子元素的大小 */
width: 200px;
height: 100px;
}
</style>
flex
5、使用 transform ,实现 水平和垂直居中 。 -50% 表示向左/上移动元素的一半,从而实现完美居中。
<style type="text/css">
.parent {
background-color: coral;
position: relative;
height: 230px;
}
.child {
background-color: white;
position: absolute;
width:80px;
height: 80px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
人人为我,我为人人,谢谢您的浏览,我们一起加油吧。
直居中-父元素高度确定的单行文本
父元素高度确定的单行文本的竖直居中的方法是通过设置父元素的height和line-height高度一致来实现的。如下代码:
<div class="container">
hi,imooc!
</div>
css代码:
<style>
.container{
height:100px;
line-height:100px; /* 仅能用于单行文本 */
background:#999;
}
</style>
垂直居中-图片以及行内块元素
<div class="container">
<img src="imgegs/icon.png" />
</div>
css代码:
<style>
.container{
height:100px;
background:#999;
}
.container img{
vertical-align:middle;
}
</style>
垂直居中-父元素高度确定的多行文本(方法一)
父元素高度确定的多行文本、图片、块状元素的竖直居中的方法有两种:
方法一:使用插入table(包括tbody、tr、td)标签, 同时设置vertical-align:middle。
说到竖直居中,css中有一个用于竖直居中的属性vertical-align,但这个样式只有在父元素为td 或th时,才会生效。所以又要插入table标签了。
下面看一下例子:
html代码:
<body>
<table><tbody><tr><td class="wrap">
<div>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
</div>
</td></tr></tbody></table>
</body>
css代码:
table td{height:500px;background:#ccc}
因为td标签默认情况下就默认设置了vertical-align为middle, 所以我们不需要显式地设置了。
垂直居中-父元素高度确定的多行文本(方法二)
在chrome、firefox及IE8以上的浏览器下可以设置块级元素的display为table-cell, 激活vertical-align属性, 但注意IE6、7并不支持这个样式。
html代码:
<div class="container">
<div>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
</div>
</div>
css代码:
<style>
.container{
height:300px;
background:#ccc;
display:table-cell; /*IE8以上及Chrome、Firefox*/
vertical-align:middle; /*IE8以上及Chrome、Firefox*/
}
</style>
这种方法的好处是不用添加多余的无意义的标签,但缺点也很明显,它的兼容性不是很好,不兼容 IE6、7。
垂直居中--方法三
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
div{
width: 400px;
height: 300px;
background-color: orange;
}
/*
* 思路一:left:50%;top:50%;margin-left: -200px;margin-top: -150px;
* 思路二:left:0;top:0;right:0;bottom:0;margin:auto;
* */
div{
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%); /* 平移 */
}
</style>
</head>
<body>
<div></div>
</body>
</html>
实例1:将内层div的文本垂直居中
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>父元素高度确定的多行文本</title>
<style>
.container{
height:300px;
background:#ccc;
display:table-cell; /*IE8以上及Chrome、Firefox*/
vertical-align:middle; /*IE8以上及Chrome、Firefox*/
}
</style>
</head>
<body>
<div class="container">
<div>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
<p>看我是否可以居中。</p>
</div>
</div>
<!--下面是代码任务区-->
</body>
</html>
实例2:将内层垂直居中、外层水平居中
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style type="text/css">
#content{
width:300px;
height:300px;
border:#000 solid 1px;
margin:auto;
display:table;
}
#wenzi{
border:#F00 solid 1px;
text-align:center;
display:table-cell;
vertical-align: middle;
}
</style>
</head>
<body>
<div id="content">
<div id="wenzi">
锄禾日当午,<br>
汗滴禾下土。<br>
谁知盘中餐,<br>
粒粒皆辛苦。<br>
</div>
</div>
</body>
</html>
实例3: 使用绝对定位垂直居中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
div{
width: 220px;
height: 280px;
background: url("img/王思聪.jpg");
position: absolute;
left: 50%;
top: 50%;
margin-left: -110px;
margin-top: -140px;
}
</style>
</head>
<body>
<!--
行内元素(文本)->水平垂直居中
text-align: center;
line-height: height;
-->
<!--
块元素->水平垂直居中
margin: 0 auto;
-->
<div></div>
</body>
</html>
实例4: 使用绝对定位垂直居中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
div{
width: 600px;
height: 200px;
padding: 10px 20px;
border: 1px solid #000;
border-radius: 5px;
/* 下面这种写法也可以让一个盒子居中 */
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
</style>
</head>
<body>
<div>您确定删除:重庆万州公交坠江事件结果公布后,司乘纠纷和公交驾驶安全问题成为人们热议的焦点,如何预防和避免恶性结果的发生,才是问题的关键。“鼓励市民举报,并对勇于制止干扰公交车正常行驶违法行为的公民予以奖励。”昨日下午,西安市公安局公共交通分局召开媒体通气会,通报西安相关安全举措。这条消息吗</div>
</body>
</html>
绝对定位(固定定位)之后, 所有标准流的规则, 都不适用了。所以margin:0 auto; 失效。
解决办法:left:50%; margin-left:负的宽度的一半。(三句话)
div{
width: 600px;
height: 60px;
position: absolute; /* → 第一句 */
left: 50%; // /* → 第二句 */
top: 0;
margin-left: -300px; /*→ 第三句。宽度的一半*/
}
实例4:使用绝对定位和margin:auto垂直居中
者|颜海镜
编辑|覃云
出处丨前端之巅
本文已获作者授权,转载来源:
https://segmentfault.com/a/1190000016389031
划重点,这是一道面试必考题,很多面试官都喜欢问这个问题,我就被问过好几次了。
要实现上图的效果看似很简单,实则暗藏玄机,本文总结了一下 CSS 实现水平垂直居中的方式大概有下面这些,本文将逐一介绍一下,我将本文整理成了一个 github 仓库在:https://github.com/yanhaijing/vertical-center
欢迎大家 star。
仅居中元素定宽高适用:
居中元素不定宽高:
为了实现上面的效果先来做些准备工作,假设 HTML 代码如下,总共两个元素,父元素和子元素:
<div class="wp"> <div class="box size">123123</div> </div>
wp 是父元素的类名,box 是子元素的类名,因为有定宽和不定宽的区别,size 用来表示指定宽度,下面是所有效果都要用到的公共代码,主要是设置颜色和宽高。
注意:后面不在重复这段公共代码,只会给出相应提示。
/* 公共代码 */ .wp { border: 1px solid red; width: 300px; height: 300px; } .box { background: green; } .box.size{ width: 100px; height: 100px; } /* 公共代码 */
绝对定位的百分比是相对于父元素的宽高,通过这个特性可以让子元素的居中显示,但绝对定位是基于子元素的左上角,期望的效果是子元素的中心居中显示。
为了修正这个问题,可以借助外边距的负值,负的外边距可以让元素向相反方向定位,通过指定子元素的外边距为子元素宽度一半的负值,就可以让子元素居中了,css 代码如下。
/* 此处引用上面的公共代码 */ /* 此处引用上面的公共代码 */ /* 定位代码 */ .wp { position: relative; } .box { position: absolute;; top: 50%; left: 50%; margin-left: -50px; margin-top: -50px; }
这是我比较常用的方式,这种方式比较好理解,兼容性也很好,缺点是需要知道子元素的宽高。
点击查看完整 DEMO:
http://yanhaijing.com/vertical-center/absolute1.html
这种方式也要求居中元素的宽高必须固定,HTML 代码如下:
<div class="wp"> <div class="box size">123123</div> </div>
这种方式通过设置各个方向的距离都是 0,此时再讲 margin 设为 auto,就可以在各个方向上居中了。
/* 此处引用上面的公共代码 */ /* 此处引用上面的公共代码 */ /* 定位代码 */ .wp { position: relative; } .box { position: absolute;; top: 0; left: 0; right: 0; bottom: 0; margin: auto; }
这种方法兼容性也很好,缺点是需要知道子元素的宽高。
点击查看完整 DEMO:
http://yanhaijing.com/vertical-center/absolute2.html
这种方式也要求居中元素的宽高必须固定,所以我们为 box 增加 size 类,HTML 代码如下:
<div class="wp"> <div class="box size">123123</div> </div>
感谢 css3 带来了计算属性,既然 top 的百分比是基于元素的左上角,那么在减去宽度的一半就好了,代码如下
/* 此处引用上面的公共代码 */ /* 此处引用上面的公共代码 */ /* 定位代码 */ .wp { position: relative; } .box { position: absolute;; top: calc(50% - 50px); left: calc(50% - 50px); }
这种方法兼容性依赖 calc 的兼容性,缺点是需要知道子元素的宽高。
点击查看完整 DEMO:
http://yanhaijing.com/vertical-center/absolute3.html
还是绝对定位,但这个方法不需要子元素固定宽高,所以不再需要 size 类了,HTML 代码如下:
<div class="wp"> <div class="box">123123</div> </div>
修复绝对定位的问题,还可以使用 css3 新增的 transform,transform 的 translate 属性也可以设置百分比,其是相对于自身的宽和高,所以可以讲 translate 设置为 -50%,就可以做到居中了,代码如下:
/* 此处引用上面的公共代码 */ /* 此处引用上面的公共代码 */ /* 定位代码 */ .wp { position: relative; } .box { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
这种方法兼容性依赖 translate2d 的兼容性。
点击查看完整 DEMO:
http://yanhaijing.com/vertical-center/absolute4.html
利用行内元素居中属性也可以做到水平垂直居中,HTML 代码如下:
<div class="wp"> <div class="box">123123</div> </div>
把 box 设置为行内元素,通过 text-align 就可以做到水平居中,但很多同学可能不知道通过通过 vertical-align 也可以在垂直方向做到居中,代码如下:
/* 此处引用上面的公共代码 */ /* 此处引用上面的公共代码 */ /* 定位代码 */ .wp { line-height: 300px; text-align: center; font-size: 0px; } .box { font-size: 16px; display: inline-block; vertical-align: middle; line-height: initial; text-align: left; /* 修正文字 */ }
这种方法需要在子元素中将文字显示重置为想要的效果。
点击查看完整 DEMO:
http://yanhaijing.com/vertical-center/lineheight.html
很多同学一定和我一样不知道 writing-mode 属性,感谢 @张鑫旭老师的反馈,简单来说 writing-mode 可以改变文字的显示方向,比如可以通过 writing-mode 让文字的显示变为垂直方向。
<div class="div1">水平方向</div> <div class="div2">垂直方向</div> .div2 { writing-mode: vertical-lr; }
显示效果如下:
水平方向 垂 直 方 向
更神奇的是所有水平方向上的 css 属性,都会变为垂直方向上的属性,比如 text-align,通过 writing-mode 和 text-align 就可以做到水平和垂直方向的居中了,只不过要稍微麻烦一点:
<div class="wp"> <div class="wp-inner"> <div class="box">123123</div> </div> </div> /* 此处引用上面的公共代码 */ /* 此处引用上面的公共代码 */ /* 定位代码 */ .wp { writing-mode: vertical-lr; text-align: center; } .wp-inner { writing-mode: horizontal-tb; display: inline-block; text-align: center; width: 100%; } .box { display: inline-block; margin: auto; text-align: left; }
这种方法实现起来和理解起来都稍微有些复杂。
点击查看完整 DEMO:
http://yanhaijing.com/vertical-center/writing-mode.html
曾经 table 被用来做页面布局,现在没人这么做了,但 table 也能够实现水平垂直居中,但是会增加很多冗余代码:
<table> <tbody> <tr> <td class="wp"> <div class="box">123123</div> </td> </tr> </tbody> </table>
tabel 单元格中的内容天然就是垂直居中的,只要添加一个水平居中属性就好了。
.wp { text-align: center; } .box { display: inline-block; }
这种方法就是代码太冗余,而且也不是 table 的正确用法。
点击查看完整 DEMO:
http://yanhaijing.com/vertical-center/table.html
css 新增的 table 属性,可以让我们把普通元素,变为 table 元素的现实效果,通过这个特性也可以实现水平垂直居中。
<div class="wp"> <div class="box">123123</div> </div>
下面通过 css 属性,可以让 div 显示的和 table 一样:
.wp { display: table-cell; text-align: center; vertical-align: middle; } .box { display: inline-block; }
这种方法和 table 一样的原理,但却没有那么多冗余代码,兼容性也还不错。
点击查看完整 DEMO:
http://yanhaijing.com/vertical-center/css-table.html
flex 作为现代的布局方案,颠覆了过去的经验,只需几行代码就可以优雅的做到水平垂直居中。
<div class="wp"> <div class="box">123123</div> </div> .wp { display: flex; justify-content: center; align-items: center; }
目前在移动端已经完全可以使用 flex 了,PC 端需要看自己业务的兼容性情况。
点击查看完整 DEMO:
http://yanhaijing.com/vertical-center/flex.html
感谢 @一丝姐 反馈的这个方案,css 新出的网格布局,由于兼容性不太好,一直没太关注,通过 grid 也可以实现水平垂直居中。
<div class="wp"> <div class="box">123123</div> </div> .wp { display: grid; } .box { align-self: center; justify-self: center; }
代码量也很少,但兼容性不如 flex,不推荐使用。
点击查看完整 DEMO:
http://yanhaijing.com/vertical-center/grid.html
下面对比下各个方式的优缺点,肯定又双叒叕该有同学说回字的写法了,简单总结下:
小贴士:关于 flex 的兼容性决方案,请看这里:
https://yanhaijing.com/css/2016/08/21/flex-practice-on-mobile/
最近发现很多同学都对 css 不够重视,这其实是不正确的,比如下面的这么简单的问题都有那么多同学不会,我也是很无语:
<div class="red blue">123</div> <div class="blue red">123</div> .red { color: red } .blue { color: blue }
问两个 div 的颜色分别是什么,竟然只有 40% 的同学能够答对,这 40% 中还有很多同学不知道为什么,希望这些同学好好补习下 CSS 基础。
*请认真填写需求信息,我们会在24小时内与您取得联系。