3

I'm reworking a site but only have permission to change the CSS. Most of the elements I need to change are properly tagged as id's or classes, but a few places have ids or classes listed inside an img tag.

I want to replace that image in the img tag using only css. Is there a way to do this? ie, hide the src img and have only my css referenced image visible?

5
  • 7
    That's not considered good practice. It's considered good practice to put images with html when they can be considered content and with css when they can be considered design. Commented Nov 28, 2011 at 20:15
  • I understand. Still, it's what the task requires. the image in question is a button. being replaced with the same button in a different graphic style, if that helps. Commented Nov 28, 2011 at 20:46
  • Buttons should be anchor tags ( <a> ) with a CSS background image. Commented Nov 28, 2011 at 21:31
  • Sometimes buttons are input types in a form. Commented Nov 28, 2011 at 21:57
  • Even easier, see stackoverflow.com/questions/3779866/… Commented Jun 14, 2012 at 16:15

4 Answers 4

5

sorry for such a late post, (almost a year, i know..), but i had the same exact problem Dreamling, Some of the html used on our site is called up externally, so editing the html was not an option for me either. Here's how i solved the problem... Using only CSS.

Use Firebug if you have it. Now look for the image you'd like to replace in the HTML. (firebug will show the id's and classes of the elements)

Your HTML should look something like this for it to work. (with an img src element inside a span element)

    <span class="Dreamlings_ClassA Dreamlings_ClassB">
    <img src="http://www.dreamlingsSite.com/dreamlingspic.png" alt="Dreamling's Pic">
    <span>[This is just an extra span!] </span>
    </span>

Now for the CSS :)

Call up the first element by class in the css. (use the last class name to be more specific in with editing [if you have multiple span elements with same first class name])

    <span class="Dreamlings_ClassB">

should look something like this..

span.Dreamlings_ClassB {
    background-image: url('../dreamlingsnewpic.png') !important; 
}

and to hide that pesky image in the img src element..

span.Dreamlings_ClassA img {
    display: none !important;
}

And thats it! :)

p.s. I was using the !important tags in my css to overwrite other external stylesheets.. but you don't have to use the tags if yours css will work without them. (you just have to be more specific in the css with id's and classes)

Hope this helped!

-tony

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

Comments

1

If your image tag is inside a container, anything that's a block, then use this:

<style>

#container {
   background: url('image.png') no-repeat;
   text-indent: -9999;
}

</style>
<div id="container">
    <img src="image.png" alt="image to be replaced" />
</div>

As others said, it's really not good practice, but it works. Only tested in Chrome.

4 Comments

Well, the button is actually the src inside a form, or an input button. It does have a container of sorts, the button is inside an li, but there are several li's with the same classes called. <input type="image" src="images/btn_next.png" alt="Next" id="btn_next">
Sorry for the delay, could you post the whole form? You could always use a series of adjacent selectors (li + li + li), then cancel the others with (li + li + li + li), for example. Gets complicated though... lol
Ok, if you can use CSS3, use the :nth-child selector. For example, li:nth-child(4), if you're trying to access the 5th row.
Yes, but this is generic css for a set of similar (but not exact copies) pages with forms. So, the id's and classes are what I have to work with. Thanks for the input though. :D
0

I want to replace that image in the img tag using only css.

Not that I know of, no. An image's src attribute can't be altered from CSS.

I also can't think of a workaround to do this, not even a terribly kludgy one. You can of course assign a background-image to the image element, but the actual image will always be in front of it,

You would have to have the original HTML altered in a way so the original button is a <button> element with a background-image property - that you can override using CSS.

2 Comments

Ok, I was wondering why it was so hard to find the info about it. I've never had to do it, usually I have access to everything to change. In fact, it does seem like my background: is working, but doesn't seem to show up with the image on top of it. So I tried messing with the z-index, I was even getting into input#btn_next[src$=".png"] territory...
It's more a puzzle than I think the first responders realized. If it can't be done via css only, it won't hurt to have the css working for whenever the next html revision is available. That part is just currently out of my hands. Thanks!
0

Restricting access to the HTML but allowing access to edit CSS is odd practice. Both elements go hand in hand to produce the page.

Anyway, you could try removing or changing the name of "btn_next.png" so that it doesnt display when called from "src" and make the CSS the following:

#btn_next {
   background: url('image.png') no-repeat;
   display:block;
   width:150px; /* for example */
   height:30px; /* for example */
}

If that doesnt work, the only other way would be to hide the input button and replace the li row with a background image but then the button will cease to work. Unless you have access to an already included javascript file, then you can look at other solutions.

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.