Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,987 courses, including more Web and personalized recommendations.Start Your Free Trial Now
- View Offline
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.
- 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