Html <img> background | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Html <img> background

Is it possible to add a background-image to an img-tag? I tried like this: <img src = "imageOne.png" style = "height: 500px; width: 250px; background-image: imageTwo.jpg; opacity: 0.5;" /> It didn't get a background at all. Please note that imageOne and imageTwo are only example names, not the real ones.

23rd Sep 2018, 10:16 AM
Joel Kronqvist
Joel Kronqvist - avatar
2 Answers
+ 1
This might be a concept to revisit later, but... Most of the time, backgrounds are for aesthetics, so it's better to put them in the stylesheet as suggested in the prior post. If a background image also happens to have semantic value (adds meaningful content), then consider leaving it in the HTML file in an img tag. More on what to weigh here: https://stackoverflow.com/questions/492809/when-to-use-img-vs-css-background-image
23rd Sep 2018, 10:50 AM
Janning⭐
Janning⭐ - avatar
0
OK, thank you everyone, I didn't know that url() was necessary🤦 I simply wrote the images name...
30th Sep 2018, 5:25 AM
Joel Kronqvist
Joel Kronqvist - avatar