CSS怎么让图片居中

html-css012

CSS怎么让图片居中,第1张

1、首先先在页面里加载一张图片,代码和效果如下图所示:

2、然后设置给图片起一个class名,方便一会儿的操作。

3、然后给图片设置css样式,因为方便的原因就直接在html页面写css样式了。

4、经常使用“margin: 0 auto”来实现水平居中,而一直认为“margin: auto”是不能实现垂直居中,但是实际上,仅需要添加一些限制便能实现效果,就是通过定位:

position: absolute

top: 0

left: 0

bottom: 0

right: 0

设置定位让上下左右都为0,然后通过margin:0 auto,来让元素实现上下左右都居中。

5、设置完CSS样式之后,通过浏览查看代码的效果就可以,可以看到图片已经实现了。

6、最后给大家附上全部的代码:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8" />

<title>使用CSS让图片水平垂直居中</title>

</head>

<body>

<img class="pic" src="img/timg.jpg" alt="" />

</body>

<style type="text/css">

.pic{

margin: auto

position: absolute

top: 0

left: 0

bottom: 0

right: 0

}

</style>

</html>

写个简单的例子给你吧

htlm如下:

<h4>图片水平居中</h4>

<div class="demo1">

<img src="你的图片" alt="">

</div>

<h4>图片垂直居中</h4>

<div class="demo2">

<div class="imgbox">

<img src="你的图片" alt="">

</div>

</div>

<h4>图片水平垂直居中</h4>

<div class="demo3">

<div class="imgbox">

<img src="你的图片" alt="">

</div>

</div>

css如下:

<style type="text/css">

.demo1{width: 200pxheight: 200pxborder: 1px solid #cccdisplay: inline-blocktext-align: center}

.demo1 img{width: 100pxheight: auto}

.demo2{width: 200pxheight: 200pxborder: 1px solid #cccdisplay: table}

.demo2 .imgbox{display: table-cellvertical-align: middle}

.demo2 .imgbox img{width: 100pxheight: auto}

.demo3{width: 200pxheight: 200pxborder: 1px solid #cccdisplay: table}

.demo3 .imgbox{display: table-cellvertical-align: middletext-align: center}

.demo3 .imgbox img{width: 100pxheight: auto}

</style>

水平或者垂直居中单一的要求很好做到,我说几种自己总结的常用的水平且垂直居中的几种方法:

第一种 借助inline-block的特点

#d1{

display:inline-block

width:500px

height:500px

border:1px solid red

text-align:center

}

#d1:after{

content:""

display:inline-block

height:100%

vertical-align:middle

background:#000

}

#d2{

display:inline-block

width:200px

height:200px

border:1px solid red

vertical-align:middle

}

<div id="d1">

<div id="d2"></div>

</div>

第二种 利用css的transform 好用但是兼容性不好,IE10+以及其他现代浏览器才支持(手机开发可忽略)

.box{width:300pxheight:300pxborder:1px solid redposition:relative}

.content{

position:absolute

width:100px

height:100px

border:1px solid red

margin:0 auto

top:50%left:50%

/* transform:translateY(-50%)仅垂直居中*/

/* transform:translateX(-50%)仅水平居中*/

transform: translate(-50%, -50%)

/*

若父容器下只有一个元素,且父元素设置了高度,则只需要使用相对定位即可

父元素{

height:xxx

}

.子元素 {

position: relative

top: 50%

transform: translateY(-50%)

}

*/

}

<div class="box">

<div class="content"></div>

</div>

第三种:绝对定位之后的偏移

.box{

border:1px solid red

width:300pxheight:300pxposition:relative

}

.content{

border:1px solid red

width: 200pxheight: 200px

position: absoluteleft: 50%top: 50%

margin-top: -100px /* 高度的一半 */

margin-left: -100px /* 宽度的一半 */

}

<div class="box">

<div class="content"></div>

</div>

第四种:定位之后的margin: auto

.box{

border:1px solid red

width:300pxheight:300pxposition:relative

}

.content{

width: 200px

height: 200px

position: absolute

left: 0

top: 0

right: 0

bottom: 0

margin: auto

border:1px solid red

}

<div class="box">

<div class="content"></div>

</div>

第五种 flex布局

<div style="display:flexdisplay: -webkit-flexjustify-content:centeralign-items:centerwidth: 300pxheight: 300pxborder:1px solid red">

<div style="width: 200pxheight: 200pxborder:1px solid red"></div>

</div>

第六种利用display:table-cell的vertical-align属性 子元素加上“display:inline-block”可水平居中

<div style="display:table-cellvertical-align:middletext-align:centerwidth:300pxheight:300pxborder:1px solid red">

<div style="border:1px solid redwidth:200pxheight:200pxdisplay:inline-block"></div>

</div>

第七种 使用css3中的display:-webkit-box的用法这种方法还没有得到浏览器的普遍支持,如有兴趣,自行学习