Gradient fundamentals

CSS lets you control every part of how one color fades into another — direction, shape, the colors themselves and how they transition. The language defines three gradient types: linear, radial, and conic.

/* Basic linear CSS gradient examples */
background-image: linear-gradient(#ff8a00, #e52e71);
background-image: linear-gradient(to right, violet, darkred, purple);
background-image: linear-gradient(40deg, rgb(255 0 0) 60%, orange);

/* Basic radial CSS gradient examples */
background-image: radial-gradient(#ff8a00, #e52e71);
background-image: radial-gradient(circle at top right, #ff8a00, red, #e52e71);

/* Basic conic CSS gradient examples */
background-image: conic-gradient(#ff8a00, #e52e71);
background-image: conic-gradient(red 50deg, yellow 100deg, lime 200deg, aqua, blue, magenta, red);

Linear gradients

linear-gradient() is the most common type. The colors flow in a single direction — left-to-right, top-to-bottom, or at any angle you choose.

The function is declared on either the background shorthand or background-image property:

linear-gradient(
  [ <angle> | to <side-or-corner> ]? ,
  <color-stop-list>
)
<side-or-corner> = [to left | to right] || [to top | to bottom]

When no angle is passed, CSS assumes to bottom. In the example below, #ff8a00 transitions into #e52e71; the same effect can be written in more than one way.

.gradient {
  background-image: linear-gradient(#ff8a00, #e52e71);
}
/* Explicitly declare the direction */
background-image: linear-gradient(to bottom, #ff8a00, #e52e71);

/* Explicitly declare the angle, in degrees */
background-image: linear-gradient(180deg, #ff8a00, #e52e71);

To run the gradient from left to right, add a parameter at the start of the function beginning with the word to, which signals direction:

.gradient {
  background-image:
    linear-gradient(
      to right,
      #ff8a00, #e52e71
    );
}

The same syntax works for corners. to top right starts the axis at the bottom-left corner and ends it at the top-right:

.gradient {
  background-image:
    linear-gradient(
      to top right,
      #ff8a00, #e52e71
    );
}

On a perfect square that gradient's angle would be 45deg, but on a non-square element it is not. To pin it to exactly 45deg, declare the angle directly and drop to:

.gradient {
  background-image:
    linear-gradient(
      45deg, 
      #ff8a00, #e52e71
    );
}

Two colors is only a starting point — the function accepts as many comma-separated colors as you want:

.gradient {
  background-image:
    linear-gradient(
      to right, 
      red, 
      #f06d06, 
      rgb(255, 255, 0), 
      green
    );
}

Browser support data (from Caniuse; a number means support at that version and up):

Desktop

ChromeFirefoxIEEdgeSafari
10*36101215.4

Mobile / Tablet

Android ChromeAndroid FirefoxAndroidiOS Safari
1571604*15.4

Radial gradients

radial-gradient() begins at a single point and emanates outward. Because it simulates a light source, which isn't always straight, the color transitions tend to feel more natural. It too is declared on background or background-image — gradients are, after all, CSS-created images that would otherwise be authored in image editing software.

radial-gradient(
  [ <ending-shape> || <size> ]? [ at <position> ]? ,
  <color-stop-list>
);

Its parameters break down into four values:

  • shape — the shape the transition follows. Only circular forms are possible: circle or ellipse. If omitted, CSS measures the element's side lengths: a perfect square matches circle, a rectangle matches ellipse.
  • size — controls where the ending shape sits relative to the center, by name or by exact length.
    • closest-side: the gradient ends at the nearest side; if two sides tie, it is distributed evenly.
    • farthest-side (default): ends at the side farthest from the center.
    • closest-corner: ends at the nearest corner.
    • farthest-corner: ends at the farthest corner.
    • radius: a numeric circle radius. It must be positive pixels or relative units — no negative units and no percentages, since negative circles don't exist and percentages would be relative to unpredictable surrounding values. A second numeric value may be supplied to size an ellipse explicitly, with the same restriction on negatives; there, percentages are allowed because size is relative to the gradient box rather than the element.
  • position — behaves like background-position: top, right, left and center work, as do combinations such as top center, and exact numeric or percentage values relative to the element's bounding box. Default is center center.
  • color-stop — the color values themselves; hex, named, RGB and HSL are all accepted.

With no parameters declared, everything defaults to a gradient centered on the element that transitions evenly between the declared colors:

.gradient {
  background-image:
    radial-gradient(
      #ff8a00,
      #e52e71
    );
}

In that example the gradient takes on ellipse because the element isn't square; a square would get circle instead. Declaring the shape explicitly overrides the guess:

.gradient {
  background-image:
    radial-gradient(
      circle,
      #ff8a00,
      #e52e71
    );
}

The example fades all the way to the ending color at the far edge. Declaring the size value makes the fade stop at the closest side instead:

.gradient {
  background-image:
    radial-gradient(
      circle closest-side,
      #ff8a00,
      #e52e71
    );
}

That value can be closest-corner, closest-side, farthest-corner or farthest-side — in effect, "fade from the center to this side, and fill the rest accordingly." The starting point doesn't have to be the center either; at in the first parameter places it anywhere:

.gradient {
  background-image:
    radial-gradient(
      circle at top right,
      #ff8a00,
      #e52e71
    );
}

Desktop

ChromeFirefoxIEEdgeSafari
10*36101215.4

Mobile / Tablet

Android ChromeAndroid FirefoxAndroidiOS Safari
1571604*15.4

Conic gradients

conic-gradient() resembles a radial gradient — both are circular and use the element's center as the source for color stops — but conic stops are placed around the circle rather than emerging from its center. The name comes from the cone shape seen from above, provided a distinct angle is given and the color contrast is strong enough to show it.

conic-gradient(
  [ from <angle> ]? [ at <position> ]?,
  <angular-color-stop-list>
)

At its simplest:

.gradient {
  background-image:
    conic-gradient(
      #ff8a00, #e52e71
    );
}

Here the location defaults to 50% 50% and the colors are distributed evenly. The same result can be written in several valid ways:

.gradient {
  /* Original example */
  background-image: conic-gradient(#ff8a00, #e52e71);
  /* Explicitly state the location center point */
  background-image: conic-gradient(at 50% 50%, #ff8a00, #e52e71);
  /* Explicitly state the angle of the start color */
  background-image: conic-gradient(from 0deg, #ff8a00, #e52e71);
  /* Explicitly state the angle of the start color and center point location */
  background-image: conic-gradient(from 0deg at center, #ff8a00, #e52e71);
  /* Explicitly state the angles of both colors as percentages instead of degrees */
  background-image: conic-gradient(#ff8a00 0%, #ff8a00, #e52e71);
  /* Explicitly state the angle of the starting color in degrees and the ending color by a full turn of the circle */
  background-image: conic-gradient(#ff8a00 0deg, #e52e71 1turn);
}

Without angles, the gradient divides evenly from 0deg to 360deg, producing a "hard stop" where the colors meet at each end. Adding a third value smooths the transition and produces the cone perspective:

.gradient {
  background-image:
    conic-gradient(
      #ff8a00,
      #e52e71,
      #ff8a00
    );
}

That makes conic gradients handy for color-picker-style patterns or a beach-ball spinner:

.conic-gradient {
  background-image:
    conic-gradient(
      red,
      yellow,
      lime,
      aqua,
      blue,
      magenta,
      red
    );
}

Hard stops between three colors also turn it into a simple pie chart:

.gradient {
  background-image:
    conic-gradient(
      lime 40%,
      yellow 0 70%,
      red 0
    );
}

Desktop

ChromeFirefoxIEEdgeSafari
6983No7912.1

Mobile / Tablet

Android ChromeAndroid FirefoxAndroidiOS Safari
15716015712.2-12.5

Repeating gradients

Repeating gradients take what color stops can already achieve on linear-gradient() and radial-gradient() and bake it in: the pattern repeats infinitely. Three notations are supported in the official specification and one is in the current working draft; each shares the syntax of its base type, so repeating-linear-gradient() follows linear-gradient().

Repeating gradientRelated notationExample
repeating-linear-gradientlinear-graadientrepeating-linear-gradient(45deg, #ff8a00, #ff8a00 10px, #e52e71 10px, #e52e71 20px);
repeating-radial-gradientradial-gradientrepeating-radial-gradient(#ff8a00 0 8%, #e52e71 8% 16%);
repeating-conic-gradientconic-gradientrepeating-conic-gradient(from 0deg, #ff8a00 0deg 30deg, #e52e71 30deg 60deg);

A repeating linear gradient alternating between two colors every 10 pixels at a 45-degree angle:

.gradient {
  background-image:
    repeating-linear-gradient(
      45deg,
      #ff8a00,
      #ff8a00 10px,
      #e52e71 10px,
      #e52e71 20px
    );
}

The radial equivalent adds a declared shape and starting point:

.gradient {
  background-image:
    repeating-radial-gradient(
      circle at 0 0,
      #ff8a00,
      #ff8a00 10px,
      #e52e71 10px,
      #e52e71 20px
    );
}

Conic work the same way; the key difference is that radial colors transition from the source center while conic colors transition around the element:

.gradient {
  background-image:
    repeating-conic-gradient(
      from 0deg, 
      #ff8a00 0deg 30deg, 
      #e52e71 30deg 60deg
  );
}

The support data below covers linear and radial repeating gradients specifically:

Desktop

ChromeFirefoxIEEdgeSafari
10*3.6*10125.1*

Mobile / Tablet

Android ChromeAndroid FirefoxAndroidiOS Safari
1571604*5.0-5.1*

Patterns and effects

  • Patterns — repeating gradients cover checkerboards and chevrons, and a diagonal graph-paper snippet produces grid paper. Lea Verou's CSS gradient pattern gallery is a deeper reference.
  • Border gradients — gradients can back borders, feed pattern-based fancy borders, or combine hard stops with overlapping content for layered effects.
  • Animated gradients — shifting background-position makes a gradient's transitions appear to move. A demo by Marty also uses radial gradients with hard stops on the pointer position to simulate sunlight.

Accessibility

Treat contrast the way you would with background-color: every color in the gradient and every transition between them must leave content on top legible. A contrast checker helps. For motion, hover transitions between solid colors are rarely a concern, but when background-position animation makes a gradient appear to move, honor the prefers-reduced-motion media query so only the right users see it.

Specification

  • CSS Images Module Level 3 (latest)
  • CSS Images Module Level 3 (editor's draft)