11

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 !

1

3 Answers 3

10

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

Sign up to request clarification or add additional context in comments.

9 Comments

He can't modify the HTML, so I assume he can't add the class on the image.
change to element selector then
It worked , content url overwrites the src of img tag
This may work on Chrome, but it won't work on Firefox
Thanks Li and Ali for your time and thoughts .. :) have a great day
|
4

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>

3 Comments

will try this and let you know in sometime , thanks pol for your time !
@MayurRandive can you use jQuery/Javascript?
@ Deepak Yadav : No
2

Good question to learn about unnoticed selectors using CSS,

Additionally, You can read more about other selectors,

For example:

img: hover {}

and some other nice selectors for different range of elements

:active

:after

:before

:first-child

:first-letter

:first-line

:focus

:hover

:lang

:link

:visited

You can even conditionally select like this:

img[src="setting-icon.png"] {
    border: 1px solid #000000;
    content:url("/new/image/source.png");
}

Reference: W3.org - Advanced Selectors

Microsoft Web Expression 4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.