Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG background-image via CSS not showing in webkit browsers

I am an open-source developer and going for pure HTML5 and CSS3 with my webframework (http://m-m-m.sf.net). I want to draw a validation error icon via input:invalid rule in CSS aligned to the right. But it is only working in FF but not in webkit based browsers such as Chrome or Safari. I created a minimal standanlone html (without validation and :invalid) for testing:

<!DOCTYPE HTML>
<html>
  <head>
  <style type="text/css">
  <!--
input {
  border-color: #ff2222;
  background-color: #ff8888;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='1em' height='1em'><g><circle cx ='8' cy ='8' r ='8' style='fill:%23ff0000;stroke:none'/><text x='6' y='13' style='font-size:14px;fill:%23ffffff;stroke:none;font-family:Monospaced;font-weight:bold'>!</text></g></svg>");
  background-repeat: no-repeat;
  /* background-size: auto; */
  background-position: 98% 50%;
}
  -->
  </style>
  </head>
  <body>
    <input type="text" placeholder="type here" />
  </body>
</html>

Any ideas?

like image 372
Jörg Avatar asked May 06 '13 19:05

Jörg


People also ask

Why is SVG background image not showing?

Why is SVG not showing up in Chrome? Chrome browser doesn't support SVG format with <img src=””> tag anymore. Peoples are facing issues that they aren't able to see their images in Chrome browser because the images are in SVG format. So I found a solution to display SVG image in Chrome browser with <object> tag.

Why is my background not showing in CSS?

Make sure the image path is set correctly in the background-image url. Once you have made sure that your CSS file is linked correctly, also check that the image itself is set correctly. Again, you will want to open your code inspector in the browser to check.

Can I use SVG as background image CSS?

SVG images can be used as background-image in CSS as well, just like PNG, JPG, or GIF. All the same awesomeness of SVG comes along for the ride, like flexibility while retaining sharpness. Plus you can do anything a raster graphic can do, like repeat.

Why background image is not showing in Div?

I recommend moving your css from the inline scope. Assuming that your . png file actually exists, try setting the background size and repeat tags. If that doesn't work, try checking in your browser's developer tools for the response codes and making sure that the url is correct.


1 Answers

You may try base64 encoded data uris for svg background images.

This is how it would look in CSS:

input {
  border-color: #ff2222;
  background-color: #ff8888;
  background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHg9IjBweCIgeT0iMHB4IiB3aWR0aD0iMTZweCIgaGVpZ2h0PSIxNnB4Ij48Zz48Y2lyY2xlIGN4PSI4IiBjeT0iOCIgcj0iOCIgZmlsbD0iI2ZmMDAwMCIgc3Ryb2tlPSJub25lIiAvPjx0ZXh0IHg9IjYiIHk9IjEzIiBzdHlsZT0iZm9udC1zaXplOiAxNHB4OyBmb250LWZhbWlseTogU2Fucy1zZXJpZjsgZm9udC13ZWlnaHQ6IGJvbGQ7IHN0cm9rZTogbm9uZTsgZmlsbDogI2ZmZmZmZjsiPiE8L3RleHQ+PC9nPjwvc3ZnPg==");
  background-repeat: no-repeat;
  /* background-size: auto; */
  background-position: 98% 50%;
}

I changed the font-family to Sans-serif as the other (Monospaced) font got rendered 2px more to the right by Chrome on Windows, you could play with this a little. I used this online encoder.

Here is the same svg with the Monospaced font:

  background-image: url("data:image/svg+xml;charset=utf-8;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHg9IjBweCIgeT0iMHB4IiB3aWR0aD0iMTZweCIgaGVpZ2h0PSIxNnB4Ij48Zz48Y2lyY2xlIGN4PSI4IiBjeT0iOCIgcj0iOCIgZmlsbD0iI2ZmMDAwMCIgc3Ryb2tlPSJub25lIiAvPjx0ZXh0IHg9IjYiIHk9IjEzIiBzdHlsZT0iZm9udC1zaXplOiAxNHB4OyBmb250LWZhbWlseTogTW9ub3NwYWNlZDsgZm9udC13ZWlnaHQ6IGJvbGQ7IHN0cm9rZTogbm9uZTsgZmlsbDogI2ZmZmZmZjsiPiE8L3RleHQ+PC9nPjwvc3ZnPg==");

and a jsfiddle

like image 50
Martin Turjak Avatar answered Sep 28 '22 15:09

Martin Turjak