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.
One of the things we haven't talked about yet is how do we mix in transparency with gradients, and so, in this example we are going to be taking a look at a page that's styled with the background of just a gigantic image. And so I will jump over in to the web browser and take a look at this image, and so basically what I want to do, is I want to actually simulate some sort of like dark effect that sort of just covers the top part of this and fades gradually into the image, making the image a little bit more dramatic, if you will. This is something that's easily accomplished in something like Photoshop, but in some cases you might not have access to that, but you might just want something that loads a little bit quicker and that you can do on-the-fly and you can change at a moment's notice, and so that's where gradients come in really handy And so what I am going to do is go back over into my code editor for a moment, and what we are going to do is we are just going to make some changes to this.
And so the first thing I am going to do is I am going to take this URL section of the background element. I am just going to push that down to another line, and then we'll just tab it over so that it meets right here where my normal arguments would go. And so what I am going to do here is just type out webkit-linear gradient. Of course if you are working in Mozilla or any other browser, you'd put that prefix for moz, ms or Opera with an o in front of that, but in this case since I am just working in webkit for now for demonstration purposes, I am just going to use the webkit syntax for now.
So webkit-linear-gradient, I want this to first start out as black and then I want that to go to transparent, and let's move that to 25%, so that way it moves 25% along the width of the gradient, and put a comma after the parentheses and then we've got our URL, and at the bottom here these are very important to have on there as well, background-repeat, no-repeat, background-attachment: fixed. Both of these will help to make sure that the gradient just goes from top to bottom and does not repeat several different times down the page.
So once we have that done, I am going to go back over into my browser and refresh, and watch the top here. See that gradient goes right there and the transparent part actually starts 25% of the way down the height of the browser. And so I can go back in now anytime I want without having to go back in Photoshop or anything else. I can go back in and I can simply change where that gradient starts to change what this image looks like. So if I go back over into this code window and change this to something like 70% and save it and go back and Refresh, watch what happens to the image; much more gradual fade.
A very nice effect on the image itself. Let's say for instance that the client comes back to me and says "you know what, we don't like how dark the image is at the top." That's okay, I don't have to go back into Photoshop, I'll just change this from black to white or whatever color they want, and so that would change like that. Now you will notice the middle of this looks a little muddy and that is because when you type out the transparent property right there, it's actually referring to transparent black. So it's giving you an RGBA value of 000 and then an alpha value of 0 as well.
So in this case, what we are doing is actually fading white into a transparent black which is accounting for that muddy texture in the middle. Now if you wanted to, you could change this to something like a blue, something like that. Refresh and you can kind of see what it does. You could change this to suit the mood or the look of the photo. Whatever you wanted to do. And another good one would might be something like orange. Let's try that out. Refresh, there we go. Kind of gives us sort of a dusky feel. But in any case, anytime you come back in here you can easily change both the color and the position of the gradient.
You can also change the background if the image falls off or runs out of space, you can change the background color for that, if I wanted that to be black if it was on a larger monitor or something like that. You can also change the image anytime you want, change the position of the gradient, whatever you want, just come back and change that via the code and then refresh your page to see exactly what has happened. And so using transparencies and gradients are actually a great way to simulate some effects that you would otherwise have the jump into an image editing application like Photoshop or even Fireworks to create, and in this case its going to save you a lot of time, and it's going to save you some load time on your pages as well.
There are currently no FAQs about CSS: Gradients.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.