On this page12 sections
  1. Learning Objectives
  2. 1. Linear Gradients
  3. 2. Colour Stops
  4. 3. Transparency in Gradients
  5. 4. Radial Gradients
  6. 5. Conic Gradients
  7. 6. Repeating Gradients
  8. 7. Colour Keywords and color-mix()
  9. Common Beginner Mistakes
  10. Practice Questions
  11. Practice Answers
  12. Key Takeaways

Lecture 034: Gradients & Colour Keywords

Category7: Advanced Styling
Lecture034 of 057
Estimated video length12 min
TargetCSS3 · any modern browser
LevelIntermediate

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 transparent and currentColor, 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, currentColor and color-mix()

1. Linear Gradients

Example

html
<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

html
<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:

html
<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

html
<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

html
<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

html
<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()

KeywordMeaning
transparentFully see-through
currentColorThe element's current text color
inheritThe parent's value

Example: borders and icons that follow the text colour:

html
<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

  1. Write a left-to-right gradient from yellow to orange.
  2. How do you make a hard edge between two colours?
  3. Write a radial gradient that's a circle.
  4. Which gradient type makes pie charts?
  5. Make diagonal stripes 10px wide.
  6. What does currentColor mean?

Practice Answers

  1. background: linear-gradient(to right, yellow, orange);
  2. Give both colours the same stop position, e.g. red 50%, blue 50%.
  3. background: radial-gradient(circle, white, blue);
  4. conic-gradient
  5. background: repeating-linear-gradient(45deg, black 0 10px, white 10px 20px);
  6. The element's current text colour.

Key Takeaways

  • linear-, radial- and conic-gradient() blend colours with no image.
  • Colour stops control position; equal stops make hard edges.
  • repeating-* gradients make stripes and rings.
  • transparent, currentColor and color-mix() make colours flexible.