Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Creating a gradient background

From: CSS: Gradients

Video: Creating a gradient background

All right, so now it's time to jump into a couple of our mini projects here. And the first project I'm going to walk you through is how do we create a gradient background for our web pages that we work on? So I've got an empty CSS document for you, and I've also got an index document which I've got open inside of Chrome. You may also want to open this up in other browsers like Firefox, and Internet Explorer, maybe even Opera, just to see the differences between them, and see exactly what works best for you. Now let's go back over into our CSS document. And so if I wanted to add a gradient that goes across the entire page, my first reaction would be just to type out something like the body tag, and then just start typing up the gradient.

Creating a gradient background

All right, so now it's time to jump into a couple of our mini projects here. And the first project I'm going to walk you through is how do we create a gradient background for our web pages that we work on? So I've got an empty CSS document for you, and I've also got an index document which I've got open inside of Chrome. You may also want to open this up in other browsers like Firefox, and Internet Explorer, maybe even Opera, just to see the differences between them, and see exactly what works best for you. Now let's go back over into our CSS document. And so if I wanted to add a gradient that goes across the entire page, my first reaction would be just to type out something like the body tag, and then just start typing up the gradient.

So in this case, since I'm working in Chrome, I'm only going to target webKit for now. I can go back in and fill in the other browser prefixes later. Let's do background and then we'll do a -webkit-linear- gradient(), and inside of my parentheses I just want to do a couple of colors. Let's do a light gray, so #ececec. That's a really light gray color. And then, we're going to fade that into, let's say, kind of a mid-gray. So we'll do #666; should work fine. So I want this to go from top to bottom. So I'm going to define top at the very beginning, and put a comma, and then we'll finish that off with semicolon.

And now when I do this, I'll save it, and look what happens when I go into the browser. You would think that this would automatically just generate a nice smooth gradient for me. But when I do this, I get something that looks like this, almost like playing Minesweeper back in early 1990s. And it's not what I want. So how do I fix this so that the gradient fades gradually from one point to another? There is a couple of different ways to do this, and there seems to be some debate online as to which one works the best. I'm going to show you both of them and how they work. Let's go into my code now. So the first method is to add a couple of different property adjustments to the body tag itself.

So I could add something like background-repeat, and set that to no-repeat, so there is no repetition of the background. And then, background-attachment, we're going to set that to be fixed. So when I do this and save it, I can go in, and I get a nice gradual fade from top to bottom. Now another school of thought would be that we simply remove those and do something like the HTML tag style to 100%. So if I do that, if I remove these and then go back up here and add HTML, a new declaration for that, and we'll just call this height and set that to 100%, and save it.

Now if I go over and refresh, you can see there that it's basically the same thing. Now there are some schools of thought that one causes more of a problem than the other. And what I suggest you do is just play around with this in all of the different browsers that you want to test with, and whichever works the best in most of the browsers, I think that will be the best solution for you. I've read some reports that using the height at 100% causes some problems in Internet Explorer. So if that's a big deal for you, you might want to think about just removing that portion of this, and going with the background-repeat and background-attachment portions of that.

And as you can see, when I save that and refresh it, it does the exact same thing. So again, if you want to create a gradient that goes from top to bottom and fills up the entire full screen background of your page, this is the way to do that. Just style the body tag with a background property, use your linear gradient syntax that we talked about throughout the course, and you can then go in and you can add all of the different syntaxes for each individual browser. So we could do this a few different times here, and put in moz for Mozilla, and then ms for Internet Explorer, and then finally we would do o for Opera.

So that should take care of all of our different browsers that we need. We've also got background-repeat and background- attachment appended there at the bottom to help with the full screen issue, and then when I go back in and refresh, everything should be good to go. So take some time, play around with this, adjust your colors, adjust your positioning as you see fit, and I you think you'll be really happy with what you're able to create without having to use any images whatsoever.

Show transcript

This video is part of

Image for CSS: Gradients
CSS: Gradients

23 video lessons · 8851 viewers

Justin Seeley
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed CSS: Gradients.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.