首页 > 解决方案 > 如何在css和html中为图像属性添加背景颜色

问题描述

我正在尝试更改图像的背景颜色。通常,为此,我使用;

.img {
background-image: url("./image.jpg");
background-color: "#3333";
}

但我正在尝试使用 html img 标签做同样的事情。但这似乎不起作用。所以以一种形式;

<div class="container">
  <img class="image" src="./image.jpg"/>
</div>

标签: htmlcssbackground

解决方案


在 css...

.name -> Style is assigned to a "class="
#name -> Style is assigned to an "id="
 name -> Style is assigned to an tag <name></name>

你可以试试这个:(在你的 CSS 中将 .img 更改为 img)

img {
 background-image: url("./image.jpg");
 background-color: #333;
}

<div class="container">
  <img class="image" src="./image.jpg"/>
</div>

注意:您可能需要 .png 或其他透明图像才能通过图像查看背景。


推荐阅读