2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: Linear Gradient
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Linear Gradient
  
						 
					
						
							
								
									
										
										
										
											2018-10-23 09:53:29 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								You can use a CSS linear gradient to make colors transition or fade from one color to another.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-23 09:53:29 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Syntax 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								To create a linear gradient you must define at least two color stops.(They are the colors the transitions are created from). It is declared on either the `background`  or `background-image`  properties.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```css
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								background: linear-gradient(direction, colour-stop1, colour-stop2, ...);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-23 09:53:29 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Note: If no direction is specified, the transition is `top to bottom`  by default
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-23 09:53:29 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Different Gradient transitions
  
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								**Top to Bottom :**
							 
						 
					
						
							
								
									
										
										
										
											2018-10-23 09:53:29 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```css
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								background: linear-gradient(red, yellow);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Left To Right :**
							 
						 
					
						
							
								
									
										
										
										
											2018-10-23 09:53:29 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								To make it left to right, you add an additional parameter at the beginning of the linear-gradient starting with the word `to`  followed by the direction you want the transition to go.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```css
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								background: linear-gradient(to right, red , yellow);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Diagonal Positions :**
							 
						 
					
						
							
								
									
										
										
										
											2018-10-23 09:53:29 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								You can also transition a gradient diagonally by specifying the direction you want the transition to go, for example: `top left`  or `bottom right` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Here's a sample for a gradient starting at top-left
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```css
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								 background: linear-gradient(to bottom right, red, yellow);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-23 09:53:29 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Using Angles to Specify Direction of the gradient
  
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								You can also use angles, to be more accurate in specifying the direction of the gradient:
							 
						 
					
						
							
								
									
										
										
										
											2018-10-23 09:53:29 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```css
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								background: linear-gradient(angle, colour-stop1, colour-stop2);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The angle is specified as an angle between a horizontal line and the gradient line.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-23 09:53:29 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```css
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								background: linear-gradient(90deg, red, yellow);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-23 09:53:29 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Using more than two colors
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You're not limited to just two colors, you can use as many comma separated colors as you want.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```css
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								background: linear-gradient(red, yellow, green); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-23 09:53:29 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-23 09:53:29 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								You can also use other color units like RGB or hex codes to specify the colors.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-23 09:53:29 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Define where the colors will transition
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Your gradient can transition wherever you want by giving your colors a value when you define the linear-gradient
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```css
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								background: linear-gradient(to right,red 5px, yellow 25%, green 80%);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-23 09:53:29 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								In this case, the transition from red to yellow will start at 5px and finish at 25% from the left, and the transition from yellow to green will start at 25% and finish at 80% from the left.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Hard color stops
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You can not only use gradients to transition with fading colors, but you can also use it to change from one solid color to another solid color instantly
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```css
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								background: linear-gradient(to right,red 45%, yellow 45%);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-23 09:53:29 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Here the transition from red to yellow will start at 45% and finish at 45%.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-23 09:53:29 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								**More information:** [Linear-gradient on Mozilla Docs ](https://developer.mozilla.org/en-US/docs/Web/CSS/linear-gradient )