简体   繁体   English

更改 div 和图像的背景

[英]Change background of div and image

As the title suggests, I wanted to add a background both to the image and the div itself.如标题所示,我想为图像和 div 本身添加背景。 I see it's being used on amazon but can't seem to figure out the trick behind it.我看到它正在亚马逊上使用,但似乎无法弄清楚它背后的技巧。 What I mean is, the image has a white background, but the image as it's been applied onto the div doesn't show that white space.我的意思是,图片有白色背景,但应用到 div 上的图片不会显示空白。 Like so:像这样:

Image with white background:白色背景的图像:

白色背景图像

Image being rendered onto the div:正在渲染到 div 上的图像:

在 div 上呈现的图像

Here is what I have tried so far:到目前为止,这是我尝试过的:

 .grid-wrapper { display: grid; grid-template-columns: 1fr 2fr; background: #fff; box-shadow: rgba(0, 0, 0, 0.02) 0px 1px 3px 0px, rgba(27, 31, 35, 0.15) 0px 0px 0px 1px; border-radius: 4px; }.image-block { background: red; margin: auto; }.image-block img { width: 100% height: 100%; }.information { padding: 10px; }.information p { font-size: 0.875rem; line-height: 1.5; }
 <div class="grid-wrapper"> <div class="image-block"> <img src="https://m.media-amazon.com/images/I/61xgpXecLML._AC_UY218_.jpg"> </div> <div class="information"> <p>Logitech M510 Wireless Computer Mouse for PC with USB Unifying Receiver - Graphite</p> <p>Price: $100</p> </div> </div>

You can see a bit of the background: red;你可以看到一些background: red; just under the image.就在图像下方。

If you want a transparent image background you should use a PNG image, then for the red background just remove the background color from the image-block如果你想要一个透明的图像背景,你应该使用 PNG 图像,然后对于红色背景,只需从图像块中删除背景颜色

.image-block {
    margin: auto;
}

The problem here is that the first image you posted has a white background, but contrary to your assumption the second image doesn't have a white background, it has a transparent background and that makes all the difference.这里的问题是您发布的第一张图片有白色背景,但与您的假设相反,第二张图片没有白色背景,它有透明背景,这一切都不同。
When wrapping around an image with a transparent background a div element will be displayed on the transparent background.当环绕具有透明背景的图像时,一个 div 元素将显示在透明背景上。 That's likely what is shown in the second image.这可能是第二张图片中显示的内容。
Then there's the question, How do you use an image with a transparent background?那么问题来了,如何使用具有透明背景的图像?
The image you are using is in.jpg format that doesn't support transparent background, you can use either.png or the.svg for that.您使用的图像是不支持透明背景的.jpg 格式,您可以使用.png 或the.svg。 It should work without changing the code other than the image import.它应该可以在不更改图像导入以外的代码的情况下工作。

you should use png image and then whatever background color you need can be given to div wrapping the image.您应该使用 png 图像,然后可以将您需要的任何背景颜色提供给包装图像的 div。

With a.jpg image you may cheat as this: I'm sorry for my English, I'm not sure to understand what's your goal...使用 .jpg 图片你可能会这样作弊:对不起我的英语不好,我不确定你的目标是什么......

 .grid-wrapper { display: grid; grid-template-columns: 1fr 2fr; background-color: #ffffff; box-shadow: rgba(0, 0, 0, 0.02) 0px 1px 3px 0px, rgba(27, 31, 35, 0.15) 0px 0px 0px 1px; border-radius: 4px; }.image-block { background-color: red; margin: auto; padding:20px; }.image-block img { }.image-container{ background-color: white; padding:20px; }.information { padding: 10px; }.information p { font-size: 0.875rem; line-height: 1.5; }
 <div class="grid-wrapper"> <div class="image-block"> <div class="image-container"> <img src="https://m.media-amazon.com/images/I/61xgpXecLML._AC_UY218_.jpg" alt="image"> </div> </div> <div class="information"> <p>Logitech M510 Wireless Computer Mouse for PC with USB Unifying Receiver - Graphite</p> <p>Price: $100</p> </div> </div>

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

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