TExcelTAD
Home › CSS › CSS Gradients

CSS Gradients

A gradient is a smooth blend between two or more colours, generated by the browser. Gradients count as images, so you use them wherever an image fits: background-image, border-image, mask-image and list-style-image. They need no download, scale to any size and are easy to tweak.

Gradient functions

Function Shape
linear-gradient() Colours change along a straight line
radial-gradient() Colours spread out from a centre point
conic-gradient() Colours rotate around a centre point, like a colour wheel
repeating-linear-gradient() etc. Repeat the pattern to make stripes and rings

Linear gradients

The first argument sets the direction: a keyword such as to right or to bottom right, or an angle. 0deg points up and angles turn clockwise, so 90deg points right. Without a direction the gradient runs top to bottom.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; padding: 6px; }
  div { height: 90px; border-radius: 10px; color: white; font-weight: 600; display: grid; place-items: center; text-shadow: 0 1px 2px rgba(0,0,0,.5); }
  .a { background: linear-gradient(#ff512f, #dd2476); }
  .b { background: linear-gradient(to right, #00b4db, #0083b0); }
  .c { background: linear-gradient(to bottom right, #f7971e, #ffd200); }
  .d { background: linear-gradient(135deg, #667eea, #764ba2); }
  .e { background: linear-gradient(90deg, red, orange, yellow, green, blue, indigo, violet); }
  .f { background: linear-gradient(to right, rgba(0,0,0,.9), transparent), #26a69a; }
</style>
</head>
<body>
  <div class="a">default</div><div class="b">to right</div><div class="c">to bottom right</div>
  <div class="d">135deg</div><div class="e">7 colours</div><div class="f">fade to transparent</div>
</body>
</html>

Colour stops

Each colour can carry a position. Stops let you control where colours sit, and two stops at the same position make a hard edge with no blending.

Syntax Effect
red, blue evenly spread
red 20%, blue 80% solid red until 20%, blend to 80%, then solid blue
red 50%, blue 50% sharp split at the middle
red 0 33%, white 33% 66%, blue 66% three solid bands (two positions per colour)
red, 30%, blue a colour hint: move the blend midpoint to 30%
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; display: grid; gap: 8px; padding: 6px; }
  div { height: 46px; border-radius: 8px; display: flex; align-items: center; padding-left: 10px; font-family: monospace; color: #111; }
  .a { background: linear-gradient(90deg, #ff8a80, #82b1ff); }
  .b { background: linear-gradient(90deg, #ff8a80 30%, #82b1ff 70%); }
  .c { background: linear-gradient(90deg, #ff8a80 50%, #82b1ff 50%); }
  .d { background: linear-gradient(90deg, #ef5350 0 33%, #fafafa 33% 66%, #42a5f5 66%); }
</style>
</head>
<body>
  <div class="a">red, blue</div>
  <div class="b">red 30%, blue 70%</div>
  <div class="c">red 50%, blue 50% (hard edge)</div>
  <div class="d">three bands</div>
</body>
</html>

Radial and conic gradients

radial-gradient(shape size at position, stops) grows outward. The shape is circle or ellipse (the default). conic-gradient(from angle at position, stops) sweeps around a point, which makes it handy for pie charts and colour wheels.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; display: flex; flex-wrap: wrap; gap: 16px; padding: 10px; }
  div { width: 120px; height: 120px; }
  .r1 { background: radial-gradient(#fff176, #ff7043); border-radius: 12px; }
  .r2 { background: radial-gradient(circle at 30% 30%, white, #42a5f5 40%, #0d47a1); border-radius: 50%; }
  .pie { border-radius: 50%; background: conic-gradient(#ef5350 0 40%, #ffca28 40% 70%, #66bb6a 70% 100%); }
  .wheel { border-radius: 50%; background: conic-gradient(red, yellow, lime, aqua, blue, magenta, red); }
</style>
</head>
<body>
  <div class="r1"></div>
  <div class="r2"></div>
  <div class="pie"></div>
  <div class="wheel"></div>
</body>
</html>

Tip: The pie chart above takes one line of CSS. Turn it into a donut by adding a white circle on top, for example with a radial-gradient layer or a pseudo-element.

Repeating gradients and patterns

Repeating gradients tile their stops over and over. Combine several gradient layers, separated by commas, and you can build stripes, checkerboards and grids without any image files.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { display: flex; flex-wrap: wrap; gap: 14px; padding: 10px; }
  div { width: 130px; height: 110px; border-radius: 10px; }
  .stripes { background: repeating-linear-gradient(45deg, #ffeb3b 0 12px, #212121 12px 24px); }
  .rings { background: repeating-radial-gradient(circle, #7e57c2 0 8px, #d1c4e9 8px 16px); }
  .checker { background: conic-gradient(#333 25%, #eee 0 50%, #333 0 75%, #eee 0) 0 0 / 30px 30px; }
  .grid { background-color: #e3f2fd;
          background-image: linear-gradient(#90caf9 1px, transparent 1px), linear-gradient(90deg, #90caf9 1px, transparent 1px);
          background-size: 16px 16px; }
</style>
</head>
<body>
  <div class="stripes"></div><div class="rings"></div><div class="checker"></div><div class="grid"></div>
</body>
</html>

Note: Because gradients are images, you set them with background or background-image, not background-color. You can still layer a gradient over a solid fallback colour.

Test yourself

Which direction does linear-gradient(90deg, red, blue) run?

  1. Top to bottom
  2. Bottom to top
  3. Left to right
  4. Right to left

How do you make a hard edge between two colours?

  1. Use repeating-linear-gradient
  2. Give both stops the same position, like red 50%, blue 50%
  3. Add sharp: true
  4. Use conic-gradient only

Which gradient function is best for a pie chart?

  1. linear-gradient
  2. radial-gradient
  3. conic-gradient
  4. repeating-linear-gradient