CSS / 5 MIN READ
Background gradient
Learn how to create linear gradients in CSS.
From the original Fervor library. Examples may use older package versions.
🎨 How to Create a Linear Gradient in CSS (Step-by-Step Tutorial)
A linear gradient in CSS is a smooth transition between two or more colors along a straight line. It’s a great way to make your website look more modern and visually appealing without using images. Let’s break it down step by step! 🚀
1️⃣ Basic Syntax of a Linear Gradient
CSS provides the linear-gradient() function, which is used as a background.
background: linear-gradient(direction, color1, color2, ...);
direction– Defines the angle or direction of the gradient (optional).color1, color2, ...– The colors you want to blend.
2️⃣ Creating a Simple Top-to-Bottom Gradient
By default, gradients go from top to bottom if you don’t specify a direction.
div {
width: 300px;
height: 200px;
background: linear-gradient(red, blue);
}
✅ This smoothly blends from red (top) to blue (bottom).
3️⃣ Changing the Gradient Direction
You can control the direction in two ways: using keywords or angles.
Using Keywords:
background: linear-gradient(to right, red, blue);
🎯 to right makes the gradient go left to right (red → blue).
Other keyword directions:
to leftto topto bottomto top right(orto bottom left, etc.)
Using Angles:
background: linear-gradient(45deg, red, blue);
🎯 45deg starts from the top-left moving diagonally.
Some common angles:
0deg→ Left to Right90deg→ Top to Bottom180deg→ Right to Left270deg→ Bottom to Top
4️⃣ Using Multiple Colors
You can add more colors for cool effects:
background: linear-gradient(to right, red, yellow, green, blue);
🌈 This creates a smooth transition between red → yellow → green → blue.
5️⃣ Adding Color Stops
You can control where colors appear using percentages or pixel values.
background: linear-gradient(to right, red 20%, yellow 50%, blue 80%);
✅ Red covers 20%, yellow starts at 50%, and blue starts at 80%, making the transition more controlled.
6️⃣ Repeating the Gradient
Want a striped effect? Use repeating-linear-gradient():
background: repeating-linear-gradient(45deg, red, yellow 10%, blue 20%);
🎨 This creates repeating stripes of red, yellow, and blue every 10-20%.
7️⃣ Applying It to a Full Page
You can make a full-page gradient by applying it to the body:
body {
height: 100vh;
background: linear-gradient(to bottom, #ff7e5f, #feb47b);
}
📌 100vh ensures it covers the entire screen height.
🎬 Final Thoughts
CSS gradients are a powerful tool for creating beautiful backgrounds without images. Play around with different directions, colors, and stops to create your own unique styles! 🎉
🔹 Try it yourself! Experiment with different gradients in CSS Gradient Generator for live previews.
🎨 Cool Stuff You Can Do with CSS Gradients! 🔥
Now that you’ve mastered the basics of linear gradients, let’s take things up a notch. Here are some awesome tricks you can do with CSS gradients to make your website look sleek and modern! 🚀
1️⃣ Gradient Text (Fancy Headings)
Make your text shine with a gradient color effect!
h1 {
font-size: 50px;
font-weight: bold;
background: linear-gradient(90deg, #ff7e5f, #feb47b);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
✨ This makes the text transparent while keeping the gradient as the fill color.
🔹 Pro Tip: Try adding multiple colors for a rainbow text effect! 🌈
2️⃣ Gradient Borders (Stylish Outlines)
Want a cool gradient border? You can’t apply gradients directly to border, but here’s a trick using border-image:
div {
width: 200px;
height: 100px;
border: 5px solid;
border-image: linear-gradient(to right, #ff7e5f, #feb47b);
border-image-slice: 1;
}
🔥 Now your box has a gradient border instead of a boring solid color!
3️⃣ Gradient Buttons (Sleek & Modern UI)
Make your buttons stand out with a gradient background!
button {
padding: 10px 20px;
font-size: 18px;
color: white;
border: none;
border-radius: 5px;
background: linear-gradient(45deg, #ff416c, #ff4b2b);
cursor: pointer;
transition: 0.3s;
}
button:hover {
background: linear-gradient(45deg, #ff4b2b, #ff416c);
}
🛑 This makes your button change gradient direction on hover for a cool effect!
4️⃣ Gradient Shadows (Glowy Vibes)
Make elements glow with a gradient box-shadow!
.box {
width: 200px;
height: 100px;
background: #222;
box-shadow: 0 0 20px 5px rgba(255, 0, 150, 0.5),
0 0 40px 10px rgba(0, 255, 255, 0.5);
}
💡 Try changing the shadow colors to match your website’s theme!
5️⃣ Animated Gradient Background (Trippy Effect)
Want an animated gradient background? Here’s how:
@keyframes gradientMove {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
body {
height: 100vh;
background: linear-gradient(270deg, #ff7e5f, #feb47b, #86a8e7, #7f7fd5);
background-size: 400% 400%;
animation: gradientMove 6s ease infinite;
}
🎆 This makes the gradient move and shift over time, creating a dynamic background effect!
6️⃣ Gradient Overlays (Cool Image Effects)
Want to overlay a gradient on an image for a cinematic look?
.overlay {
width: 100%;
height: 300px;
background: linear-gradient(to top, rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0));
position: absolute;
bottom: 0;
}
📸 This is great for text overlays on hero images!
7️⃣ Gradient Stripes (Funky Patterns)
Want diagonal gradient stripes? Use repeating-linear-gradient():
.stripes {
width: 300px;
height: 200px;
background: repeating-linear-gradient(
45deg,
#ff7e5f 0%,
#ff7e5f 10%,
#feb47b 10%,
#feb47b 20%
);
}
🟠⚪ This creates a striped pattern with gradients! Try changing colors for a retro 80s feel.
8️⃣ Gradient Progress Bar (Cool Loading Effect)
Want a fancy gradient progress bar?
.progress {
width: 100%;
height: 10px;
background: linear-gradient(to right, #4facfe, #00f2fe);
border-radius: 5px;
animation: load 3s linear infinite;
}
@keyframes load {
0% { width: 0%; }
100% { width: 100%; }
}
💾 Great for modern loading indicators!
🎬 Final Thoughts
CSS gradients aren’t just for backgrounds—you can use them for text, borders, buttons, shadows, and even animations!
🔥 Now it’s your turn! Try combining these effects to create unique designs. Got any cool gradient tricks? Share them with me! 😃🚀