Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing an image (in an <img /> tag) using css

Tags:

html

css

I have the following html:

<div class="A">
    <img src="image1.png" width="100px" height="100px"/> 
</div>

In my media queries css style sheet, I would like to replace that image with another one (image2.png).

What is the css code I need to write?

I tried the following:

.A img
{
   background:url("image2.png") no-repeat;
}

But this doesn't seem correct?

like image 392
Ctea Ctea Avatar asked Aug 27 '12 12:08

Ctea Ctea


People also ask

How do I replace one image with another in CSS?

Answer: Use the CSS background-image property You can simply use the CSS background-image property in combination with the :hover pseudo-class to replace or change the image on mouseover.

How do I fix an image in CSS?

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.

How do you IMG an image in CSS?

To add images to a page, we use the <img> inline element. The <img> element is a self-containing, or empty, element, which means that it doesn't wrap any other content and it exists as a single tag. For the <img> element to work, a src attribute and value must be included to specify the source of the image.


2 Answers

If you are using CSS3, then content is the answer:

.A img
{
    content: url("image2.png");
}
like image 185
Michal Klouda Avatar answered Nov 03 '22 01:11

Michal Klouda


You can't modify that in CSS, instead, use a div like this:

<div id='#theImage'></div>

Then in CSS:

#theImage {
    width:100px;
    height:100px;
    background:url("image1.png") no-repeat;
}

Then you can restyle the div using a media query.

like image 27
Rich Bradshaw Avatar answered Nov 03 '22 01:11

Rich Bradshaw