【发布时间】:2019-01-10 09:53:36
【问题描述】:
我使用 object fit contains 将图像推入一个更小的 div。当浏览器窗口宽度减小时,图像保持纵横比,但显示的图像部分开始被截断,而不是统一调整大小/缩小。
有没有办法在窗口宽度减小时使用对象适合而不会使部分图像被截断?
*{
box-sizing: border-box;
margin: 0px;
padding: 0px;
}
.container{
height: auto;
width: 100%;
display: flex;
flex-wrap: wrap;
flex-direction: row;
}
.holder{
height: 400px;
width: 33.33%;
padding-left: 5px;
margin-top: 5px;
}
.last{
padding-right: 5px;
}
img{
object-fit: cover;
object-position: 35% 25%;
height:100%;
width:100%;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<link rel="stylesheet" type="text/css" href="style/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="script/javascript.js"></script>
<title> </title>
</head>
<body>
<div class="container">
<div class="holder">
<img src="https://picsum.photos/1920/660">
</div>
<div class="holder">
<img src="https://picsum.photos/1920/660">
</div>
<div class="holder last">
<img src="https://picsum.photos/1920/660">
</div>
<div class="holder">
<img src="https://picsum.photos/1920/660">
</div>
<div class="holder">
<img src="https://picsum.photos/1920/660">
</div>
<div class="holder last">
<img src="https://picsum.photos/1920/660">
</div>
</div>
<img src="https://picsum.photos/1920/660">
</body>
</html>
【问题讨论】:
-
你可以试试object-fit:fill;在给定宽度的 div 中占据整个图像