How To Work With Linear, Radial, And Repeating CSS Gradients | Van SEO Design |
How To Work With Linear, Radial, And Repeating CSS Gradients Posted: 10 Dec 2012 05:30 AM PST Any time you can replace a bitmapped image with code you should. The code is going to weigh less and be more maintainable. That’s not to say we shouldn’t use bitmapped images, but where appropriate go with the code instead. While they’ve been with us for a couple of years now, I’ve only recently started using css gradients in the sites I build. I’m not sure why I waited so long, but wait I did. Here and there I find them creeping into my work to style a button or to add some background texture. As a reminder to use them more I though I’d write about them here. I also created a simple demo so you can see css gradients in action. Browser SupportSince I’m saying we should use css gradients more, I should probably back that up with some data. Browser support for gradients is good. It’s not perfect, but it’s pretty good. The latest versions of all major browsers support gradients in some way. They all seem to be closing in on supporting a single standard, which is the W3c candidate recommendation from April of this year. As I’m testing only webkit browsers currently need to use a vendor prefix. As with a lot of css, IE is a potential stumbling block as there’s no support in IE9 and below. You can use IE filters to create gradients on older versions, but you’ll need to use more than css alone to make it happen. You might argue that suggests we should wait a bit longer, but there should never be a time where the inability to display a gradient breaks your design. If it does it says more about your design than about the gradient. Gradients are things of progressive enhancement and not usability requirements. They should never be essential to your design working. In other words if someone using IE8 can’t see your gradient, it’s not a big deal. How to Work with GradientsYou can use a gradient on any css property that accepts images. For example:
I think we sometimes see them as available only for backgrounds, but any property that can take a url to an image can also be set to display a css gradient. There are 4 types of gradients.
I trust you know the difference between a linear and radial gradient. The repeating versions of both work similarly, except they repeat. Gradient Lines and Color StopsGradients are drawn over a line that extends infinitely in both directions. They will be drawn into a box with the dimensions of the object they’re being added to, however, you can set the size of the gradient explicitly. The dimensions of the object are the default. For example setting background-size: 100px 200px means the gradient will be drawn into 100px by 200px box even if the element itself is larger or smaller. Color stops are points along the gradient line where we specify a solid color. In between color stops the color will transition between the two colors at each stop. This transition is the gradient we see.
For example if you set a color stop at at 20px from the left edge of an element, the color in the color stop will be solid from the edge to the 20px mark. At which point it will begin to transition to the color in the next color stop. If you only specify the color then the first stop will be at 0% and the last at 100%. Linear GradientsYou create a linear gradient by specifying a gradient line and then several color stops along that line.
The first argument above [ [ < angle> | to < side-or-corner> ] ,]? specifies a gradient line. The values can take an angle or keywords. When an angle is used 0deg points up, 90deg points to the right, and so on. With keywords you use “to” and then give a location, such as to top, to right, to top right. The default is 0deg or to top. The second argument < color-stop>[, < color-stop>]+ takes 2 or more colors and optionally a length for each.
The two lines above while similar, actually have different gradient lines. The first “left” says the line starts at the left. The second “to left” says the line ends at the left. Webkit browsers don’t seem to honor the “to” keyword at the moment, though it works fine with other browsers. We should still use vendor prefixes. The latest versions of all but webkit browsers no longer need them, but go a few versions back and they do. We should also provide a default fallback color and a fallback gradient image. Complete code for setting a linear gradient looks like the following
Note: Webkit previously used a different syntax (-webkit-gradient), which I didn’t include above. You might want to include it in the stack just above the current webkit line, depending on how many versions back you need to support.
You aren’t limited to only using two colors. You could set a linear gradient with more than two color stops like the following:
However, I’ve noticed that additional color stops beyond two often don’t transition as nicely. There’s usually a point (the color stop) where you see a solid band of a color. Radial GradientsRadial gradients emerge from a single point and you create them by setting a shape, size, and position.
When you have an ellipse you set multiple sizes.
The first set of values are the horizontal and the second set are the vertical.
As with the “to” keyword, I haven’t had any luck getting webkit browsers to recognize the “at” keyword. Otherwise the css above works across browsers.
The code above should produce a radial gradient you see in the image above. It transitions from yellow to orange to red and its shape should be a circle with a center point that matches the center of the element. Repeating gradientsWe have 2 types of repeating gradients
They take the same values as above, but the color stops are repeated indefinitely in both directions. Here’s an example of a repeating-linear gradient.
The pattern (seen in the image above) will repeat along the entire gradient line. With even minimal variation of the above values you can create some very different and interesting patterns. One oddity I’ve noticed is that where Firefox and Opera treat the angle above as 30 degrees from the horizontal, webkit browsers treat it as 30 degrees from the vertical. Compare how gradient 5 in the demo looks in a webkit and non-webkit browser to see the difference. Here’s an example of a radial-gradient.
Above I stuck with 2 colors, but you can certainly add more and again with even slight variation you can achieve a lot different and interesting results. SummaryIf you aren’t using css gradients yet, now is as good a time as any to start. This post is as much to remind me as it is to remind you. Once again check out the simple demo I created and even better experiment on your own. Browser support is more than adequate and we aren’t dealing with design critical elements in gradients. And if you absolutely must have a gradient you can always set a fallback image so there really is no excuse. Gradients are pretty easy to work with. The biggest hassles at the moment are probably the vendor prefixes and slight differences in how browsers honor the standard, but we should all be used to these kind of issues by now. Browsers are moving toward adopting the latest spec and only webkit browsers still require a vendor prefix in their latest version so hopefully this hassle won’t exist for too much longer. Do you use css gradients? If so how have they worked for you? If not, what’s keeping you from using them? The post How To Work With Linear, Radial, And Repeating CSS Gradients appeared first on Vanseo Design. |
You are subscribed to email updates from Vanseo Design » Blog To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 20 West Kinzie, Chicago IL USA 60610 |