Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace src of Image tag using css property

Tags:

html

css

Is there any way by which i can replace the image mentioned in SRC attribute of image tab using any of css trick ?

<img src = "setting-icon.png"></img>

i want to replace the setting-icon.png with css property, I am able to put another image in background with background-image property of image tag but i need to hid the one mentioned in src and show what the one i mention in background-image property in css.

Yes this is weird requirement but the thing is i am doing customization in a third party application where i only have control over css, I can not modify the HTML tags.

thanks for reading through !

like image 269
Mayur Randive Avatar asked Feb 23 '17 03:02

Mayur Randive


People also ask

How do I change the src of an image in CSS?

No you can't set the image src attribute via CSS. The closest you can get is, as you say, background or background-image . I wouldn't recommend doing that anyway as it would be somewhat illogical.

How do I replace one image with another in CSS?

Answer: Use the CSS background-image property You can simply use the CSS background-image property in combination with the :hover pseudo-class to replace or change the image on mouseover.

Can we change image src using Javascript?

The cool thing about JavaScript is that you can use it to programmatically alter the DOM. This includes the ability to change an image's src attribute to a new value, allowing you to change the image being loaded.

How do I change a specific image in HTML?

Step 1: Firstly, we have to type the Html code in any text editor or open the existing Html file in the text editor in which we want to change the size of an image. Step 2: Now, place the cursor inside the img tag. And then, we have to use the height and width attribute of the img tag for changing the size of an image.


2 Answers

You can't change the html attributes values with CSS, only javascript.

But, with CSS you can "hide" the image and put a background in its place.

div img {
  height: 0;
  width: 0;
  padding-top: 175px;
  padding-left: 280px;
  background-image: url("http://www.planwallpaper.com/static/cache/6f/82/6f8200c95d588fde83d1f212f674611a.jpg");
}
<img src="http://www.planwallpaper.com/static/cache/a1/4e/a14e880ef245c3d159ba96ebbeb4c8c3.jpg">

<div>Changed img:</div>
<div><img src="http://www.planwallpaper.com/static/cache/a1/4e/a14e880ef245c3d159ba96ebbeb4c8c3.jpg"></div>
like image 20
pol Avatar answered Oct 01 '22 22:10

pol


You can use content:url("image.jpg")

https://developer.mozilla.org/en-US/docs/Web/CSS/content

In your CSS,

.img {
    content:url("/new/image/source.png");
}

If you cannot modify HTML,

img {
    content:url("/new/image/source.png");
}

In HTML,

<img class="img"/>

I have not try this yet, but I not sure if the inline attribute src will overweight the CSS content.

Update

It should work if you already have src for your img element. Thanks @pol

like image 57
X.Creates Avatar answered Oct 01 '22 20:10

X.Creates