Skip to main content

Continue from the previous article, in this part we give another simple samples about gradient on css3. CSS3 gradient let you display smooth transitions between two or more specified colors.

CSS3 defines two types of gradients:

  • Linear Gradients (goes down/up/left/right/diagonally)

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.

Linear Gradient – Top to Bottom (this is default)

#gradient1
{
background: -webkit-linear-gradient(yellow, green); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(yellow, green); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(yellow, green); /* For Firefox 3.6 to 15 */
background: linear-gradient(yellow, green); /* Standard syntax (must be last) */
}

 gradient1

Linear Gradient – Left to Right

#gradient2
{
background: -webkit-linear-gradient(left, yellow , green); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(right, yellow, green); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(right, yellow, green); /* For Firefox 3.6 to 15 */
background: linear-gradient(to right, yellow , green); /* Standard syntax (must be last) */
}

gradient2

Linear Gradient – Diagonal

#gradient3
{
background: -webkit-linear-gradient(left top, yellow , green); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(bottom right, yellow, green); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(bottom right, yellow, green); /* For Firefox 3.6 to 15 */
background: linear-gradient(to bottom right, yellow , green); /* Standard syntax (must be last) */
}

gradient3

Linear Gradients – Transparency

#gradient4
{
background: -webkit-linear-gradient(left, rgba(255,0,0,0), rgba(255,0,0,1)); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(right, rgba(255,0,0,0), rgba(255,0,0,1)); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(right, rgba(255,0,0,0), rgba(255,0,0,1)); /* For Firefox 3.6 to 15 */
background: linear-gradient(to right, rgba(255,0,0,0), rgba(255,0,0,1)); /* Standard syntax (must be last) */
}

gradient4

Linear Gradients – Multiple colors

#gradient5
{
background: -webkit-linear-gradient(red, orange, yellow, green, blue, indigo, violet); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(red, orange, yellow, green, blue, indigo, violet); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(red, orange, yellow, green, blue, indigo, violet); /* For Fx 3.6 to 15 */
background: linear-gradient(red, orange, yellow, green, blue, indigo, violet); /* Standard syntax (must be last) */
}

gradient6

Linear Gradients – Multiple colors left to right

#gradient6
{
background: -webkit-linear-gradient(left, red, orange, yellow, green, blue, indigo, violet); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(left, red, orange, yellow, green, blue, indigo, violet); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(left, red, orange, yellow, green, blue, indigo, violet); /* For Fx 3.6 to 15 */
background: linear-gradient(to right, red, orange, yellow, green, blue, indigo, violet); /* Standard syntax (must be last) */
}

gradient5

Linear Gradients – Repeating

#gradient7
{
background: -webkit-repeating-linear-gradient(red, yellow 10%, green 20%); /* For Safari 5.1 to 6.0 */
background: -o-repeating-linear-gradient(red, yellow 10%, green 20%); /* For Opera 11.1 to 12.0 */
background: -moz-repeating-linear-gradient(red, yellow 10%, green 20%); /* For Firefox 3.6 to 15 */
background: repeating-linear-gradient(red, yellow 10%, green 20%); /* Standard syntax (must be last) */
}

gradient7

Linear Gradient – 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 left, to bottom right, etc.).

Angles 0deg

#gradient8
{
background: -webkit-linear-gradient(0deg, green, blue); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(0deg, green, blue); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(0deg, green, blue); /* For Firefox 3.6 to 15 */
background: linear-gradient(0deg, green, blue); /* Standard syntax (must be last) */
}

gradient8

Angles 90deg

#gradient9
{
background: -webkit-linear-gradient(90deg, green, blue); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(90deg, green, blue); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(90deg, green, blue); /* For Firefox 3.6 to 15 */
background: linear-gradient(90deg, green, blue); /* Standard syntax (must be last) */
}

gradient9

Angles 180deg

#gradient10
{
background: -webkit-linear-gradient(180deg, green, blue); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(180deg, green, blue); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(180deg, green, blue); /* For Firefox 3.6 to 15 */
background: linear-gradient(180deg, green, blue); /* Standard syntax (must be last) */
}

gradient10

Angles -90deg

#gradient11
{
background: -webkit-linear-gradient(-90deg, green, blue); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(-90deg, green, blue); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(-90deg, green, blue); /* For Firefox 3.6 to 15 */
background: linear-gradient(-90deg, green, blue); /* Standard syntax (must be last) */
}

gradient11

  • Radial Gradients (defined by their center)

To create a radial gradient you must also define at least two color stops. You can also specify the gradient’s center, shape (circle or ellipse) as well as its size. By default, center is center, shape is ellipse, and size is farthest-corner.

Radial Gradient – Shape Default

#gradient12
{
background: -webkit-radial-gradient(red, green, blue); /* Safari 5.1 to 6.0 */
background: -o-radial-gradient(red, green, blue); /* For Opera 11.6 to 12.0 */
background: -moz-radial-gradient(red, green, blue); /* For Firefox 3.6 to 15 */
background: radial-gradient(red, green, blue); /* Standard syntax (must be last) */
}

gradient12

Radial Gradient – Shape Circle

#gradient13
{
background: -webkit-radial-gradient(circle, red, green, blue); /* Safari 5.1 to 6.0 */
background: -o-radial-gradient(circle, red, green, blue); /* For Opera 11.6 to 12.0 */
background: -moz-radial-gradient(circle, red, green, blue); /* For Firefox 3.6 to 15 */
background: radial-gradient(circle, red, green, blue); /* Standard syntax (must be last) */
}

gradient13

Radial Gradient – Closest Side

#gradient14
{
background: -webkit-radial-gradient(60% 55%, closest-side,blue,green,yellow,red); /* Safari 5.1 to 6.0 */
background: -o-radial-gradient(60% 55%, closest-side,blue,green,yellow,red); /* For Opera 11.6 to 12.0 */
background: -moz-radial-gradient(60% 55%, closest-side,blue,green,yellow,red); /* For Firefox 3.6 to 15 */
background: radial-gradient(60% 55%, closest-side,blue,green,yellow,red); /* Standard syntax (must be last) */
}

gradient14

Radial Gradient – Farthest Side

#gradient15
{
background: -webkit-radial-gradient(60% 55%, farthest-side,blue,green,yellow,red); /* Safari 5.1 to 6.0 */
background: -o-radial-gradient(60% 55%, farthest-side,blue,green,yellow,red); /* For Opera 11.6 to 12.0 */
background: -moz-radial-gradient(60% 55%, farthest-side,blue,green,yellow,red); /* For Firefox 3.6 to 15 */
background: radial-gradient(60% 55%, farthest-side,blue,green,yellow,red); /* Standard syntax (must be last) */
}

gradient15

Radial Gradient – Closest Corner

#gradient16
{
background: -webkit-radial-gradient(60% 55%, closest-corner,blue,green,yellow,red); /* Safari 5.1 to 6.0 */
background: -o-radial-gradient(60% 55%, closest-corner,blue,green,yellow,red); /* For Opera 11.6 to 12.0 */
background: -moz-radial-gradient(60% 55%, closest-corner,blue,green,yellow,red); /* For Firefox 3.6 to 15 */
background: radial-gradient(60% 55%, closest-corner,blue,green,yellow,red); /* Standard syntax (must be last) */
}

gradient16

Radial Gradient – Farthest Corner

#gradient17
{
background: -webkit-radial-gradient(60% 55%, farthest-corner,blue,green,yellow,red); /* Safari 5.1 to 6.0 */
background: -o-radial-gradient(60% 55%, farthest-corner,blue,green,yellow,red); /* For Opera 11.6 to 12.0 */
background: -moz-radial-gradient(60% 55%, farthest-corner,blue,green,yellow,red); /* For Firefox 3.6 to 15 */
background: radial-gradient(60% 55%, farthest-corner,blue,green,yellow,red); /* Standard syntax (must be last) */
}

gradient17

Radial Gradient – Repeating

#gradient18
{
background: -webkit-repeating-radial-gradient(red, green 4%, yellow 10%, grey 15%); /* For Safari 5.1 to 6.0 */
background: -o-repeating-radial-gradient(red, green 4%, yellow 10%, grey 15%); /* For Opera 11.6 to 12.0 */
background: -moz-repeating-radial-gradient(red, green 4%, yellow 10%, grey 15%); /* For Firefox 3.6 to 15 */
background: repeating-radial-gradient(red, green 4%, yellow 10%, grey 15%); /* Standard syntax (must be last) */
}

gradient18

Note: Internet Explorer 9 and earlier versions do not support gradients.