简体   繁体   English

CSS 删除图像周围的空白

[英]CSS remove white space around image

I'm using an image from Amazon directly on my website, so I can't manually trim it in an image editing program.我直接在我的网站上使用来自亚马逊的图像,所以我无法在图像编辑程序中手动修剪它。 Around the image is lots of white space, which is annoying because under this image, there is text.图片周围有很多空白,这很烦人,因为在这张图片下,有文字。 And this makes the gap between the image and the text under it really big (because of said white space at the bottom of the image).这使得图像和它下面的文本之间的差距非常大(因为图像底部的空白区域)。

Also, every image contains a different amount of white space, so I can't just set a fixed negative margin-bottom .此外,每个图像都包含不同数量的空白,所以我不能只设置一个固定的负margin-bottom

I know that mix-blend-mode: multiply;我知道mix-blend-mode: multiply; makes the white space transparent, but the gap is still there because the white (now transparent part) still takes up space in the layout.使空白区域透明,但间隙仍然存在,因为白色(现在是透明部分)仍然占用布局中的空间。

How do I make the white part go away so other html elements can use that space?如何使白色部分 go 消失,以便其他 html 元素可以使用该空间?

Here's a codepen: https://codepen.io/AlessioG/pen/VweqMEg这是一个代码笔: https://codepen.io/AlessioG/pen/VweqMEg

You can use the object-fit css property to crop the image.您可以使用适合对象的 css 属性来裁剪图像。

Set the container div to the cropped size that you want the image to be, and set the image to that same width and height, then use object-fit on the image.将容器 div 设置为您希望图像的裁剪大小,并将图像设置为相同的宽度和高度,然后在图像上使用 object-fit。

<div id="crop-container">
    <img id="crop-image">
</div>

#crop-container {
    width:200px;
    height:200px;
}

#crop-image {
    object-fit:cover;
    width:200px;
    height:200px;
}

Just use object-fit and then assign a higher number to width than height in proportion.只需使用 object-fit,然后按比例为宽度分配比高度更高的数字。 Please dont use element names like div p or id names etc. Since it is pretty hard to overwrite that.请不要使用 div p 或 id 名称等元素名称。因为很难覆盖它。

 .image { margin: 0; object-fit: cover; width: 250px; height: 200px; }.header-two { margin: 10px; font-weight: bold; display: block; font-size: 20px; }
 <div class="container"> <img class="image" src="https://i.imgur.com/l2QrzBg.jpg"> <h2 class="header-two">This is a text</h1> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM