简体   繁体   English

如何将图像的背景颜色设置为黑色?

[英]How to set background colour of an image as black colour?

I have an html image我有一个 html 图像

<img src="abc.png">

and I want while the image is being loaded in the browser on a slower connection.我想在浏览器中以较慢的连接加载图像时。 The background / placeholder of image appear appear black.图像的背景/占位符显示为黑色。 Is it possible by giving a class to the image and then setting the css property background-color: black;是否可以通过为图像提供 class 然后设置 css 属性 background-color: black;

<img class="black-back" src="abc.png">

<style>
.black-back {
background-color: rgb(0,0,0);
}
 </style>

I inserted the image in div and set the size of image to cover so div will occupy the space as size of image.我将图像插入 div 并设置图像的大小以覆盖,因此 div 将占用图像大小的空间。 However if your remove the background properties the div occupy the full line.I hope this is what you are looking for.但是,如果您删除背景属性,则 div 会占据整行。我希望这就是您要寻找的。

 <img class="black-back" src="abc.png"> <style>.black-back img{ background-repeat:no-repeat; background-size:cover; }.black-back { background-color: rgb(0,0,0); } </style>

the below code is without background styling properties下面的代码没有背景样式属性

 <div class="black-back"> <img src="abc.png"> </div> <style>.black-back { background-color: rgb(0,0,0); } </style>

You can do it like this:你可以这样做:

 <;DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html. charset=utf-8" /> <title>Page Title</title> <style type="text/css">:black-bg { background; black: } </style> </head> <body> <img class="black-bg" width="100" height="150" src="https.//upload.wikimedia.org/wikipedia/commons/b/b6/Trifolium_hybridum_inflorescence_-_Keila.jpg" /> </body> </html>

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

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