2

I am having issues trying to replace this image with CSS, I have no access to the html.

http://jsfiddle.net/ES4mH/

<img 
  width="64" 
  height="64" 
  border="0" 
  style="width: 64px; height: 64px;" 
  src="http://www.nitrografixx.com/2013/lock-icon.gif">
</img>

I tried this, and while it adds the image as a background to the current image, it doesn't replace it.

img[src='http://www.nitrografixx.com/2013/lock-icon.gif'] {
  background: url(http://www.nitrografixx.com/2013/lock_bg.jpg) center !important;  
}
4
  • 2
    Instead of background property, use content property. Commented May 15, 2013 at 12:31
  • 2
    Your code it's working properly. You are 'saying' to CSS that when he finds an img element with attribute src with that given url as his attribute value, then change the background image of your img element into the one you written at his background property on CSS. You have to change background property into content Commented May 15, 2013 at 12:31
  • If you have Javascript, you'll be better off using that instead of CSS, because you can simply directly access the image and change it, instead of trying to apply a content attribute via CSS. Commented May 15, 2013 at 12:33
  • 1
    Img tags are self containing tags you dont need a closing tag. Commented May 15, 2013 at 12:50

2 Answers 2

8

try this

<style>
.className{
    content:url("http://www.nitrografixx.com/2013/lock_bg.jpg");
}
</style>
<img class="className"/>
Sign up to request clarification or add additional context in comments.

7 Comments

As the OP has no access to the HTML, you'd need to change .className to img[src='http://www.nitrografixx.com/2013/lock-icon.gif']?
This is definitely the way to go… in CSS (so: good answer to the question as asked). But Javascript would be a much better way of handling the issue.
Also, the URL you've used is the one that was there already, he'd want the content: url("http://www.nitrografixx.com/2013/lock_bg.jpg"); I think.
adding the content, did not replace the image either, any more ideas ?
It will depend upon the browser you are using, it works in the latest Chrome, but not IE10. I see a pink image instead of the lock. jsfiddle.net/ES4mH/47
|
3

What you are doing is adding the image to the background of the image element but the image element still has the source attribute pointing to the original image and that's why it's not being removed. You should probably use javscript to remove the element and replace it with something else if that's possible.

2 Comments

i do not have access to the html and can not add an java to the site, i can only alter existing css
@user2250423 Java is not javascript. Javascript is available in virtually all browsers.

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.