我有3张图片: 1- 320 x 4220 2- 600 x 450 3- 695 x 355
如何使用css和html获得所有这三个图像的尺寸为260x260?我不想剪切图像,我想保持它们原来的比例。谢谢你的帮忙
发布于 2019-08-31 02:04:02
object-fit就是为此而生的,它将保持比例,还可以按照你想要的方式裁剪你的图像:
object-fitCSS属性设置替换元素的内容(如<img>或<video>)应如何调整大小以适合其容器。
语法值:fill | contain | cover | none | scale-down
您可以使用object-position属性更改被替换元素的内容对象在元素框中的对齐方式。例如:
img[class] {
width:260px;
height:260px;
object-fit:cover;
}
img:nth-child(1) {
object-position: center top;
}
img:nth-child(3) {
object-position: right top;
}<img class src=https://placeimg.com/320/420/animals>
<img class src=https://placeimg.com/600/450/animals>
<img class src=https://placeimg.com/695/355/animals>
<p>Original size</p>
<img src=https://placeimg.com/320/420/animals>
<img src=https://placeimg.com/600/450/animals>
<img src=https://placeimg.com/695/355/animals>
如果你的图像只是设计的一部分,那么background-image,background-size,background-position可以做类似的视觉效果。
发布于 2019-08-31 01:56:35
这就是你要找的吗?它使用background-size:contain自动确保整个图像适合,然后使用background-position:center在div中居中。
#image {
border: 2px solid black;
height:260px;
width:260px;
background: url("https://wallpapertag.com/wallpaper/full/0/6/3/49612-mountains-wallpaper-1920x1080-for-android-40.jpg");
background-repeat: no-repeat;
background-position: center;
background-size: contain;<div id="image">
https://stackoverflow.com/questions/57731199
复制相似问题