CSS2SVG is a JavaScript-based utility for converting CSS3 Gradients to SVG images to help maintain a website's appearance in browsers that do not support CSS3 gradients but do support SVG graphics (such as Internet Explorer 9).
The script is pure JS and does not require any libraries (though you could certainly fork a version that does if you want). It is also presented in a single HTML page for easy usage and editing.
- For offline usage, download CSS2SVG.htm from the file listing above; that's the only file you need!
- Copy and paste any gradients into the input box at the top of the script page.
linear-gradient
and nowrepeating-linear-gradient
(new in Version 1.5.0) are supported, with or without prefixes. - If you're converting many gradients, then you may want to mark the "Clear Input on Focus" checkbox to expedite pasting/typing new gradients.
- Declare a size. Typically for tiled background images, a percentage is best because it will stretch to fill the element's background. However, if you know the gradient is to fill the background of an element with a specific size, you can leave it as pixel-based dimensions. For accurate rendering of angles, pixels are recommended.
- Click "Convert".
- Copy and paste the SVG output into any plain text editor and save it as an .svg file.
- That's all you have to do! You'll also see that the "Preview" section takes the same code in your output and inserts an actual SVG file into the page for you to see how the image will look. If you're not satisfied with the appearance, or you just want to change it, then edit the output and click "Update" to refresh the preview. Please note the browser compatibility list; older browsers may not support the SVG preview/editing functionality.
Check out some sample gradients to work with to demonstrate the script's functionality.
Check out advanced usage documentation here.
Check out the embedded comments in the script for further explanations of the steps it carries out. In short, the script
- parses input gradient(s) to separate angles or origin/destination keywords and individual color stops;
- calculates SVG vector coordinates based on the angles or keywords;
- generates color stops with your specified colors and offsets (or calculates missing offsets);
- and finally builds an SVG file with the complete gradient(s).
If you want to contribute to this project, then we recommend you play around with the code in the JSFiddle or offline, and when you know your changes work, then commit them here to GitHub. The converter is intentionally presented as a single HTML page with all the CSS and scripting in it, so, if you want to download it for editing or offline use, you can be assured you have ALL the necessary code. From JSFiddle, you should open the "full screen result" of your fiddling (URL available under the "Share" button), and when you look at the source code, make sure you are viewing it for the IFRAME in which the result is shown (you may need to right+click > show only this frame). JSFiddle also adds a dummy script and CSS link to the top of the document which do not need to be included in the git commit and please change the title of the document:
<title> - jsFiddle demo</title>
<script type='text/javascript' src='/js/lib/dummy.js'></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
- Full Support: Chrome 7+, IE9+, FF4+, Safari 5.1+, Opera 11.6+
- SVG Preview/Editing Unavailable: Chrome 6-, IE8-, FF3.6-, Safari 5.0-, Opera 11.5-
- Bug fix: Following the 1.5.3 update, gradients with any explicitly defined color-stops other than
calc
expressions were rarely if at all converting properly.
- Added support for CSS
calc()
expressions, e.g.calc(100% - 75px)
, as gradient stop values. Nestedcalc()
expressions are not supported, however. - Bug fix / improvement in error-handling: The script's color validation check was not always throwing an error when it should do so.
- Bug fix: Use of ID selectors in Batch Mode would sometimes cause the script to improperly throw an error message.
- Bug fix: Logic for calculating
repeating-linear-gradient
color-stops was not being triggered properly. - Bug fix: Error message was being thrown unnecessarily for gradients with no angle or origin/destination keywords defined.
- Significantly improved gradient stops calculation. Now, any gradient with any number of missing stops should get converted properly.
- Added support for absolute units in gradient color-stops: you may now use
px
,cm
,mm
,in
,pt
, andpc
. - Added experimental support for
repeating-linear-gradient
. The script produces output with correct angles (arguably the most important part); however, eagle-eyed observers will notice discrepancies from CSS rendering—this is especially the case when the first color stop is not equal to zero. Future updates may correct such discrepancies if possible. - Bug fix: Due to the way the script previously handled missing color-stops, some gradients with at least three stops, where the initial color-stop(s) is/are defined and the last two left off, were not rendering properly.
- Bug fix: A temporary
div
variable used in converting color names to hex was not being cleaned up properly after execution, leading to accumulation of unneeded divs in the page DOM with multiple conversions. - Bug fix: Gradients with no angle or origin/destination keywords defined, with a color name at the first color-stop would not convert properly when the first color-stop is explicitly labeled (e.g.,
linear-gradient(transparent 0%, ...)
were not working). - Bug fix / improvement in error-handling:
hsl/a
colors with hues >360 degrees were not converting to the proper color. - Bug fix / improvement in error-handling: The script was silently allowing hex colors not in proper three- or six-digit notation, instead of throwing an error in the color validation check.
- Added further enhancements to the page's usability. Now, you can perform a conversion or update by pressing shift+enter inside the input/output
textarea
s, or pressing enter when the radio buttons, checkboxes, or dimension inputs have focus. - Despite the significant updates as listed above, the page code is now about 5% smaller compared to Version 1.2.0, thanks to various micro-optimizations.
- Still no radial-gradient support yet, but Anthony plans to figure that out eventually!
- —Fixed in 1.2.0—
For some reason, FF3.6 and below (and Opera 10 and below) can only do one conversion per page load/refresh. Those browsers are ancient, though. So, any fixes for them will be of lowest priority for us, but you're welcome to contribute a fix! This issue is odd, though. Even IE7 can do multiple conversions! Heck, so can IE6! - —Fixed in 0.9.6—
Older browsers that don't fully supportrgba
orhsl/a
will trip on the included color validation loop, if input gradient(s) has/havergba
and/orhsl/a
.
Anthony intends to continue updating this script as time allows. Here's what he plans to implement (subject to change):
- Radial gradient support.
canvas
output support, to facilitate converting SVG output into bitmap images (e.g., PNG) that IE8- and other ancient browsers lacking SVG support can read.- (Maybe?) A tabbed interface to group the SVG code output, data URI output, canvas output, and SVG preview into one central location.
Original Project Page: http://www.kmhcreative.com/labs/css3-2-svg/
I’m not very good with math so I would never have been able to figure out how to build this without the following:
http://www.codeproject.com/KB/books/learnsvgchapter07.aspx
HUGE thanks to Anthony Martinez for his additions to this! http://www.linkedin.com/in/canthonymartinez/
The script is licensed under a Creative Commons Attribution-ShareAlike 3.0 license.