CSS Gradient

CSS Gradients

Gradient Backgrounds

CSS gradients let you display smooth transitions between two or more specified colors. CSS defines two types of gradients:

  • Linear Gradients (goes down/up/left/right/diagonally) and
  • Radial Gradients (defined by their center)

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(direction, color-stop1, color-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:

  •       
                        <!DOCTYPE html>		 
    <html>	
    <head>	
        <title>	CSS Gradient</title>	
    <style>	
    div{
        margin-bottom:20px;
        height: 200px;
    }
    .color_gradient {
      background-color: red; /* For browsers that do not support gradients */
      background-image: linear-gradient(to right, blue , green);
    }
    .color_gradient2 {
      background-color: red; /* For browsers that do not support gradients */
      background-image: linear-gradient(to top right, blue , green);
    }
    </style>	
    </head>	
    <body>	
        <div class="color_gradient">	</div>	
        <div class="color_gradient2">	</div>	
    </body>	
    </html>
                        

    Control 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".

  •       
                        <!DOCTYPE html>	 
    <html>
    <head>
        <title>CSS Web Layout</title>
    <style>
    .color_gradient {
      height: 55px;
      background-color: red; /* For browsers that do not support gradients */
      background-image: linear-gradient(to right, darkred, red, crimson, skyblue, navy, blue, darkblue);
    }
    </style>
    </head>
    <body>
        <div class='color_gradient'></div>
    </body>
    </html>
                        

    Using Multiple Color Stops

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

  •       
                        <!DOCTYPE html>	 
    <html>
    <head>
        <title>CSS Web Layout</title>
    <style>
    .color_gradient {
      height: 55px;
      background-color: red; /* For browsers that do not support gradients */
      background-image: linear-gradient(to right, darkred, red, crimson, skyblue, navy, blue, darkblue);
    }
    </style>
    </head>
    <body>
        <div class='color_gradient'></div>
    </body>
    </html>
                        

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

  •       
                        <!DOCTYPE html>	 
    <html>
    <head>
        <title>CSS Web Layout</title>
    <style>
    div{
    	height: 200px;
    }
    .color_gradient1 {
      background-color: red; /* For browsers that do not support gradients */
      background-image: repeating-linear-gradient(red, yellow 10%, green 20%);
    }
    
    .color_gradient2 {
      background-color: red; /* For browsers that do not support gradients */
      background-image: repeating-linear-gradient(45deg,red,yellow 7%,green 10%);
    }
    
    .color_gradient3 {
      background-color: red; /* For browsers that do not support gradients */
      background-image: repeating-linear-gradient(190deg,red,yellow 7%,green 10%);
    }
    
    .color_gradient4 {
      background-color: red; /* For browsers that do not support gradients */
      background-image: repeating-linear-gradient(90deg,red,yellow 7%,green 10%);
    }
    
    .color_gradient5 {
      background-color: red; /* For browsers that do not support gradients */
      background-image: repeating-linear-gradient(45deg, red 0px, red 10px, red 10px, yellow 10px, yellow 20px);
    }
    </style>
    </head>
    <body>
        <div class="color-gradient1"></div>
        <div class="color-gradient2"></div>
        <div class="color-gradient3"></div>
        <div class="color-gradient4"></div>
        <div class="color-gradient5"></div>
    </body>
    </html>
                        

    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:

  •       
                        <!DOCTYPE html>		 
    <html>	
    <head>	
        <title>	CSS Web Gradient</title>	
    <style>	
    .color-gradient1 {
        height:200px;
        background-image: linear-gradient(to right, rgba(52, 52, 152, 0.5), rgba(52, 52, 152, 1));
    }
    </style>	
    </head>	
    <body>	
    <div class="color-gradient1">	</div>	
    </body>	
    </html>
                        

    Dess App

    DessApp is an Integrated E-learning Education, Interactive and User-friendly features, smarter options and redefining your school costs effectively and efficiently.

    View
    1 1