FF and IE don't load img src from CSS

孤者浪人 提交于 2019-12-22 05:50:10

问题


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


回答1:


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.




回答2:


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;
}



回答3:


Try to removing the double quotes to a content value

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


来源:https://stackoverflow.com/questions/18202306/ff-and-ie-dont-load-img-src-from-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!