On this page12 sections
Lecture 034: Gradients & Colour Keywords
| Category | 7: Advanced Styling |
| Lecture | 034 of 057 |
| Estimated video length | 12 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
Gradients blend colours smoothly, with no image file needed. CSS has three kinds: linear, radial and conic, plus repeating versions for stripes. This lecture also covers the special colour keywords
transparentandcurrentColor, and modern colour tricks.
Learning Objectives
- Write linear gradients with directions, angles and colour stops
- Write radial and conic gradients
- Make stripes with repeating gradients
- Use
transparent,currentColorandcolor-mix()
1. Linear Gradients
Example
<style>
div { height: 50px; margin-bottom: 8px; border-radius: 8px; color: white; padding: 6px 10px; font: 13px monospace; }
</style>
<div style="background: linear-gradient(#1971c2, #12b886);">top to bottom (default)</div>
<div style="background: linear-gradient(to right, #f76707, #e64980);">to right</div>
<div style="background: linear-gradient(to bottom right, #7048e8, #22b8cf);">to bottom right</div>
<div style="background: linear-gradient(135deg, #f59f00, #e03131);">135deg</div>
<div style="background: linear-gradient(to right, #e03131, #f59f00, #fab005, #2f9e44, #1971c2, #7048e8);">many colours</div>2. Colour Stops
A percentage after a colour says where it sits. Two stops at the same place make a hard edge.
Example
<style>
div { height: 50px; margin-bottom: 8px; border-radius: 8px; }
</style>
<div style="background: linear-gradient(to right, #1971c2 20%, #12b886 80%);"></div>
<div style="background: linear-gradient(to right, #2f9e44 50%, #e03131 50%);"></div>
<div style="background: linear-gradient(to right, #212529 33%, #f8f9fa 33% 66%, #2f9e44 66%);"></div>3. Transparency in Gradients
Example: fade a photo to dark at the bottom:
<div style="height: 180px; display: flex; align-items: flex-end; padding: 14px; color: white;
background: linear-gradient(to bottom, transparent 40%, rgb(0 0 0 / 0.8)), url('https://picsum.photos/id/1043/600/300') center / cover;">
<strong>Autumn in the north</strong>
</div>4. Radial Gradients
Example
<style>
div { display: inline-block; width: 140px; height: 100px; margin: 4px; border-radius: 8px; }
</style>
<div style="background: radial-gradient(#ffd43b, #f76707);"></div>
<div style="background: radial-gradient(circle, #ffd43b, #f76707);"></div>
<div style="background: radial-gradient(circle at top left, #74c0fc, #1864ab);"></div>
<div style="background: radial-gradient(circle, #fff 10%, #e64980 11% 40%, #fff 41%);"></div>5. Conic Gradients
Colours rotate around a centre point, like a pie chart.
Example
<style>
.pie { width: 120px; height: 120px; border-radius: 50%; display: inline-block; margin: 6px; }
</style>
<div class="pie" style="background: conic-gradient(#e03131, #f59f00, #2f9e44, #1971c2, #7048e8, #e03131);"></div>
<div class="pie" style="background: conic-gradient(#1971c2 0 40%, #2f9e44 40% 75%, #f59f00 75%);"></div>The second one is a pie chart: 40% blue, 35% green, 25% orange.
6. Repeating Gradients
Example
<style>
div { height: 60px; margin-bottom: 8px; border-radius: 8px; }
</style>
<div style="background: repeating-linear-gradient(45deg, #ffd43b 0 12px, #212529 12px 24px);"></div>
<div style="background: repeating-radial-gradient(circle, #74c0fc 0 8px, #e7f5ff 8px 16px);"></div>7. Colour Keywords and color-mix()
| Keyword | Meaning |
|---|---|
transparent | Fully see-through |
currentColor | The element's current text color |
inherit | The parent's value |
Example: borders and icons that follow the text colour:
<style>
.tag { color: #2b8a3e; border: 2px solid currentColor; padding: 4px 10px; border-radius: 999px; display: inline-block; margin: 4px; }
.tag.red { color: #c92a2a; }
.soft { background: color-mix(in srgb, #1971c2 20%, white); padding: 8px; }
</style>
<span class="tag">Green tag</span>
<span class="tag red">Red tag</span>
<p class="soft">color-mix makes a pale tint of blue without picking a new colour by hand.</p>Change color and the border follows. color-mix() blends two colours by a percentage.
Common Beginner Mistakes
1. Using background-color for gradients. Gradients are images, so use background or background-image.
2. Forgetting circle. Radial gradients are ellipses by default.
3. Too many bright colours. Two or three related colours usually look best.
Practice Questions
- Write a left-to-right gradient from yellow to orange.
- How do you make a hard edge between two colours?
- Write a radial gradient that's a circle.
- Which gradient type makes pie charts?
- Make diagonal stripes 10px wide.
- What does
currentColormean?
Practice Answers
background: linear-gradient(to right, yellow, orange);- Give both colours the same stop position, e.g.
red 50%, blue 50%. background: radial-gradient(circle, white, blue);conic-gradientbackground: repeating-linear-gradient(45deg, black 0 10px, white 10px 20px);- The element's current text colour.
Key Takeaways
linear-,radial-andconic-gradient()blend colours with no image.- Colour stops control position; equal stops make hard edges.
repeating-*gradients make stripes and rings.transparent,currentColorandcolor-mix()make colours flexible.