break free by learning
  • Home
  • About
  • Free Stuff

CSS Gradient Background Fade

Apr 02, 2008 | css | fyd

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.
gradient gradient gradient

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.

The HTML
The CSS

download
Download HTML, CSS, and Images

Not the prettiest example, but you get the idea – pick the colors you want and make it look good.
example

Popularity: 100% [?]

Related Post:

  • CSS Custom Search Field or Textfield (38)
  • CSS Horizontal Menu (0)
  • CSS Two Classes for one Item (0)

19 Responses to “CSS Gradient Background Fade”

  1. AE Conlon says:
    April 24, 2008 at 10:24 am

    This was VERY helpful for a project I am currently working on- thanks for the well-written directions!

    Reply
  2. Am says:
    June 24, 2008 at 12:41 pm

    Great example, clear simple and well written. Thanks for supplying the supporting files!

    Reply
  3. soy says:
    December 16, 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.

    Reply
  4. R H says:
    March 15, 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?

    Reply
  5. Nico says:
    March 19, 2009 at 12:59 am

    thx… it really helpful for my project..

    thx

    Reply
  6. Affilliate Marketing says:
    June 24, 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.

    Reply
  7. SJ says:
    August 12, 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?

    Reply
  8. ArkyMark says:
    August 27, 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.

    Reply
  9. Jose says:
    October 26, 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!

    Reply
  10. Mcraan says:
    November 9, 2009 at 11:01 pm

    Hey we need Gradient angle 45 Degrees so pls give me that kind tutorials

    Thanks
    Mcraan

    Reply
  11. Elizabeth says:
    January 4, 2010 at 11:58 am

    Very helpful. Easy to understand and implement. Thank you!

    Reply
  12. Jeff says:
    January 22, 2010 at 4:41 pm

    the way this was written was 136798735542343434123X better than the others i was seeing… i understood this one

    Reply
  13. CSS/HTML Help - Disney Secrets Forums says:
    January 30, 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!) [...]

    Reply
  14. ARMAND says:
    April 29, 2010 at 2:02 am

    how did you make your header..there's a shadow in the sides..please help

    Reply
  15. logan says:
    May 3, 2010 at 12:26 pm

    good to see theres still some people who write good content!

    Reply
  16. fyd says:
    May 5, 2010 at 1:34 pm

    @ARMAND – it's just art I made in photoshop – the image: http://freeyourdesign.com/images/wrap_bg.gif and used css to position it and the header graphic. I also have a background image that repeats horizontally across the top to match the wrap_bg.gif – background image: http://freeyourdesign.com/images/bg.gif

    Reply
  17. Deepti says:
    September 27, 2010 at 5:17 am

    ITS WORKING THANX A TON

    Reply
  18. Mn8 Multimedia says:
    December 19, 2010 at 2:40 am

    Thanks for the CSS tutorial, it's very helpful.

    Reply
  19. sangan says:
    June 30, 2011 at 2:03 am

    Its working, Thank you.

    Reply

Leave a Reply

Click here to cancel reply.

Categories

  • css (8)
  • freelance (2)
  • illustrator (1)
  • inspiration (4)
  • jquery (1)
  • photoshop (9)
  • resources (8)
  • site news (6)
  • the basics (4)
  • web design (1)
  • whats good (1)
  • wordpress (1)

Recent Posts

    Archives

    • April 2010 (1)
    • April 2009 (1)
    • May 2008 (3)
    • April 2008 (14)
    • March 2008 (8)
    • February 2008 (8)
    • January 2008 (11)

    ©2012 freeyourdesign | Designed by Michael Hargis | | twitter icons