Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choosing SVG or CSS3 for gradients

Tags:

css

svg

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:

  • It works in IE9
  • My CSS is cleaner w/o browser prefixes
  • Easy reuse of gradient

CSS3 may be better because:

  • Seems like a job for CSS
  • Less downloads for the client
  • Everything is in one place

An important consideration that I don't know the answer to is which performs better?

Is there a best practice for implementing background gradients?

like image 694
C.J. Avatar asked Feb 12 '12 08:02

C.J.


People also ask

Can SVG handle 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.

Can you do gradients in CSS?

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)

How do you SVG a gradient?

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!

How do you make a SVG gradient in CSS?

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.


1 Answers

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

like image 139
Jim Jeffers Avatar answered Oct 09 '22 21:10

Jim Jeffers