Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Creating a gradient background


From:

CSS: Gradients

with Justin Seeley

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.
please wait ...
Watch the Online Video Course CSS: Gradients
1h 13m Beginner Feb 15, 2013

Viewers: in countries Watching now:

Join Justin Seeley as he introduces gradients, a somewhat hidden strength of CSS that allows you to add depth and texture to your web designs while reducing load times—creating a better viewing experience for your visitors versus static images. Discover how to create simple linear and radial gradients; apply them to backgrounds, buttons, and text; and write browser-specific syntax to ensure your designs look the same across multiple platforms. The course also shows how to create metallic textures, diagonal gradients, and repeatable patterns. Along the way, Justin demonstrates real-world uses for gradients and techniques to extend them beyond the basics.

Topics include:
  • Exploring browser compatibility
  • Creating horizontal and vertical gradients
  • Adding extra color stops
  • Exploring radial gradient syntax
  • Using transparency
  • Creating fallbacks
  • Generating gradients via the web
Subject:
Web
Software:
CSS
Author:
Justin Seeley

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.

There are currently no FAQs about CSS: Gradients.

 
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

* Estimated file size

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

Upgrade to View Courses Offline

login

With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

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 ?

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:

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Start your FREE 10-day trial

Begin learning software, business, and creative skills—anytime,
anywhere—with video instruction from recognized industry experts.
lynda.com provides
Unlimited access to over 4,000 courses—more than 100,000 video tutorials
Expert-led instruction
On-the-go learning. Watch from your computer, tablet, or mobile device. Switch back and forth as you choose.
Start Your FREE Trial Now
 

A trusted source for knowledge.

 

We provide training to more than 4 million people, and our members tell us that lynda.com helps them stay ahead of software updates, pick up brand-new skills, switch careers, land promotions, and explore new hobbies. What can we help you do?

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.