Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FF and IE don't load img src from CSS

I'm setting the src for an image with css like this

#Banner {
content: url(../Banners/prussia-awesomeness.gif);
width: 1000px;
}

here's my image

   <div id="Header" class="Header">
        <img id="Banner" src="as"/>
    </div>

the image loads in google chrome with the proper img src (../Banners/prussia-awesomeness.gif)

in internet explorer and firefox it keeps the src "as".

Does ie and ff not support loading image sources from css?

EDIT:

adding

#Banner:after {
content: url(../Banners/prussia-awesomeness.gif);
width: 1000px;
}

made it work in firefox, ie however still refuses to cooperate.

also tried adding :before (with : and ::), which made no difference in any browser

like image 992
KristianMedK Avatar asked Aug 13 '13 06:08

KristianMedK


2 Answers

It seems that CSS content property doesn't work for IMG but other elements for IE & Safari. Check out this Fiddle. For Safari check this out.

HTML:

<div id="Header" class="Header">
        <img id="Banner1" src="as"/>
    <h1 id="Banner">test</h1>
    </div>

CSS:

#Banner:before {
content: url(http://w3c.org/2008/site/images/favicon.ico);
width: 1000px;
}
#Banner1:before {
content: url(http://w3c.org/2008/site/images/favicon.ico);
width: 1000px;
}

It may not work with IE8 if you don't have DOCTYPE! specified.

For FF you need to use :before or :after pseudo elements to make it work. For more info go through this.

like image 91
Nilesh Thakkar Avatar answered Oct 17 '22 03:10

Nilesh Thakkar


According to MDN,

The content CSS property is used with the ::before and ::after pseudo-elements to generate content in an element.

So, use content with pseudo elements like :after or :before(single colon is for IE8 compatibility). Something like this:

#Banner:after {
    content: url(../Banners/prussia-awesomeness.gif);
    width: 1000px;
}
like image 2
Mr_Green Avatar answered Oct 17 '22 01:10

Mr_Green