Can I convert an image to CSS3?

偶尔善良 提交于 2019-12-10 13:09:03

问题


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?

回答1:


  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.



回答2:


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.




回答3:


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.




回答4:


Maybe u could use this: https://javier.xyz/img2css/, the principle is to use box-shadow,it's fine if the picture is small, so u should consider performance



来源:https://stackoverflow.com/questions/18171889/can-i-convert-an-image-to-css3

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