LESSON 5.3
CSS Gradients
Create beautiful color transitions without image files
Learn how to use CSS gradients to build smooth color transitions for backgrounds and design elements. No image files needed, and they load faster too.
๐ก The Concept
CSS gradients let you create smooth color transitions as a background image, without using an image file. That makes them fast to load and a great choice for backgrounds, buttons, and decorative sections. The screenshot below demonstrates what you can do with gradients. They are the presets on the Gradient Generator.

There are two main types: linear gradients (colors flow in a straight line) and radial gradients (colors radiate outward from a center point).

Linear Gradients
The basic syntax flows from top to bottom by default:
background-image: linear-gradient(green, pink);
You can control the direction using keywords or degrees:
/* Left to right */
background-image: linear-gradient(to right, green, pink);
/* Diagonal */
background-image: linear-gradient(to bottom right, navy, lightblue);
/* Using degrees */
background-image: linear-gradient(135deg, navy, lightblue);
Add more color stops to create richer gradients:
background-image: linear-gradient(to right, red, yellow, green);
You can also control exactly where each color begins using percentages:
background-image: linear-gradient(to right, navy 0%, royalblue 60%, lightblue 100%);
Radial Gradients
A radial gradient radiates outward from a center point:
background-image: radial-gradient(circle, yellow, orange);
Applying a Gradient to an Element
Use a gradient the same way you would use any background image:
.hero {
background-image: linear-gradient(to right, #1a1a2e, #16213e);
color: white;
padding: 60px 40px;
}
Gradients are much faster to load than image files, making them a great choice for page performance.

Exercise: Try It Yourself
Step 1. Open JSFiddle and paste this into the HTML panel:
<h1>Minneapolis Lakes</h1>
Then paste this into the CSS panel:
body {
height: 400px;
text-align: center;
}
Step 2. Open the Gradient Generator, design a linear gradient you like, and copy the CSS. Add it to the body rule in the CSS panel as the background-image. Run your Fiddle. (If the text is difficult to read, try changing it to white.)

Step 3. Back in the Gradient Generator, try changing the direction. Update the CSS in JSFiddle and see how it changes.
Step 4. Try a radial or conic gradient.
๐ Tools of the Trade: Use the Gradient Generator any time you need gradient CSS โ design it visually and copy the code. Bookmark the Tools of the Trade page for quick access to all course tools.
Frequently Asked Questions
background-color sets a single solid color. Gradients are technically background images โ even though there is no image file โ so they use the background-image property. You can also use the shorthand background property for either one.
No. The Gradient Generator on the Tools of the Trade page lets you design gradients visually and copy the ready-to-use CSS. Writing it by hand is useful to understand the syntax, but in practice most developers use a generator.
A color stop is a point in the gradient where a specific color is set. By default, two color stops are placed at 0% and 100%. You can add more stops and control their positions with percentages, for example: linear-gradient(to right, red 0%, yellow 40%, green 100%).
Yes. Use rgba() or transparent as a color stop. For example, linear-gradient(to right, rgba(0,0,0,0.8), transparent) creates a dark overlay that fades out โ a common technique for text legibility over photos.
A linear gradient flows in a straight line from one color to another. A radial gradient radiates outward from a center point, like a spotlight. A conic gradient sweeps the colors around a center point like the face of a clock. All three are created with CSS โ no image file needed.
Yes, with a small workaround. Apply the gradient as a background-image on the text element, then add -webkit-background-clip: text and -webkit-text-fill-color: transparent. This clips the gradient to the shape of the text characters.
Linear and radial gradients are supported in all modern browsers. Conic gradients have excellent support in current browsers as well. If you need to support very old browsers, a solid background-color fallback before your background-image line will ensure those users see something reasonable.
โ Key Takeaways
Gradients let you blend colors smoothly without using an image file. This lesson showed you how to create linear and radial gradients using only CSS. Next up is CSS Box Shadows, where you will learn to add depth and dimension to elements using the box-shadow property.
Google Ads
The costs of this website are partially offset by revenue from Google Ads.Tools of the Trade
HTML/CSS Lessons
-
-
-
1.3. HTML Tag Preview
-
-
-
2.2. The HTML Framework
-
-
2.4. HTML Headings
-
-
-
-
-
-
3.01. Introduction to CSS
-
3.02. How CSS Cascades
-
-
-
-
3.05. CSS Colors
-
3.06. RGBA Colors
-
-
3.08. Font and Text Properties
-
3.09. Classes and IDs
-
-
-
-
-
4.3. HTML Embedded Images
-
-
-
-
-
-
5.1. CSS Borders
-
5.2. CSS Border Radius
-
5.3. CSS Gradients
-
5.4. CSS Box Shadows
-
5.5. CSS Text Shadows
-
7.1. The Display Property
-
-
-
7.4. The Box Model
-
-
-
-
-
-
9.1. Website Tables
-
9.2. HTML Forms
-
-
9.4. Special Form Fields
-
9.5. Embedding Multimedia
-
-
9.7. Keyboard Shortcuts
-
10.1. What Is a Framework?
-
10.2. Font Awesome Icons
-
-
10.4. Bootstrap Grid System
-
-
10.6. Bootstrap Navigation
-
-
11.2. Purchasing a Domain Name
-
11.3. Website Hosting
-
11.4. Nameservers and DNS
-
11.5. FTP
-
11.6. GitHub Pages
