Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I convert an image to CSS3?

Supposing that I have a polygon image PNG file like this (No border, the shape is filled with one color, no gradient, and background of the image is transparent) http://www.enchantedlearning.com/crafts/books/shapes/gifs/4.GIF

I'm thinking of using that polygon image as a background image and it will be changed (to another image with different color) when the user hovers on it.

But I also want the color of the background image to be customizable. So, I'm thinking if there is any possibility to draw the polygon instead of using image files so that the color will be customizable (I don't think it's a good idea to create one file for one color and so on so forth).

  • What is the best solution for this case? Using png or drawing it by css?
  • Is there a tool/website to convert my png to css code?
like image 523
Atthapon Junpun-eak Avatar asked Aug 11 '13 12:08

Atthapon Junpun-eak


3 Answers

  1. Make the white areas transparent (colour to alpha in GIMP)
  2. Convert the image to a data URI (it's optional but it will make your site load faster)
  3. Use the url in (2) as the background-image and use any background-color you want.
like image 52
Nabil Kadimi Avatar answered Sep 20 '22 14:09

Nabil Kadimi


Use this to convert an image: http://codepen.io/blazeeboy/pen/bCaLE I think it's much better to use converted images because browsers load them faster.

like image 34
Ugnius Malūkas Avatar answered Sep 20 '22 14:09

Ugnius Malūkas


I think CSS is the wrong thing to use for this. Yes, it is possible to create a lot of shapes using CSS, but there are limitations, and in any case, drawing shapes with CSS is a bit of a hack, even when it's just a simple triangle.

Rather than CSS, I would suggest SVG is the appropriate tools for this job.

SVG is a graphics format for vector graphics that can be embedded in a site, and can be created or altered via Javascript directly within the site. Changing the colour and shape of a simple polygon is about as easy as it gets with SVG.

The other advantage of using SVG is that because it's a vector graphic, it's scalable, so you could display it at any size.

The only down-side of SVG is that it isn't supported by old versions of IE (IE8 and earlier). However, these browsers do support an alternative language called VML, and several good Javascript libraries exist which will work with either, thus allowing you complete cross-browser compatibility. The one I'd recommend is Raphael.js.

So a tiny (and very easy) bit of Javascript code instead of a very messy bit of CSS. Seems like a winner to me.

like image 25
Spudley Avatar answered Sep 20 '22 14:09

Spudley