I have this image tag:
<img src="http://placehold.it/200x200"/>
I need to replace the image via css (because I can't edit the html),so I use this css:
img {
content: url('http://lorempixel.com/200/200');
}
it's working well on chrome but not working in firefox and ie,any idea why?
To replace the text with a logo image, use a negative text indent value (it has to be a pretty high value, like the one below) and insert the image using background: url(). Make sure you define the width and height as well, or the image won't render.
To do so, we change the visibility of this text using CSS to hidden first. Then we add a new text at the exact same position, using the pseudo elements and corresponding explicit positioning. Note that after is the pseudo element in use here. We use the visibility modifier once again to show the new text.
CSS position property is used to set the position of text over an image. This can be done by enclosing the image and text in an HTML “div”. Then make the position of div “relative” and that of text “absolute”. The absolute elements are positioned relative to their parent (div).
To keep your background fixed, scroll, or local in CSS, we have to use the background-attachment property. Background-attachment: This property is used in CSS to set a background image as fixed or scroll. The default value of this property is scroll.
1) Set width and height of your image to 0.
2) Add a padding of 100px.
3) Add your new image via a background image
img {
display: inline-block;
background: url(http://lorempixel.com/200/200) no-repeat; /* <--- */
width: 0; /* <--- */
height: 0; /* <--- */
padding: 100px; /* <--- */
}
.replace {
display: inline-block;
background: url(http://lorempixel.com/200/200) no-repeat;
width: 0;
height: 0;
padding: 100px;
}
<h4>Image from placehold.it:</h4>
<img src="http://placehold.it/200x200"/>
<hr>
<h4>Same image in markup - but replaced via CSS</h4>
<img class="replace" src="http://placehold.it/200x200"/>
FIDDLE
Well, lets say we added padding to your original img element:
img {
padding:100px;
background: pink;
}
The result is your original img with 100px of pink background on each side
Now set width/height to 0:
img {
padding:100px;
background: pink;
width:0;height:0;
}
You get a 200px X 200px pink area: (So now you've eliminated your original image)
Now change the pink background to your new background
background: url(http://lorempixel.com/200/200) no-repeat;
and you're done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With