Using Gradients and Border Radius in CSS3: Part II

Introduction

 
 
Continuing further from the previous Using Border Radius and Gradients in CSS3: Part I  article, where we saw a basic introduction to using gradients, in this article, we will see how to use more complex gradients. Let's take an example of a linear gradient again.
 
Create the following HTML file and save it as index.html:
 
Index.html
  1. <!Doctype html>  
  2.   
  3. <head>  
  4.     <meta charset="utf-8">  
  5.     <title>Testing CSS3</title>  
  6.     <meta name="description" content="">  
  7.     <link rel="stylesheet" href="css/style.css">  
  8. </head>  
  9.   
  10. <body>  
  11.     <article>  
  12.         Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam molestie blandit elit sit amet vulputate. Nullam nec pharetra mi. Sed aliquet, turpis a rhoncus molestie, odio sapien imperdiet elit, non consequat sapien magna eu sem. Nulla facilisi. Vivamus augue enim, aliquam id vestibulum vitae, laoreet quis massa. Integer lacinia ultrices enim, id tristique justo blandit vitae.  
  13.         <br>  
  14.         <br>  
  15.         Donec nulla lectus, mattis vel pellentesque non, interdum ut nunc. Suspendisse potenti. Quisque a mauris dui. Nulla dictum augue eu lorem ornare sollicitudin egestas leo lacinia. Suspendisse potenti. Phasellus interdum lectus vel ipsum porta egestas. Maecenas in elit ut tortor vestibulum tristique. Proin imperdiet, felis non varius interdum, ipsum nisi malesuada metus, a porttitor est nibh non tortor. Duis diam diam, facilisis vel egestas eu, tempor ut nunc. Nam pharetra, tellus lacinia semper tempor, elit metus adipiscing massa, ut gravida neque felis vel libero.  
  16.         <br>  
  17.         <br>  
  18.         Sed semper tincidunt viverra. Mauris egestas nunc at nibh condimentum volutpat. Phasellus imperdiet adipiscing quam nec condimentum. Aenean at ornare sem. Vestibulum vitae nibh ligula. Ut ac egestas nisi. Sed a nisl lorem, vel pretium neque. Duis tincidunt nulla sit amet odio euismod ut blandit augue congue. Quisque ac lorem vitae nisl cursus volutpat. Phasellus bibendum lacinia feugiat. Nunc enim ligula, auctor iaculis commodo et, varius sit amet libero. Maecenas eu ante ut purus luctus semper eu et elit. Maecenas sit amet lectus justo.  
  19.         <br><br>  
  20.         <a href="http://www.lipsum.com/feed/html">About Lorem Ipsum</a>  
  21.     </article>  
  22. </body>  
  23.   
  24. </html> 
This HTML file is identical to the one created in the previous article. The only change will be the styles we apply. Save the following code in a file named style.css under the css folder:
 
css/style.css
  1. article {  
  2.     displayblock;  
  3.     text-alignjustify;  
  4.     width600px;  
  5.     margin1em auto;  
  6.     padding25px;  
  7.     colorblack;  
  8.     /* for Firefox */  
  9.     background-image:  
  10.         -moz-linear-gradient(45deg,  
  11.         #0000FF 8%,  
  12.         #FF6633 20%,  
  13.         #FFFF00 40%,  
  14.         #00FF00 60%,  
  15.         #FF3300 80%,  
  16.         #AA00AA 100%);  
  17.     /* for WebKit */  
  18.     background-image:  
  19.         -webkit-linear-gradient(45deg,  
  20.         #0000FF 8%,  
  21.         #FF6633 20%,  
  22.         #FFFF00 40%,  
  23.         #00FF00 60%,  
  24.         #FF3300 80%,  
  25.         #AA00AA 100%);  
  26.     /* for Opera */  
  27.     background-image:  
  28.         -o-linear-gradient(45deg,  
  29.         #0000FF 8%,  
  30.         #FF6633 20%,  
  31.         #FFFF00 40%,  
  32.         #00FF00 60%,  
  33.         #FF3300 80%,  
  34.         #AA00AA 100%);  

Similar to the earlier example, here too, care is taken to include code that will work on multiple browsers instead of catering to just one. Again when you view the HTML file in various browsers, you will find identical output everywhere similar to the one shown below.
 
img1.jpg
 
This colorful effect has been achieved using a combination of a start color, and end color and some color stops in between. A color stop is an in-between value and has a color and a position. When the browser fades the color from one stop to the next stop, a smooth multi-color effect is created, thus resulting in a gradient.
 
You can tweak or fine-tune the various values in the background-image property shown above until you get the color effect you are looking for. Alternatively, instead of using only hexadecimal color codes, you can also work with decimal color codes, that is, the RGB color combination.
  1. article {  
  2.   displayblock;  
  3.   text-alignjustify;  
  4.   width600px;  
  5.   margin1em auto;  
  6.   padding25px;  
  7.   colorwhite;  
  8.   /* Mozilla gradient syntax */  
  9.   background-image:  
  10.     -moz-linear-gradient(0% 0% 270deg,  
  11.     #800000 0,  
  12.     #0000ff 37%,  
  13.     #000404 83%,  
  14.     #000000 92%,  
  15.     #000404 98%);  
  16.   /* W3C gradient syntax for WebKit */  
  17.   background-image:  
  18.     -webkit-linear-gradient(top,  
  19.     #800000 0,  
  20.     #0000ff 37%,  
  21.     #000404 83%,  
  22.     #000000 92%,  
  23.     #000404 98%);  
  24.   /* W3C gradient syntax for Opera */  
  25.   background-image:  
  26.     -o-linear-gradient(top,  
  27.     #800000 0,  
  28.     #0000ff 37%,  
  29.     #000404 83%,  
  30.     #000000 92%,  
  31.     #000404 98%);  
  32.   /* Unprefixed W3C syntax */  
  33.   background-image:  
  34.     linear-gradient(top,  
  35.     #800000 0,  
  36.     #0000ff 37%,  
  37.     #000404 83%,  
  38.     #000000 92%,  
  39.     #000404 98%);  
  40.   /* Old WebKit syntax */  
  41.   background-image:  
  42.     -webkit-gradient(linear,  
  43.     #800000 0,  
  44.     #0000ff 37%,  
  45.     #000404 83%,  
  46.     #000000 92%,  
  47.     #000404 98%);  

This will result in an output similar to the below:
 
img2.jpg
 
In all the examples so far, we have used gradients with background colors. However, the use of gradients need not be confined to background alone. You can use them in borders, lists, and other elements wherever applicable. However, as of now, browser support for such borders is limited.
 
Of course, if you are too lazy or don't have time to sit and churn out the various codes required to create a gradient, you can use ready-made tools.

Conclusion

 
This article explored further ways of working with gradients.