tags:

views:

245

answers:

5

I am familiar with CSS techniques to replace text with an image. For example, here are 9 of them: http://css-tricks.com/nine-techniques-for-css-image-replacement/

Are there any techniques for replacing images? Is there anyway to set the background of an image to an image and then hide or move the foreground of the image (the image src element).

I am trying to write a skin for a site that has an image that I want to replace. Thanks.

From how I understand it he's trying to do this in pure CSS, with no changes to HTML or JavaScript.

That is correct. I am adding a new stylesheet to an existing page. Let say I can not modify HTML or utilize javascript.

A: 

Maybe you can set an opacity of an element and then set the background to the image you want.

Musicfreak: I meant using TWO elements.

EFraim
Opacity affects the background, so that would make the background transparent as well.
musicfreak
A good idea though.
MikeNereson
@EFraim: Well the OP said he can't change the HTML. I didn't downvote you because it's still a good idea, though.
musicfreak
A: 

you will have assign different classes for the two states then write some javascript to have the image change upon an event.

for example:

.firsImage { background:transparent url(/images/someImage.jpg) no-repeat; }
.secondIMage { background:transparent url(/images/image2.jpg) no-repeat; }

HTML:

<div id="imageDiv" class="firstImage"> some content </div>
<a onclick="changeImage()">Change the image!</a>

Javascript:

function changeImage(){
    var imageDiv =  document.getElementById("imageDiv")

    if ( imageDiv.className === "firsImage" )
        document.getElementById("imageDiv").className = "secondImage"
    else
        document.getElementById("imageDiv").className = "firstImage"
}
strife25
He doesn't want the src to change on an event, he wants to use CSS to change the image's src, essentially.
musicfreak
A: 

The best way to replace images is to set the background position. First create the two different images and put them one above the other in the same image. Say your skin element is 50x50 pixels, you'd create a 50x100 image.

Then use some code like this:

.skinElement1 {
  background: #fff url("image.png") no-repeat 0 0;
}
.skinElement2 {
  background: #fff url("image.png") no-repeat 0 -50px;
}

So to view the second image you move the background up by the required amount. You could either use javascript or your server-side code to set the appropriate class.

DisgruntledGoat
+6  A: 

After a little bit of tinkering, I figured it out!

img.someclass {
  background: url("NEW IMAGE URL") top right;
  height: 0;
  width: 0;
  padding: 200px 550px 0 0; /* Insert actual image size (height width 0 0) */
}

This will make the height and width of the actual image 0, but will expand the box to fill the size of the image with padding. The only downside to this is it won't look perfect in older versions of Internet Explorer.

musicfreak
Awesome, musicfreak. Here's a demo of it working for me…http://www.screentoaster.com/watch/stU09VQkdIR11ZRlheXltbU1dS
MikeNereson
Damn, your solution is good, way better than my second attempt (which does work but is unnecessarily complex).
DisgruntledGoat
DisgruntledGoat, still a good attempt. Thanks you and musicfreak for your help.
MikeNereson
+2  A: 

If you have an element surrounding the image, e.g. a DIV, you should be able to set a background image (along with no-repeat and a position) on it, then set the image to display:none.

Alternatively, here's a haphazard solution that seems to work. It positions the image off-screen, then uses the :after pseudo-element to set a background image. It should be workable, but you'll need to fiddle with the values to get it working right. It won't work in IE6 though.

<style>
  img.test {
    background: url('image_to_show.png') no-repeat right top;
    position: relative;
    left: -16000px;
  }
  img.test:after {
    content: ".";
    color: transparent;
    display: block;
    width: 16000px;
  }
</style>

<img class="test" src="image_to_hide.png">
DisgruntledGoat
Hmm, interesting, I wouldn't have thought about using :after. +1
musicfreak
Also, as a side note in case someone wants to use this approach, you should probably make it more than 500px to the left because of widescreen monitors with gigantic resolutions.
musicfreak
musicfreak, you're right; edited. Originally I did this assuming that it would only work if the original image was close to the edge of the canvas - because I didn't think I'd be able to "stretch" the element across other elements.
DisgruntledGoat