html图片设置底部,html – 使用css显示图像裁剪图像的顶部和底部

更新

我已经更新了答案,但我不确定你想要什么,你只是说了你不想要的.所以我假设你想要:

>保持纵横比

>避免种植

>没有黑条,只是图像边缘到边缘

我们知道这是一张纵横比为16:9的宽屏海报,所以如果您想要宽度为270像素,请执行以下操作:

将宽度除以16

270/16 = 16.875

取该商并乘以9

16.875 * 9 = 151.875

向上或向下舍入

Round up to 152px

使用结果更改高度,然后应用object-fit:cover

152px is the height of an image that's 270px wide and has an aspect ratio of 16:9.

请查看Fiddle和更新的代码段

编辑

为了反映更新并更好地理解OP的目标,编辑了此代码段.

object-fit是一个简单的CSS属性.请参阅此article下面的代码段已注释.顺便说一句,这个代码片段中你需要的唯一代码就是对象:cover,其余的样式和标记仅用于演示.

片段

/* We know this is a widescreen poster with the aspect ratio of 16:9, so if you want a width of 270px, do the following:

270/16 = 16.875

16.875 * 9 = 151.875

Round up to 152px

152px is the height of an image that's 270px wide and has an aspect ratio of 16:9 */

.bg1 {

width: 270px;

height: 152px;

object-fit: cover;

outline: 2px dashed blue;

}

.bg2 {

width: 270px;

height: 152px;

object-fit: contain;

outline: 2px dashed blue;

}

.bg3 {

width: 270px;

height: 152px;

outline: 2px dashed blue;

}

aside {

height: 100%;

width: 40%;

display: inline-block;

position: absolute;

right: 0;

top: 0;

}

figure {

height: 180px;

width: 270px;

max-width: 50%;

}

.pointer {

position: absolute;

}

.pointer b {

font-size: 32px;

}

#a.pointer {

top: 43%;

left: 52%;

}

#b.pointer {

bottom: 5%;

left: 52%;

}

.box {

width: 600px;

height: 450px;

position: relative;

}

.spacer {

position: relative;

padding: .1% 0;

width: 2px;

}

code {

font-family: Consolas;

color: red;

}

object-fit: cover

hqdefault.jpg

object-fit: contain

hqdefault.jpg

Without anything but the height property

hqdefault.jpg

object-fit: cover will stretch an image to the edges of it's container (parent element) while keeping aspect ratio and cropping.

But when given the correct dimensions, object-fit: cover will result in a perfect fit edge to edge. No cropping either.

object-fit: contain will stretch an image to the edges of it's container while keeping aspect ratio but will not crop at the edges, so as you can see, this image has space left and right. At wider dimentions, the space will manifest below and above.

This is the image when set to it's aspect ratio at 270 x 152px and as you can see, without object-fit:cover, math alone will not resolve the problem.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值