in css by fyd on 02 Apr 2008
A quick and simple way to use a gradient background for you web site. Make the gradient look like it is fading out with no weird lines - just smoothness. It's really quite easy to do using CSS to edit the body property of the HTML document.
We will start off with the HTML. So, open a new document - I'll be saving mine as fade.html. Get the basic HTML shell written out and go ahead and add a link to a stylesheet - style.css. Now here is how we will layout the page for presentation purposes.
<html> <head> <title>Gradient Background Fade</title> link to stylesheet <link href="style.css" rel="stylesheet" type="text/css" media="all" /> </head> <body> <div id="main"> main container <p>Place holder text</p> <div id="leftSide">left demonstration </div> <div id="rightSide"> right demonstration <h1>Right Section</h1> </div> </div> </body> </html>
Note: if you copy and paste this you need to take out the 'main container', 'left demonstration', 'right demonstration' text - they are not to be in the code they are just comments I made.
Just a quick layout to show a couple uses of the gradient fade. Before we get to the CSS we need to make(or get) an image for the background. You can use Photoshop to create a simple gradient
In Photoshop create a new file - I'll make mine 10px wide by 400px high.
Pick the colors you want to use for your gradient - Take note of whatever the bottom color of the gradient is - we will need it later. If you double click on the color in the Tools pallet you can copy the hexadecimal number (# followed by six digits). Fill your file with the gradient and save it. Or right-click and save the images I used.

Now to the CSS
body
{
/*the color here #2c2721 was the bottom color
of my gradient image - a seamless fade*/
background: #2c2721 url(img/bg3.gif) repeat-x;
}
/*just a container to hold things*/
#main
{
margin: auto;
width: 700px;
height: 600px;
background: #ffffff;
}
#main p
{
padding: 10px;
}
/*the area to the right with a gradient fade*/
#rightSide
{
float: right;
margin-right: 10px;
height: 500px;
width: 200px;
/*#ffffff is the bottom color in gradient*/
background: #ffffff url(img/rightbg.gif) repeat-x;
}
#rightSide h1
{
color: #fff;
margin-left: 5px;
}
/*left area with fade example*/
#leftSide
{
float: left;
height: 500px;
width: 200px;
/*#ffffff is bottom color in gradient*/
background: #ffffff url(img/bg.gif) repeat-x;
margin-left: 10px;
}
All the extra CSS is just formatting to show the examples. The background: is the property we need to worry about to make the gradient fade work. Use repeat-x to make it show up all the way across the page, but not repeat down the page.

Download HTML, CSS, and Images
Not the prettiest example, but you get the idea - pick the colors you want and make it look good.

Popularity: 100% [?]



April 24th, 2008 at 10:24 am
This was VERY helpful for a project I am currently working on- thanks for the well-written directions!
June 24th, 2008 at 12:41 pm
Great example, clear simple and well written. Thanks for supplying the supporting files!
December 16th, 2008 at 11:32 pm
Hey, I tried your example but didn't turn out ok. My background is black and I have the centered block on the page, but the gradient blocks don't show up. So I'll check my code against yours and see where I went wrong. But this has helped me greatly. I've been trying to get a really good css gradient tutorial that makes it seamless instead of seeing the weird lines. I'll come back and comment when I am successful.
March 15th, 2009 at 4:02 pm
i tried this and i couldn't get it to work? i get a white page with the text from your example posted top to bottom, looks like no formatting?
where do we put the css page when we save it to our hd?
March 19th, 2009 at 12:59 am
thx… it really helpful for my project..
thx
June 24th, 2009 at 7:41 am
Excellent post. I've found that gradient backgrounds generally look very nice, even though many people don't understand how they're made. Thanks for simplifying things.
August 12th, 2009 at 8:24 pm
This looks great-easy to follow.
Just wondering if you can tell me what the advantage is over dropping a small gradient JPEG into the background of a table cell?
Does CSS create a faster download?
August 27th, 2009 at 3:55 pm
Excellent tutorial! Straight-forward and easy to follow - very good work.
This is an elegant effect for sits that require a lot of vertical-scrolling, and you can make the gradient-graphic longer if the site will need it.
October 26th, 2009 at 9:49 am
Wow…superb tutorial even for a web developer like myself. I've always wanted to learn how to do this cool effect. Thank you for simplifying the solution to this. Best of all, it's clean and using good practices. Thumbs up, waaay up!
November 9th, 2009 at 11:01 pm
Hey we need Gradient angle 45 Degrees so pls give me that kind tutorials
Thanks
Mcraan
January 4th, 2010 at 11:58 am
Very helpful. Easy to understand and implement. Thank you!
January 22nd, 2010 at 4:41 pm
the way this was written was 136798735542343434123X better than the others i was seeing… i understood this one
January 30th, 2010 at 7:15 am
[…] CSS/HTML Help Will this one give you what you need, Josh? Tom (… who's fading in and out this morning himself!) […]