图片在父元素里面水平垂直居中

<!DOCTYPE html>

<html lang="en">

<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>

</head>
<style>
.img_wrap{
width: 500px;
height: 300px;
border: 1px dashed #ccc;
display: table-cell;
vertical-align: middle;
text-align: center;
}

img{

height:auto;

}
</style>
<body>

<div class="img_wrap">
  <img src="https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=1438475101,354016904&fm=58">
</div>
</body>

</html>

 

第二种》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

 

<!DOCTYPE html>

<html lang="en">

<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>

</head>

<body>

<div class="pic"><img src="https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=1438475101,354016904&fm=58" alt=""></div>

<div class="pic"><img src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2201558756,12364836&fm=111&gp=0.jpg" alt=""></div>

</body>

</html>

<style>

.pic { width: 120px;height: 120px;margin: 40px auto;border: 1px solid #ccc;text-align: center;padding: 5px; }

.pic:before { content: "";display: inline-block;height: 100%;vertical-align: middle;width: 0; }

.pic img { max-width: 120px; max-height: 120px;vertical-align: middle; }

</style>

 

第三种图片不变形用背景实现

 

<!DOCTYPE html>

<html lang="en">

<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>

</head>

<body>

<div class="pic"></div>

</body>

</html>

<style>

.pic {
height: 300px;
width: 300px;
background-repeat: no-repeat!important;
background-position: center center!important;
background-size: cover!important;
background: url(https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=1438475101,354016904&fm=58);
}

</style>

posted @ 2017-07-28 21:22  奔跑的小七  阅读(488)  评论(0编辑  收藏  举报