I would like to use gradients heavily in a new website I'm working on. I've been wondering if it would be better to implement the gradients in CSS3 or SVG.
Typically I only need multi-stop linear gradients so both meet my needs there.
I initially assumed this was best done in CSS3, but started to question my decision and would appreciate other opinions.
My thinking thus far is that SVG (as a CSS background) may be better because:
CSS3 may be better because:
An important consideration that I don't know the answer to is which performs better?
Is there a best practice for implementing background gradients?
SVG provides for two types of gradients: linear gradients and radial gradients. Once defined, gradients are then referenced using 'fill' or 'stroke' properties on a given graphics element to indicate that the given element shall be filled or stroked with the referenced gradient.
CSS defines three types of gradients: Linear Gradients (goes down/up/left/right/diagonally) Radial Gradients (defined by their center) Conic Gradients (rotated around a center point)
To use a gradient, we have to reference it from an object's fill or stroke attributes. This is done the same way you reference elements in CSS, using a url . In this case, the url is just a reference to our gradient, which I've given the creative ID, "Gradient". To attach it, set the fill to url(#Gradient) , and voila!
Code explanation:The x1, x2, y1,y2 attributes of the <linearGradient> tag define the start and end position of the gradient. The color range for a gradient can be composed of two or more colors. Each color is specified with a <stop> tag. The offset attribute is used to define where the gradient color begin and end.
According to a test performed by Lea Verou (I trust her work), CSS gradients are faster: http://lea.verou.me/2011/08/css-gradients-are-much-faster-than-svg/
UPDATE:
You could also consider using modernizr to serve up SVG to IE9 which supports SVG backgrounds but does not support CSS gradients.
In your CSS you would just do:
.cssgradients #someElement { /* Gradient background rule. */ }
.no-cssgradients #someElement { /* SVG background rule. */ }
More info here:
http://modernizr.com
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With