Curriculum
Course: CSS Advanced
Login

Curriculum

CSS Advanced

CSS Rounded Corners

0/1

CSS Border Images

0/1

CSS Color Keywords

0/1

CSS Text Effects

0/1

CSS 2D Transforms

0/1

CSS 3D Transforms

0/1

CSS Transitions

0/1

CSS Animations

0/1

CSS Tooltip

0/1

CSS Style Images

0/1

CSS Image Reflection

0/1

CSS Masking

0/1

CSS Buttons

0/1

CSS Multiple Columns

0/1

CSS User Interface

0/1

CSS Box Sizing

0/1

CSS Media Queries

0/1
Text lesson

Linear Gradients

Image

CSS gradients enable the rendering of seamless transitions between two or more designated colors.

CSS categorizes gradients into three types:

  1. Linear Gradients (which can extend vertically, horizontally, or diagonally)
  2. Radial Gradients (center-defined gradients)
  3. Conic Gradients (rotated gradients originating from a central point)

CSS Linear Gradients

To generate a linear gradient, it’s necessary to specify a minimum of two color stops, representing the colors intended for seamless transitions. Additionally, you can designate a starting point and direction (or angle) to apply the gradient effect.

Syntax

background-image: linear-gradient(directioncolor-stop1color-stop2, …);

Direction – Top to Bottom (this is default)

Below is an example illustrating a linear gradient that originates from the top, commencing with red and transitioning smoothly into yellow:

Image

 

Example 

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

Direction – Left to Right

Below is an example demonstrating a linear gradient that begins from the left side, transitioning from red to yellow:

Image

Example 

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

Direction – Diagonal

By specifying both horizontal and vertical starting positions, you can create a diagonal gradient.

The following example illustrates a linear gradient starting from the top-left corner and extending to the bottom-right corner, transitioning from red to yellow:

Image

Example 

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

Using Angles

For greater control over the gradient’s direction, you can specify an angle rather than relying on predefined directions (such as “to bottom”, “to top”, “to right”, “to left”, “to bottom right”, etc.).

A 0deg angle corresponds to “to top”, 90deg to “to right”, and 180deg to “to bottom”, allowing precise adjustment of the gradient’s orientation.

Syntax

background-image: linear-gradient(anglecolor-stop1color-stop2);

Here’s an example illustrating the application of angles in linear gradients:

Image

Example 

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

Using Multiple Color Stops

Below is an example demonstrating a linear gradient with multiple color stops, transitioning from top to bottom:

Image

Example 

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

Here’s an example illustrating the creation of a linear gradient from left to right, featuring colors of the rainbow alongside some text:

Image

Example 

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

Using Transparency

CSS gradients facilitate the integration of transparency, enabling the creation of fading effects.

To incorporate transparency, the rgba() function is employed to define the color stops. The final parameter within the rgba() function, ranging from 0 to 1, determines the transparency level: 0 represents full transparency, while 1 signifies full color (no transparency).

The following example illustrates a linear gradient starting from the left, transitioning from full transparency to full-color red:

Image

Example 

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

Repeating a linear-gradient

The repeating-linear-gradient() function is employed to repeat linear gradient patterns.

Image

Example 

A linear gradient that repeats.

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