×

CSS Gradient

CSS Gradient: The CSS gradient property is applied to show the smooth transition inside two specified colors or more than two specified colors.

Need of CSS Gradient

The following are a few reasons why CSS gradient is used:

  • We do not have to apply images to show transition effects.
  • The bandwidth and download time usage may be reduced also.
  • It can give a better appearance to an element if zoomed because any gradient is produced by a browser.

Following are two types of CSS3 gradients:

  1. Radial gradients
  2. Linear gradients

Linear Gradient

 The linear gradient in CSS3 goes left, right, up, down, and diagonally. To design the linear gradient in CSS3, we should have to specify two or more than two color stops. These are any color which can be applied to make the smooth transition. The direction and starting point can be included with any gradient effect also.

background: linear-gradient (direction, color-stop1, color-stop2.....);  
  1. Linear Gradient: (Top-Bottom)

The linear gradient from top to bottom is a default CSS linear gradient. Following is an illustration of the linear gradient, and it will start from the top. It will start blue and the transitions to the yellow.

Example:

<!DOCTYPE html> 
<html> 
<head> 
<style> 
#grad1
{ 
height: 100px; 
background: -webkit-linear-gradient(blue, yellow); /* For browser Safari 5.1 to 6.0 */ 
background: -o-linear-gradient(blue, yellow); /* For browser Opera 11.1 to 12.0 */ 
background: -moz-linear-gradient(blue, yellow); /* For browser Firefox 3.6 to 15 */ 
background: linear-gradient(blue, yellow); /* Standard syntax (should be last) */ 
} 
</style> 
</head> 
<body> 
<h3> Linear Gradient (Top-Bottom) </h3> 
<p> This linear gradient will be started from the top. It will start blue, transitioning to yellow: </p>  
<div id="grad1"></div> 
</body> 
</html> 

Output:

CSS Gradient
  1. Linear Gradient: (Left-Right)

The following illustration displays the CSS linear gradient. It will start from the left side and proceed to the right side. It will start blue from the left and the transitioning to the green.

Example:

<!DOCTYPE html> 
<html> 
<head> 
<style> 
#grad1
{ 
height: 100px; 
background: -webkit-linear-gradient(left, blue, yellow); /* For browser Safari 5.1 to 6.0 */ 
background: -o-linear-gradient(right, blue, yellow); /* For browser Opera 11.1 to 12.0 */ 
background: -moz-linear-gradient(right, blue, yellow); /* For browser Firefox 3.6 to 15 */ 
background: linear-gradient(to right, blue, yellow); /* Standard syntax (should be last) */ 
} 
</style> 
</head> 
<body> 
<h3> Linear Gradient (Left-Right) </h3> 
<p> This linear gradient will be started from the left. It will start blue, transitioning to yellow: </p> 
<div id="grad1"></div> 
</body> 
</html> 

Output:

CSS Gradient
  1. Linear Gradient: (Diagonal)

When we describe the vertical and horizontal starting positions, we can create any linear-gradient diagonal.

Example:

<!DOCTYPE html> 
<html> 
<head> 
<style> 
#grad1
{ 
height: 100px; 
background: -webkit-linear-gradient(left top, blue, yellow); /* For browser Safari 5.1 to 6.0 */ 
background: -o-linear-gradient(bottom right, blue, yellow); /* For browser Opera 11.1 to 12.0 */ 
background: -moz-linear-gradient(bottom right, blue, yellow); /* For browser Firefox 3.6 to 15 */ 
background: linear-gradient(to bottom right, blue, yellow); /* Standard syntax (should be last) */ 
} 
</style> 
</head> 
<body> 
<h3> Linear Gradient (Diagonal) </h3> 
<p> This linear gradient will be started from the left. It will start blue, transitioning to yellow: </p> 
<div id="grad1"></div> 
</body> 
</html> 

Output:

CSS Gradient

Radial Gradient

We should have to represent at least two of the color stops for making the radial gradient. This gradient can be represented through its center.

background: radial-gradient(shape size at position, start-color, ..., last-color);   
  1. Radial Gradient: (Color Stops, i.e., evenly spaced)

Color Stops, i.e., evenly spaced, is the default CSS radial gradient. It has default shapes, which can be position in the center, eclipse, size in the farthest- carner.

Example:

<!DOCTYPE html> 
<html> 
<head> 
<style> 
#grad1
{ 
height: 150px; 
width: 200px; 
background: -webkit-radial-gradient(blue, yellow, green); /* For browser Safari 5.1 to 6.0 */ 
background: -o-radial-gradient(blue, yellow, green); /* For browser Opera 11.6 to 12.0 */ 
background: -moz-radial-gradient(blue, yellow, green); /* For browser Firefox 3.6 to 15 */ 
background: radial-gradient(blue, yellow, green); /* Standard syntax (should be last) */ 
} 
</style> 
</head> 
<body> 
<h3>Radial Gradient - Color Stops, i.e., Evenly Spaced </h3> 
<div id="grad1"></div> 
</body> 
</html> 

Output:

CSS Gradient
  1. Radial Gradient: (Color Stops, i.e., differently spaced)

Example:

<!DOCTYPE html> 
<html> 
<head> 
<style> 
#grad1
{ 
height: 150px; 
width: 200px; 
background: -webkit-radial-gradient(blue 5%, yellow 15%, green 60%); /* For browser Safari 5.1 to 6.0 */ 
background: -o-radial-gradient(blue 5%, yellow 15%, green 60%); /* For browser Opera 11.6 to 12.0 */ 
background: -moz-radial-gradient(blue 5%, yellow 15%, green 60%); /* For browser Firefox 3.6 to 15 */ 
background: radial-gradient(blue 5%, yellow 15%, green 60%); /* Standard syntax (should be last) */ 
} 
</style> 
</head> 
<body> 
<h3>Radial Gradient - Color Stops, i.e., Differently Spaced </h3> 
<div id="grad1"></div> 
</body> 
</html> 

Output:

CSS Gradient

Related Topics

How to resize an image in CSS

How to resize an image in CSS? The resize image attribute is used in responsive web design to resize images to fit within the div container automatically. The resize image attribute...

3 minutes read.

CSS Selector

CSS Selector: There are some selectors used in CSS, which allows us to choose the content we wish to style. Various selectors are considered as the separation of the set...

5 minutes read.

CSS Content Property

Content Property The content attribute produces dynamic content. It can be applied with any pseudo-element ::after and ::before. These CSS properties are entirely supported inside every browser and applied to include...

5 minutes read.

Difference between HTML and CSS

HTML HTML stands for HyperText Markup Language and is used to create web pages and websites and web applications, which means we can create static web pages. Html is a scripting...

3 minutes read.

How to use google fonts in CSS

Fonts: - Fonts are used to make our website look more beautiful. Choosing appropriate font according to the web design or layout is also important. Many typefaces are accessible in...

4 minutes read.

CSS Page-break-after Attribute

Page-break-after Attribute As its name implies, the page-break-before property is applied to include page break after any element, at the time of printing a document. It adds the page break after...

5 minutes read.

CSS Color

CSS Colors: The CSS color property is used for setting the HTML element’s color. The color of the fonts and the color of the background can be set-up by using...

5 minutes read.

CSS Radio Button

Radio Button A radio button has been defined as an element of HTML. It can support us to take any input from the user. But it is challenging to design a...

4 minutes read.

CSS Line Height

CSS Line Height: This property in CSS can be used to describe the line box’s minimal height inside the element. It sets-up the distinction among two lines in our content. It...

3 minutes read.

CSS Reference

The CSS Reference lists all of the basic standard CSS properties, pseudo-classes, pseudo-elements, and data types, for all intents and purposes functional notations, and at-rules alphabetically.  The site also includes...

5 minutes read.

CSS Functions

CSS functions are used for different CSS properties. Every front-end developer should have knowledge of all CSS functions. It makes the coding easier. FunctionsExplanationattr().It returns the value of an attribute of...

5 minutes read.

CSS Border

CSS Border: The border property of CSS allows us to define the style, color, width, and radius of the element’s border. It is applied to various elements to set-up the...

4 minutes read.

CSS z-index Property

CSS z-index: CSS z-index permits us to represent any visual hierarchy over a 3D plane that is the z-axis. This index can be applied to describe the positioned element’s stacking...

2 minutes read.

How to make smooth bounce animation using CSS

The smooth bouncing animation project is done with the help of HTML and CSS. This project gives so much fun and excitement to the users. What is HTML? The HTML or HyperText...

2 minutes read.

CSS Overflow

CSS Overflow: The Overflow property in CSS is used to manage the content if it overflows from its container of the block level. We have seen that all the elements...

3 minutes read.

CSS Filter

CSS Filter CSS filter is used to apply visual effects on the images, text, and other factors of the webpage. This filter property in CSS allows us to admit the effects like...

7 minutes read.

CSS Ellipsis

The CSS ellipsis kind of is the value that mostly is used by the property: text-overflow. Here, we will discuss the following: What is CSS Ellipsis? Syntax for CSS ellipsis How to...

3 minutes read.

How to Set Space between the Flexbox

For managing layout on the Web, CSS Flexbox and CSS Grid are two excellent tools. Flexbox handles one-dimensional layouts quite well, whereas CSS Grid manages two-dimensional layouts with columns and...

4 minutes read.

Is CSS a programming language?

 A programming language is a set of grammatical rules and a vocabulary for commanding a machine to accomplish specified tasks. High-level languages like BASIC, C, C++, COBOL, Java, FORTRAN, Ada...

4 minutes read.

Untitled Reusable Block

  ...

1 minute read.