linear-gradient()

Creates a smooth colour transition along a straight line. It is used as an image, most often in background or background-image.

On this page

Example

#
CSS
.hero {
  background:
    linear-gradient(to top, rgb(12 12 16 / 0.95), transparent 70%),
    url("/collections/covers/kurs-html-css-od-podstaw.webp") center / cover;
}
Preview

The gradient darkens the bottom of the photo, so white text is legible no matter what is in the picture.

Definition and usage

#

The linear-gradient() function generates an image in which colours blend smoothly into one another along a straight line. Because it is an image, not a colour, you put it where CSS accepts images: in background-image or the background shorthand, and also in border-image and mask-image. It won’t work in background-color or color.

The first, optional argument is the direction. The words to right or to bottom right indicate the edge or corner the gradient runs towards, and an angle, for example 45deg, rotates the line: 0deg points up, 90deg to the right, and the default 180deg, that is to bottom, down.

After the direction you list the colours, at least two. Each can have a position: #7c3aed 20% means that pure purple starts at 20% of the gradient’s length. Two colours at the same position give a hard edge instead of a transition, which lets you draw stripes. The repeating-linear-gradient() version repeats the pattern indefinitely.

In background you can give several layers separated by commas, for example a semi-transparent gradient over a photo so that the text on it stays readable. The most reliable way to get a smooth change of a gradient is to animate its position (background-position) or a variable registered with @property.

Syntax

#
Syntax
linear-gradient([<angle> | to <side>]?, <color> <position>?, <color> <position>?, …)

Syntax variants

#
  • #7c3aed, #0ea5e9

    top to bottom

    Two colours with no direction. By default the gradient runs from top to bottom.
  • to right

    left to right

    A direction given in words: the gradient runs towards the right edge.
  • 45deg

    diagonal

    An angle rotates the gradient line. 45 degrees points towards the top right corner.
  • #7c3aed 30%, #0ea5e9 70%

    transition in the middle

    Pure colours up to 30% and from 70% of the length, with the transition only in between.
  • #7c3aed 50%, #0ea5e9 50%

    hard edge

    Two colours at the same position. Instead of a transition there is a sharp edge, like on a flag.
  • #f43f5e, #f59e0b, #10b981

    several colours

    There can be any number of colours. Without positions the browser spreads them out evenly.
  • repeating-linear-gradient()

    stripes

    A pattern repeated endlessly. Hard edges every 12 px give diagonal warning stripes.
  • linear-gradient() + url()

    darkened photo

    Two background layers: the gradient lies on the photo and darkens its bottom. On such a background it is easy to place legible text.

Syntax variants in practice

#

#7c3aed, #0ea5e9

Two colours with no direction. By default the gradient runs from top to bottom.
.demo {
  background: linear-gradient(#7c3aed, #0ea5e9);
}

to right

A direction given in words: the gradient runs towards the right edge.
.demo {
  background:
    linear-gradient(to right, #7c3aed, #0ea5e9);
}

45deg

An angle rotates the gradient line. 45 degrees points towards the top right corner.
.demo {
  background:
    linear-gradient(45deg, #7c3aed, #0ea5e9);
}

#7c3aed 30%, #0ea5e9 70%

Pure colours up to 30% and from 70% of the length, with the transition only in between.
.demo {
  background: linear-gradient(to right,
    #7c3aed 30%, #0ea5e9 70%);
}

#7c3aed 50%, #0ea5e9 50%

Two colours at the same position. Instead of a transition there is a sharp edge, like on a flag.
.demo {
  background: linear-gradient(to right,
    #7c3aed 50%, #0ea5e9 50%);
}

#f43f5e, #f59e0b, #10b981

There can be any number of colours. Without positions the browser spreads them out evenly.
.demo {
  background: linear-gradient(to right,
    #f43f5e, #f59e0b, #10b981);
}

repeating-linear-gradient()

A pattern repeated endlessly. Hard edges every 12 px give diagonal warning stripes.
.demo {
  background: repeating-linear-gradient(45deg,
    #f59e0b 0 12px, #18181b 12px 24px);
}

linear-gradient() + url()

Two background layers: the gradient lies on the photo and darkens its bottom. On such a background it is easy to place legible text.
.demo {
  background:
    linear-gradient(transparent, #0c0c10),
    url("/collections/covers/vibe-coding.webp")
      center / cover;
}

More examples

#
Text filled with a gradient
CSS
.title {
  background: linear-gradient(90deg, #a78bfa, #38bdf8);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
Preview

The background is clipped to the shape of the letters thanks to background-clip: text, and the transparent text colour reveals it.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • A gradient is an image, so put it in background or background-image. In background-color it will be ignored.
  • Between two colours you can put just a position, for example #7c3aed, 20%, #0ea5e9. This is a hint for where the transition should reach its midpoint.
  • Draw stripes and flags with hard edges: two colours at the same position, for example #000 50%, #fff 50%.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.