11

I have my current code:

#content img[src="/img/test.gif"] { 
    background-image:url(dark-img.png) !important;
}

From my understanding !important; overrides existing values?

Why isn't this overriding the current HTML image in place there? The background shows up, behind the HTML image.

I want it in front of the HTML image, is this possible using CSS or JS?

Edit: For what its worth, im making a userscript that will modify the existing style of the site. So I do not have direct access to the HTML image.

3
  • 3
    The image of an img element is not a "existing background-image value". Background images and "normal" (foreground, content) images are two separate things and you can't use one to override the other. Commented Sep 23, 2010 at 15:29
  • 1
    Wrong tool. You want to use JS for this. Commented Sep 23, 2010 at 15:45
  • 1
    Not necessary the wrong tool. See my answer. Commented May 1, 2012 at 10:52

10 Answers 10

13

You don't need javascript for image replacement! As long as you can identify the image by a CSS selector, you can use CSS to do the trick.

See the solution here http://www.audenaerde.org/csstricks.html#imagereplacecss

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

2 Comments

Genius... some people are tweaking things in places where they dont have access to js nor html (for example VOC templates in Dynamics) this is an amazing way of doing this with pure css...
@EgliBecerra This also can help to change particular tab favicons in Firefox using userChrome.css! This solution is really unthinkable!
11

Here is the code using only css:

<img src="tiger.jpg" 
     style="padding: 150px 200px 0px 0px; 
            background: url('butterfly.jpg'); 
            background-size:auto; 
            width:0px; 
            height: 0px;">
  • sets the image size to 0x0,
  • adds a border of the desired size (150x200), and
  • uses your image as a background-image to fill.

If you upvote this answer, give @RobAu's answer an upvote, too.

2 Comments

This approach does require a known/fixed size image though right?
@rainabba: You fix the size of the space. If the background image is larger, it is cropped. If it is smaller, it is tiled.
2

The replacement of an image in CSS can be done in several ways. Each of them has some drawbacks (like semantics, seo, browsercompatibility,...)

On this link 9 (nine!) different techniques are discussed in a very good way :

http://css-tricks.com/css-image-replacement/

If you are interested in css in general : the whole site is worth a look.

Comments

2

The background-image property, when applied to an image, refers to (drum roll ... ) the background-image of the image. It will always be behind the image.

If you want the image to appear in front of the image, you are going to have to use two images, or another container with a background-image that covers the first image.

BTW, it is bad practice to rely on !important for overriding. It can also be ineffective since 1) it can't override declarations in an element's style attribute, and 2) it only works if it can work based on the markup and the current CSS. In your case, all the huffing and puffing and !important declarations won't make an image do something it can't do.

2 Comments

Yeah sorry - not thinking clearly. I understand its the background image. Is there a way I can override it using Javascript? (thinking GM).
You can definitely override it using Javascript. Just get a reference to the image (let's say "myImage") and do myImage.src = "dark-img.png" or whatever urlyou want.
2

I answered a similar question in another SO page..

https://robau.wordpress.com/2012/04/20/override-image-src-in-css/

<img src="linkToImage.jpg" class="egg">

.egg {
  width: 100%;
  height: 0;
  padding: 0 0 200px 0;
  background-image: url(linkToImage.jpg);
  background-size: cover;
}

So effectively hiding the image and padding down the background. Oh what a hack but if you want an with alt text and a background that can scale without using Javascript?

Comments

1

Use your 'userscript' to change 'src' attribute value.

If there is an ID there, you can do this:

document.getElementById('TheImgId').src = 'yournewimagesrc';

If there is no ID:

var imgElements = document.getElementsByTagName('img');

Do iteration of imgElements. When its src value is match with your criteria, change the value with your own, do break.

Update:

Javascript:

<script language="javascript">
    function ChangeImageSrc(oldSrc, newSrc) {
        var imgElements = document.getElementsByTagName('img');
        for (i = 0; i < imgElements.length; i++){
            if (imgElements[i].src == oldSrc){
                imgElements[i].src = newSrc;
                break;
            }
        }
    }
</script>

HTML:

<img src="https://i.sstatic.net/eu757.png" />
<img src="https://i.sstatic.net/IPB9t.png" />
<img src="https://i.sstatic.net/IPB9t.png" />
<script language="javascript">
    setTimeout("ChangeImageSrc('https://i.sstatic.net/eu757.png', 'https://i.sstatic.net/IPB9t.png')", 5000);
</script>

Preview:

alt text

The first image will be replaced after 5 secs. Try Live Demo.

4 Comments

You've lost me completely, I've hardly ever touched JS let alone GM.
src of img is not style, cannot be overrided with CSS. Everybody suggests you to use Javascript.
I understand that bit. The image contains no ID, its just pure HTML, with src="image2.jpg". I tried the 'If there is no ID' thing and it doesnt seem to work from what im trying. Do I need to learn JS before I go any further into this mod? (requires 2 images to be done via JS, by the looks of things).
You don't need javascript. See my answer
0

you'll have to place the first image as a background-image too. Then you can override it. You could do in a "standard" css file for the site, and every user gets its own, where he can override what he wants.

1 Comment

I cant change the first image, as its run by the site owner - not me. Its to modify the site using Stylish.
0

i agree with all the answers here, just thought id point out that 'browsers' such as IE won't like the img[src="/img/test.gif"] as a means of selecting the image. it would need a class or id.

1 Comment

Older versions of IE won't like it. It should be fine in IE8, though.
0

The images shown in tags are in the foreground of the element, not the background, so setting a background image in an won't override the image; it'll just appear behind the main image, as you're seeing.

What you want to do is replace the image. Here's your options:

  • Start with an element with a background image, not an tag. Then changing the background image in CSS will replace it.

  • Start with an tag, but use Javascript to change the src attribute. (this can't be done in CSS, but is simple enough in JS)

EDIT:

Seeing your edit in the question, I'd suggest option 2 - use Javascript to change the src attribute. It's quite simple; something like this would do the trick:

document.getElementById('myimgelement').src='/newgraphic.jpg';

Comments

0

You should be able to replace it by just doing something like:

.image {
    content: url('https://picsum.photos/seed/picsum/400');
    width: 200px;
    height: 200px;
}

Unfortunately seems that it does not work in Firefox :(

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.