bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/CSS/Advanced Styling
CSS•Advanced Styling

CSS Gradients

The CSS gradient functions let you display smooth transitions between two or more colors within an element.

CSS defines three types of gradients

  • Linear Gradients - The color transition goes down, up, left, right, or diagonally
  • Radial Gradients - The color transition goes out from a central point
  • Conic Gradients - The color transition is rotated around a center point

The CSS gradient functions are used within the background-image property.

CSS linear-gradient() Function

The CSS linear-gradient() function creates a linear gradient.

A linear gradient defines a color transition that goes in a straight line, it can go down, up, to left, to right, or diagonally.

A linear gradient requires at least two color stops. Color stops are the colors you want to render smooth transitions among. You can also set a starting point and a direction (or an angle) along with the gradient effect.

Syntax

background-image: linear-gradient( direction , color-stop1 , color-stop2, ... );

Direction - Top to Bottom (this is default)

The following example shows a linear gradient that goes from top to bottom. It starts red, transitioning to yellow:

Example

Formatted code
#grad {
  background-image: linear-gradient(to bottom, red, yellow);
}

Live preview

Direction - Bottom to Top

The following example shows a linear gradient that goes from bottom to top. It starts red, transitioning to yellow:

Example

Formatted code
#grad {
  background-image: linear-gradient(to top, red, yellow);
}

Live preview

Direction - Left to Right

The following example shows a linear gradient that goes from left to right. It starts red, transitioning to yellow:

Example

Formatted code
#grad {
  background-image: linear-gradient(to right, red , yellow);
}

Live preview

Direction - Diagonal

The following example shows a linear gradient that goes from top-left to bottom-right. It starts red, transitioning to yellow:

Example

Formatted code
#grad {
  background-image: linear-gradient(to bottom right, red, yellow);
}

Live preview

Linear Gradient - Using Angles

If you want more control over the direction of the gradient, you can define an angle parameter, instead of the predefined directions (to bottom, to top, to right, to left, to bottom right, etc.).

  • A value of 0deg is equivalent to "to top".
  • A value of 90deg is equivalent to "to right".
  • A value of 180deg is equivalent to "to bottom".
  • A value of 270deg equivalent to "to left"

Syntax

background-image: linear-gradient( angle , color-stop1 , color-stop2 );

The following example shows how to use angles on linear gradients:

Example

Formatted code
#grad {
  background-image: linear-gradient(180deg, red, yellow);
}

Live preview

Linear Gradient - Multiple Color Stops

The following example shows a linear gradient (from top to bottom) with multiple color stops:

Example

Formatted code
#grad {
  background-image: linear-gradient(red, yellow, green);
}

Live preview

The following example shows a linear gradient (from left to right) with the color of the rainbow and some text:

Example

Formatted code
#grad {
  background-image: linear-gradient(to right, red,orange,yellow,green,blue,indigo,violet);
}

Live preview

Linear Gradient - Transparency

CSS gradients also support transparency, which can be used to create fading effects.

To add transparency, we use the rgba() function to define the color stops. The last parameter in the rgba() function can be a value from 0 to 1, and it defines the transparency of the color: 0 indicates full transparency, 1 indicates full color (no transparency).

The following example shows a linear gradient that goes from left to right. It starts fully transparent, transitioning to full color red:

Example

Formatted code
#grad {
  background-image: linear-gradient(to right, rgba(255,0,0,0), rgba(255,0,0,1));
}

Live preview

CSS repeating-linear-gradient() Function

The CSS repeating-linear-gradient() function is used to repeat linear gradients:

Example

Formatted code
#grad {
  background-image: repeating-linear-gradient(red, yellow 10%, green 20%);
}

Live preview

Previous

CSS Color Keywords

Next

CSS Radial Gradients

This chapter

Overview
53

Lessons

154m

Read time

1. CSS Rounded Corners2. Sass Tutorial3. CSS Border Images4. CSS Multiple Backgrounds5. CSS Background Size6. CSS Background Origin7. CSS Background Clip8. CSS Colors9. CSS Color Keywords10. CSS Gradients11. CSS Radial Gradients12. CSS Conic Gradients13. CSS Shadow Effects14. CSS Box Shadow15. CSS Text Effects16. CSS Custom Fonts17. CSS 2D Transforms18. CSS 2D Transform Scale19. CSS 2D Transform Skew / Matrix20. CSS 3D Transforms21. CSS Transitions22. CSS Transition Timing23. CSS Animations24. CSS Animation Timing25. CSS Animation Properties26. CSS Tooltip27. CSS Tooltip Arrows28. CSS Styling Images29. CSS Image Effects30. CSS Image Hover Overlays31. CSS Responsive Modal Images32. CSS Centering Images33. CSS Image Filter Effects34. CSS Image Shapes35. CSS object-fit Property36. CSS object-position Property37. CSS Masking38. CSS Gradient Mask Layers39. CSS SVG Mask Layers40. CSS Styling Buttons41. CSS Button Hover Effects42. CSS Button Groups43. CSS Pagination Styles44. CSS Multiple Columns45. CSS Column Rules46. CSS User Interface47. CSS Variables - var() Function48. CSS Overriding Variables49. CSS Variables and JavaScript50. CSS Variables in Media Queries51. CSS @property Rule52. CSS Box Sizing53. CSS Media Queries

On this page

CSS linear-gradient() FunctionDirection - Top to Bottom (this is default)Direction - Bottom to TopDirection - Left to RightDirection - DiagonalLinear Gradient - Using AnglesLinear Gradient - Multiple Color StopsLinear Gradient - TransparencyCSS repeating-linear-gradient() Function