Gradients

Frontend SELF EN
Level 18 , Lesson 5
Available

5.1 Linear Gradients

Gradients are a powerful tool in web design, letting you create smooth transitions between colors without using images. CSS provides two main types of gradients: linear (linear-gradient) and radial (radial-gradient). These gradients are used for backgrounds of elements, creating buttons, and other visual effects.

Linear gradients (linear-gradient)

A linear gradient creates a smooth transition between two or more colors along a specified line (axis). The line can be horizontal, vertical, or at any angle.

Syntax

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

Values

  • direction: the direction of the gradient. Can be specified in degrees (e.g., 90deg), using keywords (e.g., to right, to bottom left)
  • color-stop: a color stop, which determines the color and its position in the gradient

Usage examples

Horizontal gradient:

CSS
    
      .linear-gradient-horizontal {
        width: 200px;
        height: 200px;
        background: linear-gradient(to right, red, yellow);
      }
    
  
HTML
    
      <body>
        <div class="linear-gradient-horizontal">Horizontal Gradient</div>
      </body>
    
  

Vertical gradient:

CSS
    
      .linear-gradient-vertical {
        width: 200px;
        height: 200px;
        background: linear-gradient(to bottom, blue, green);
      }
    
  
HTML
    
      <body>
        <div class="linear-gradient-vertical">Vertical Gradient</div>
      </body>
    
  

Diagonal gradient:

CSS
    
      .linear-gradient-diagonal {
        width: 200px;
        height: 200px;
        background: linear-gradient(45deg, purple, pink);
      }
    
  
HTML
    
      <body>
        <div class="linear-gradient-diagonal">Diagonal Gradient</div>
      </body>
    
  

Multicolor gradient:

CSS
    
      .linear-gradient-multi {
        width: 200px;
        height: 200px;
        background: linear-gradient(to right, red, yellow, green, blue);
      }
    
  
HTML
    
      <body>
        <div class="linear-gradient-multi">Multicolor Gradient</div>
      </body>
    
  

Code Explanation:

  • to right: the gradient goes from left to right
  • to bottom: the gradient goes from top to bottom
  • 45deg: the gradient goes at a 45-degree angle
  • red, yellow, green, blue: several color stops to create a multicolor gradient

5.2 Radial Gradients

radial-gradient creates a smooth transition between colors, starting from the center and spreading to the edges of a circle or ellipse. Radial gradients can create interesting visual effects, like simulating 3D.

Syntax:

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

Values:

  • shape: shape of the gradient, can be circle or ellipse (default)
  • size: size of the gradient, can be closest-side, closest-corner, farthest-side, farthest-corner
  • position: position of the gradient's center, can be specified in percentages, pixels, or using keywords (e.g., center, top left)
  • color-stop: a color stop, which determines the color and its position in the gradient

Usage examples

Circular gradient:

CSS
    
      .radial-gradient-circle {
        width: 200px;
        height: 200px;
        background: radial-gradient(circle, red, yellow);
      }
    
  
HTML
    
      <body>
        <div class="radial-gradient-circle">Circular Gradient</div>
      </body>
    
  

Elliptical gradient:

CSS
    
      .radial-gradient-ellipse {
        width: 200px;
        height: 200px;
        background: radial-gradient(ellipse, blue, green);
      }
    
  
HTML
    
      <body>
        <div class="radial-gradient-ellipse">Elliptical Gradient</div>
      </body>
    
  

Center gradient:

CSS
    
      .radial-gradient-at-center {
        width: 200px;
        height: 200px;
        background: radial-gradient(circle at center, purple, pink);
      }
    
  
HTML
    
      <body>
        <div class="radial-gradient-at-center">Center Gradient</div>
      </body>
    
  

Corner gradient:

CSS
    
      .radial-gradient-at-corner {
        width: 200px;
        height: 200px;
        background: radial-gradient(circle at top left, red, blue, green);
      }
    
  
HTML
    
      <body>
        <div class="radial-gradient-at-corner">Corner Gradient</div>
      </body>
    
  

Code Explanation:

  • circle: creates a circular gradient
  • ellipse: creates an elliptical gradient
  • at center: the gradient starts from the center
  • at top left: the gradient starts from the top left corner
  • red, blue, green: several color stops to create a multicolor gradient

5.3 Additional Gradient Settings

Repeating Gradients

1. Repeating Linear Gradients (repeating-linear-gradient)

Repeating linear gradients create a repeating pattern with a linear gradient.

CSS
    
      background: repeating-linear-gradient(45deg, red, yellow 10%, green 20%);
    
  

2. Repeating Radial Gradients (repeating-radial-gradient)

CSS
    
      background: repeating-radial-gradient(circle, red, yellow 10%, green 20%);
    
  

3. Transparent Color Stops

Color stops can include transparency to create more complex visual effects.

CSS
    
      background: linear-gradient(to right, rgba(255, 0, 0, 0.5), rgba(0, 0, 255, 0.5));
    
  

4. Using Multiple Gradients

You can use multiple gradients to create complex backgrounds.

CSS
    
      background: linear-gradient(to right, red, yellow), radial-gradient(circle, blue, green);
    
  
1
Task
Frontend SELF EN, level 18, lesson 5
Locked
Linear Gradient
Linear Gradient
1
Task
Frontend SELF EN, level 18, lesson 5
Locked
Radial Gradient
Radial Gradient
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION