有没有办法在<img>标签上应用背景颜色?
<img src="abc.jpg" style="background-color:red;">
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用。
请帮我 。
谢谢
小智 8
是的,这是可能的。您只需要对图像应用填充。然后你可以得到下面的结果:
或者
您还可以使用 .png 图像并应用透明属性。
img {
max-width: 50%;
}Run Code Online (Sandbox Code Playgroud)
<img src="http://pngimg.com/uploads/mario/mario_PNG53.png" style="background-color:red;padding:20px;">Run Code Online (Sandbox Code Playgroud)
您可以使用background-blend-mode属性。
特别是,background-blend-mode: multiply似乎是您在这里需要的。
从规范:
乘法混合模式
源颜色乘以目标颜色并替换目标颜色。
.image {
width: 200px;
height: 150px;
background-image: url(https://placeimg.com/200/150/animals);
}
.blended {
background-color: red;
background-blend-mode: multiply;
}Run Code Online (Sandbox Code Playgroud)
<div class="image"></div>
<div class="image blended"></div>Run Code Online (Sandbox Code Playgroud)
注意:
由于您无法更改标记,因此仍然可以通过上述方式设置图像样式。(虽然它有点hacky)
我们可以通过向图像添加填充然后将图像的宽度和高度设置为零来替换具有相同背景图像的 img 和 css。(更多细节在这里)
img {
padding: 75px 100px;
width: 0;
height: 0;
background-color: red;
background-blend-mode: multiply;
background-image: url(https://placeimg.com/200/150/animals);
}Run Code Online (Sandbox Code Playgroud)
<img src="https://placeimg.com/200/150/animals">Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24264 次 |
| 最近记录: |