Welcome to FixHub, If you need design please contact us

CSS Gradients

 

CSS Gradients

Gradient Backgrounds

CSS gradients let you display smooth transitions between two or more specified colors.

CSS defines three types of gradients:

  • Linear Gradients (goes down/up/left/right/diagonally)
  • Radial Gradients (defined by their center)
  • Conic Gradients (rotated around a center point)

CSS Linear Gradients

To create a linear gradient you must define 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(directioncolor-stop1color-stop2, ...);

Direction - Top to Bottom (this is default)

The following example shows a linear gradient that starts at the top. It starts red, transitioning to yellow:

top to bottom (default)

Example

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

Direction - Left to Right

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

left to right

Example

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

Direction - Diagonal

You can make a gradient diagonally by specifying both the horizontal and vertical starting positions.

The following example shows a linear gradient that starts at top left (and goes to bottom right). It starts red, transitioning to yellow:

top left to bottom right

Example

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

Using Angles

If you want more control over the direction of the gradient, you can define an angle, 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".

Syntax

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

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

180deg

Example

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

Using Multiple Color Stops

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

Example

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

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

Rainbow Background

Example

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

Using 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 starts from the left. It starts fully transparent, transitioning to full color red:

Example

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

Repeating a linear-gradient

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

Example

A repeating linear gradient:

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

CSS Radial Gradients

A radial gradient is defined by its center.

To create a radial gradient you must also define at least two color stops.

Syntax

background-image: radial-gradient(shape size at position, start-color, ..., last-color);

By default, shape is ellipse, size is farthest-corner, and position is center.

Radial Gradient - Evenly Spaced Color Stops (this is default)

The following example shows a radial gradient with evenly spaced color stops:

Example

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

Radial Gradient - Differently Spaced Color Stops

The following example shows a radial gradient with differently spaced color stops:

Example

#grad {
  background-image: radial-gradient(red 5%, yellow 15%, green 60%);
}

Set Shape

The shape parameter defines the shape. It can take the value circle or ellipse. The default value is ellipse.

The following example shows a radial gradient with the shape of a circle:

Example

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

Use of Different Size Keywords

The size parameter defines the size of the gradient. It can take four values:

  • closest-side
  • farthest-side
  • closest-corner
  • farthest-corner

Example

A radial gradient with different size keywords:

#grad1 {
  background-image: radial-gradient(closest-side at 60% 55%, red, yellow, black);
}

#grad2 {
  background-image: radial-gradient(farthest-side at 60% 55%, red, yellow, black);
}

Repeating a radial-gradient

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

Example

A repeating radial gradient:

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

Bottom of Form

CSS Conic Gradients

A conic gradient is a gradient with color transitions rotated around a center point.

To create a conic gradient you must define at least two colors.

Syntax

background-image: conic-gradient([from angle] [at position,] color [degree], color [degree], ...);

By default, angle is 0deg and position is center.

If no degree is specified, the colors will be spread equally around the center point.


Conic Gradient: Three Colors

The following example shows a conic gradient with three colors:

Example

A conic gradient with three colors:

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

Conic Gradient: Five Colors

The following example shows a conic gradient with five colors:

Example

A conic gradient with five colors:

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

Conic Gradient: Three Colors and Degrees

The following example shows a conic gradient with three colors and a degree for each color:

Example

A conic gradient with three colors and a degree for each color:

#grad {
  background-image: conic-gradient(red 45deg, yellow 90deg, green 210deg);
}

Create Pie Charts

Just add border-radius: 50% to make the conic gradient look like a pie:

Example

#grad {
  background-image: conic-gradient(red, yellow, green, blue, black);
  border-radius: 50%;
}

Here is another pie chart with defined degrees for all the colors:

Example

#grad {
  background-image: conic-gradient(red 0deg, red 90deg, yellow 90deg, yellow 180deg, green 180deg, green 270deg, blue 270deg);
  border-radius: 50%;
}

Conic Gradient With Specified From Angle

The [from angle] specifies an angle that the entire conic gradient is rotated by.

The following example shows a conic gradient with a from angle of 90deg:

Example

A conic gradient with a from angle:

#grad {
  background-image: conic-gradient(from 90deg, red, yellow, green);
}

Conic Gradient With Specified Center Position

The [at position] specifies the center of the conic gradient.

The following example shows a conic gradient with a center position of 60% 45%:

Example

A conic gradient with a specified center position:

#grad {
  background-image: conic-gradient(at 60% 45%, red, yellow, green);
}

Repeating a Conic Gradient

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

Example

A repeating conic gradient:

#grad {
  background-image: repeating-conic-gradient(red 10%, yellow 20%);
  border-radius: 50%;
}

Here is a repeating conic gradient with defined color-starts and color-stops:

Example

A repeating conic gradient with defined color-starts and color-stops:

#grad {
  background-image: repeating-conic-gradient(red 0deg 10deg, yellow 10deg 20deg, blue 20deg 30deg);
  border-radius: 50%;
}

CSS Gradient Functions

The following table lists the CSS gradient functions:

Function

Description

conic-gradient()

Creates a conic gradient. Define at least two colors (around a center point)

linear-gradient()

Creates a linear gradient. Define at least two colors (top to bottom)

radial-gradient()

Creates a radial gradient. Define at least two colors (center to edges)

repeating-conic-gradient()

Repeats a conic gradient

repeating-linear-gradient()

Repeats a linear gradient

repeating-radial-gradient()

Repeats a radial gradient

 


Post a Comment

0Comments
Post a Comment (0)