简体   繁体   中英

Can I use different CSS background images based on browser type?

I have a basic website design and for the logo I have created a simple SVG image. This image is very basic and just contains some text, a polyline and a dropshadow effect using feOffset as described in the answer to this question SVG drop shadow using css3 .

The image looks fine when viewed in Chrome and IE10 but I get really rough looking pixelation when viewed in Firefox. I basically want to know if it is possible to add a background image that is dependent on the browser type such that I can create an acceptable image for each browser. ie

if (browser == X)
{ 
   use image Y
}
else
{
   use image Z
}

but only using CSS and/or HTML (I have no knowledge of javascript or jQuery).

Given how basic my image is I cannot believe that I am the only person to have had this problem, so I am wondering how web designers cope with this problem as the only solution I can think of is to create a raster image instead of the SVG but this is something that I really want to avoid if at all possible.

Yes you can, but it's quite hacky. You have to create and target the element in a manner that it can only be read by the specific browser.

Copied and modified from css-tricks.com

/***** Selector Hacks ******/

/* IE6 and below */
* html #uno  { 
background-image: url(blabla.jpg) }

/* IE7 */
*:first-child+html #dos { background-image: url(blabla.jpg) } 

/* IE7, FF, Saf, Opera  */
html>body #tres { background-image: url(blabla.jpg) }

/* IE8, FF, Saf, Opera (Everything but IE 6,7) */
html>/**/body #cuatro { background-image: url(blabla.jpg) }

/* Opera 9.27 and below, safari 2 */
html:first-child #cinco { background-image: url(blabla.jpg) }

/* Safari 2-3 */
html[xmlns*=""] body:last-child #seis { background-image: url(blabla.jpg) }

/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
body:nth-of-type(1) #siete { background-image: url(blabla.jpg) }

/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
body:first-of-type #ocho {  background-image: url(blabla.jpg) }

/* saf3+, chrome1+ */
@media screen and (-webkit-min-device-pixel-ratio:0) {
 #diez  { background-image: url(blabla.jpg)  }
}

/* iPhone / mobile webkit */
@media screen and (max-device-width: 480px) {
 #veintiseis { background-image: url(blabla.jpg)  }
}

/* Safari 2 - 3.1 */
html[xmlns*=""]:root #trece  { background-image: url(blabla.jpg)  }

/* Safari 2 - 3.1, Opera 9.25 */
*|html[xmlns*=""] #catorce { background-image: url(blabla.jpg)  }

/* Everything but IE6-8 */
:root *> #quince { background-image: url(blabla.jpg)  }

/* IE7 */
*+html #dieciocho {  background-image: url(blabla.jpg) }

/* Firefox only. 1+ */
#veinticuatro,  x:-moz-any-link  { background-image: url(blabla.jpg) }

/* Firefox 3.0+ */
#veinticinco,  x:-moz-any-link, x:default  { background-image: url(blabla.jpg)  }

Are you not open to doing this through jQuery at all? It'd be much easier that way.

window.navigator对象包含有关访问者浏览器的信息,使用navigator对象可以使用javaScript设置css。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM