整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

前端开发之CSS图片水平、垂直居中显示

过前端开发的朋友都知道,要使文字在一个div容器垂直居中显示是很简单的事,但是如果是要让一张图片在div容器中垂直显示那就有点伤脑筋了,因为图片是个置换元素,有些特殊的特性,对于前端开发的大牛来说还好,解决方法有很多,比如什么利用js啊之类的,但是这些对于初学者来说就有些难度了,今天小编就把自己平时在项目中用的方法分享给大家,给大家一个参考。

其实小编用的方法也不是什么很高端的技术,算是最low的方法吧,但是很实用,能兼容各种浏览器,为了演示,小编直接从某宝商家店铺里拿了一张商品图来做演示,因为商品图都是高清无码的,且还很大,所以我们在CSS中把图片的高度设置为200px,div容器的高度为300px,完整的代码如下:

最终的运行效果如下图:

可以看到,这个图片在div中水平、垂直都是在正中间,是不是很完美。这个原理也很简单,就是在div容器中加了个 <i> 标签并把它的 display 属性设置为 inline-block(行内块元素),让它去撑开div容器的高度,再把图片的 vertical-align 属性设置为 middle 就可以垂直居中了,当然这只是个演示,大家在实际的项目中可以利用 max-width、min-width、max-height、min-height 等属性让图片根据需要自适应,以达到想要的效果。

当然解决方法还有很多种,具体用什么方法需根据自身页面布局去权衡,总之没有最好的,只有最适合的,如果你有更好的方法也可以告诉小编哦,一起交流方法,共同进步。

直居中-父元素高度确定的单行文本

父元素高度确定的单行文本的竖直居中的方法是通过设置父元素的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垂直居中

容导读

当父div的行高等于自身高度时,内部的行内元素会上下居中显示。行内块没有固定高度时也会上下居中显示。所以需要对父div的 line-height 进行调整。利用定位属性(top、left、right、bottom)百分比的模式。若为100%,则代表偏移的长度为父div的高度(宽度)的100%。定位属性top和bottom(或是left和right)值分别设置为0,但子div有固定高度(宽度),并不能达到上下(左右)间距为0,此时给子div设置 margin:auto 会使它居中显示。

转载自喜欢JS的无名小站

例如 一个父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)

理念

弹性盒子,自带的一个居中功能

例子

<style>
 * {margin: 0; padding: 0; box-sizing: border-box;}
 .father {display: flex; align-items: center}
 .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兼容性,以及存在的已知问题

结尾

方法二和方法三兼容性要比其它好些

参考资料

Can I use
css-vertical-center-solution
CSS实现垂直居中的5种方法--前端观察