Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS3 Gradients

CSS3 Gradients


Gradient Effects


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

Previously, you had to use images to achieve these effects. However, by using CSS3 gradients, you can reduce download time and bandwidth usage. In addition, elements with gradient effects look better when zoomed in, because the gradient is generated by the browser.

CSS3 defines two types of gradients:

Linear Gradient Related Properties:background-image。

Linear Gradient Online Tools:Gradient Online Tool。


Browser support

The numbers in the table specify the first browser version that fully supports the property.

property
background-image 1.0 4.0 1.0 1.0 3.5

**Note:** IE8 and earlier versions of IE do not support this property.


CSS3 Linear Gradients

To create a linear gradient, you must define at least two color stops. Color stops are the colors you want to transition smoothly between. You can also set a starting point and a direction (or an angle).

Examples of Linear Gradients:

Syntax



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

Linear Gradient - Top to Bottom (by default)

The example below demonstrates a linear gradient starting from the top. It starts red and gradually transitions to blue:

Example

Linear gradient from top to bottom:

#grad

{

background-image:

linear-gradient

(

#e66465

,

#9198e5

)

;

}

Try it »

Linear Gradient - Left to Right

The example below demonstrates a linear gradient starting from the left. It starts red and gradually transitions to yellow:

Example

Linear gradient from left to right:

#grad

{

height:

200

px

;

background-image:

linear-gradient

(

to

right

,

red

,

yellow

)

;

}

Try it »

Linear Gradient - Diagonal

You can create a diagonal gradient by specifying the horizontal and vertical starting positions.

The example below demonstrates a linear gradient starting from the top-left (to the bottom-right). It starts red and gradually transitions to yellow:

Example

Linear gradient from top-left to bottom-right:

#grad

{

height:

200

px

;

background-image:

linear-gradient

(

to

bottom

right

,

red

,

yellow

)

;

}

Try it »


Using an Angle

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

Syntax

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

The angle is measured between the horizontal line and the gradient line, counterclockwise. In other words, 0deg creates a bottom-to-top gradient, and 90deg creates a left-to-right gradient.

However, note that many browsers (Chrome, Safari, firefox, etc.) use the old standard, where 0deg creates a left-to-right gradient and 90deg creates a bottom-to-top gradient. Conversion formula 90 - x = y where x is the standard angle and y is the non-standard angle.

The example below demonstrates how to use an angle in a linear gradient:

Example

Linear gradient with a specified angle:

#grad

{

background-image:

linear-gradient

(-

90

deg

,

red

,

yellow

)

;

}

Try it »


Using Multiple Color Stops

The example below demonstrates how to set multiple color stops:

Example

Top-to-bottom linear gradient with multiple color stops:

#grad

{

background-image:

linear-gradient

(

red

,

yellow

,

green

)

;

}

Try it »

The example below demonstrates how to create a linear gradient with rainbow colors and text:

Example

#grad

{

/*

Standard Syntax

*/

background-image:

linear-gradient

(

to

right

,

red

,

orange

,

yellow

,

green

,

blue

,

indigo

,

violet

)

;

}

Try it »


Using Transparency (transparent)

CSS3 gradients also support transparency (transparent), which can be used to create fading effects.

To add transparency, we use the rgba() function to define color stops. The last parameter in the rgba() function can be a value from 0 to 1, which defines the transparency of the color: 0 means fully transparent, and 1 means fully opaque.

The example below demonstrates a linear gradient starting from the left. It starts fully transparent and gradually transitions to fully opaque red:

Example

Linear gradient from left to right, with transparency:

#grad

{

background-image:

linear-gradient

(

to

right

,

rgba

(

255

,

0

,

0

,

0

),

rgba

(

255

,

0

,

0

,

1

))

;

}

Try it »


Repeating Linear Gradients

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

Example

A repeating linear gradient:

#grad

{

/*

Standard Syntax

*/

background-image:

repeating-linear-gradient

(

red

,

yellow

10

%

,

green

20

%

)

;

}

Try it »


CSS3 Radial Gradients

A radial gradient is defined by its center.

To create a radial gradient, you must also define at least two color stops. Color stops are the colors you want to transition smoothly between. You can also specify the center, shape (circle or ellipse), and size of the gradient. By default, the center is center (meaning the center point), the shape is ellipse (meaning an ellipse), and the size is farthest-corner (meaning to the farthest corner).

Examples of Radial Gradients:

Syntax

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

Radial Gradient - Evenly Spaced Color Stops (by default)

Example

Radial gradient with evenly spaced color stops:

#grad

{

background-image:

radial-gradient

(

red

,

yellow

,

green

)

;

}

Try it »

Radial Gradient - Unevenly Spaced Color Stops

Example

Radial gradient with unevenly spaced color stops:

#grad

{

background-image:

radial-gradient

(

red

5

%

,

yellow

15

%

,

green

60

%

)

;

}

Try it »


Setting the Shape

The shape parameter defines the shape. It can be the value circle or ellipse. Here, circle means a circle and ellipse means an ellipse. The default value is ellipse.

Example

Radial gradient with a circular shape:

#grad

{

background-image:

radial-gradient

(

circle

,

red

,

yellow

,

green

)

;

}

Try it »


Using Different Size Keywords

The size parameter defines the size of the gradient. It can be one of the following four values:

Example

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

)

;

}

Try it »


Repeating Radial Gradients

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

%

)

;

}

Try it »