37

I have a website that has an image with a src attribute and I would like to change the src location of that image with an image of my own. The image lives in a div component. I can't change the HTML and am looking ways to change it using CSS only please.

Div component:

<div class="application-title">
<img style="margin-top: 3px;height: 45px;" src="image.svgz">
</div>

Image component(CSS file):

.application-title IMG
{
    float: left;
    margin-top: 5px;
    margin-right: 10px;
    opacity: 1;
    margin-left: 0px;
    visibility: hidden;
}
2

4 Answers 4

46

You can use a background image

.application-title img {
  width:200px;
  height:200px;
  box-sizing:border-box;
  padding-left: 200px;
  /*width of the image*/
  background: url(http://lorempixel.com/200/200/city/2) left top no-repeat;
}
<div class="application-title">
  <img src="http://lorempixel.com/200/200/city/1/">
</div><br />
Original Image: <br />

<img src="http://lorempixel.com/200/200/city/1/">

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

2 Comments

Man, you won't believe. I just found that solution myself. Its the box -sizing and padding that would make it work instantaneously.
Saw this link and I was like.. booya! css-tricks.com/replace-the-image-in-an-img-with-css
23

Here is another dirty hack :)

.application-title > img {
display: none;
}

.application-title::before {
content: url(path/example.jpg);
}

1 Comment

This is the closest solution, but it's not possible to side the image.
22

you can use: content:url("image.jpg")

<style>
.your-class-name{
    content: url("http://imgur.com/SZ8Cm.jpg");
}
</style>

<img class="your-class-name" src="..."/>

3 Comments

can you please clarify how to add local url path ? <img class="grow-together-component__bottom-img" src="./images/bg-section-bottom-mobile-1.svg" alt="bottom design">, I want to change to something else from "./images/.." but it is not working
This should be the starred answer. It directly replaces the image URL as you really want, without offset hacks or other messiness. Can also use other content (like plain text). IOW it's fab.
@tekHedd I agree. The browser compatibility is also great.
5

You could do this but it is hacky

.application-title {
   background:url("/path/to/image.png");
   /* set these dims according to your image size */
   width:500px;
   height:500px;
}

.application-title img {
   display:none;
}

Here is a working example:

http://jsfiddle.net/5tbxkzzc/

1 Comment

I have added a jsfiddle. Had to add width and height, but it does work.

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.