CSS Gradients: Dynamic Images from Code

Think of CSS gradients as dynamic images generated by the browser, creating color transitions without a separate file. Use them for backgrounds and buttons. The default direction is top-to-bottom, but a 0deg angle is counterintuitively bottom-to-top.
Why it exists
Before CSS gradients, creating a smooth color transition required designing a raster image (like a PNG or JPG), exporting it, and loading it with the page. This added HTTP requests, increased page weight, and resulted in pixelation when resized or viewed on high-DPI screens. CSS gradients solve this by generating the effect directly in the browser from code.
The mental model
Treat a CSS gradient as a dynamically generated <image>, not just a background color. It's a set of instructions for the browser on how to draw a picture made of color transitions. Because it's an image, you can use it anywhere an image is valid, like background-image, border-image, or list-style-image.
How it works
You define a gradient using one of three main functions: linear-gradient(), radial-gradient(), or conic-gradient(). At a minimum, you provide two "color stops"—the colors you want to transition between. For example, linear-gradient(blue, pink) creates a vertical gradient from blue to pink. You can add more color stops and specify their positions. For linear gradients, you can also control the direction using keywords (like to right or to bottom right) or a specific angle (like 45deg).
When to use it
Use gradients for backgrounds on pages, containers, or buttons to add depth without the performance cost of an image file. They are excellent for hover states, creating subtle visual feedback. They are also perfect for creating simple patterns when combined with background-size and background-repeat.
When not to use it
Avoid using gradients for complex, photorealistic textures where a dedicated image file would be more appropriate. Overusing gradients or using too many jarring color combinations can make a UI feel dated or difficult to read. For simple, solid color backgrounds, just use the background-color property.
One canonical example
A common use is a subtle button background. A simple linear gradient can make a flat button feel more tangible. For example: background: linear-gradient(to bottom, #4A90E2, #357ABD);. This creates a vertical gradient from a lighter blue to a slightly darker blue, giving the button a slight 3D effect without any images.
Interview question
What is the primary advantage of using CSS gradients for visual effects like button backgrounds over traditional image files?
- a.They reduce HTTP requests and maintain quality across various screen resolutions.Correct
- b.They provide better browser compatibility across older web browsers.
- c.They are exclusively used for background-image properties, simplifying styling.
- d.They allow for more intricate and detailed visual patterns.
Why? this is the answer
The card states CSS gradients solve issues like added HTTP requests, increased page weight, and pixelation on high-DPI screens by generating effects directly in the browser. Option D is incorrect because the card advises against using gradients for complex, photorealistic textures.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
- #css
- #design
- #gradients
- #performance
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on css — each one lists the topics its interview covers.
See open roles