This lesson is exclusive to members

Responsive Web Design Essentials - HTML5 CSS3 Bootstrap

How to add a stretching background image to a website

Daniel Walter Scott

Download Exercise Files Download Completed Files

Contents

Certificates

We’re awarding certificates for this course!

Check out the How to earn your certificate video for instructions on how to earn yours and click the available certificate levels below for more information.

Introduction

I recommend hosting your new website with Bluehost, you can get a big discount by signing up with this link: https://www.bluehost.com/track/byol/byol_webess_24

You need to be a member to view comments.

Join today. Cancel any time.

Sign Up
Hi everyone, it's time to replace our boring gray background with our exciting background image. It's cool, kind of stretchy, fits in there, covers all the background, looks neat. Not sure whatever we're eating but it kind of looks tasty. The irony, it's a vegetarian, but meat looks so good, especially in these kind of photographs. Anyway, let's get in and work out how to do the background. 

All right, to add our background image, it's a little bit strange, the previous video, or one of the previous videos we added our image. We added it to the HTML, and it appeared at the top of that document here, and it's great. The only trouble with adding it to your HTML is that you can't have things over the top, or at least not very easily. So, we want it in the kind of background, and we want it to be more of an effect, rather than something you can interact with, just kind of hiding at the back. It's more like a style. So we add it in the CSS styles. We do it here in styles.css, and we add it to, which one should we add it to? 

Remember our tag that covers the whole background, that we made gray? It's the same tag. So the Body tag here, we made gray, we can leave gray there. I leave the background color there normally, because sometimes it takes a little while for the image to load. So at least there's something in the background until it loads. The way to do it, it's well named, it's called background-image; nice. The one thing that isn't very clear is what to do next, and it's URL. It's one of those options in there.
 
So URL, so background image, URL, inside of that bracket, sorry, yeah, the brackets, you type where it goes. So it's going to be in images, forward slash, '/' and as soon as you have the forward slash in the CSS, it says, "Hey, do you mean this image, the logo?" and you're like, "No, not the logo." I want another image that I've used for the background. So I've saved it for you, it's in your Exercise Files. So you need to come out to your finder. Find your Exercise Files, find Project1. There's one in here called Background. I'm going to copy it, I'm using Command C on a Mac, Ctrl C on a PC. Desktop, I'm going to paste it into this Images folder, along with the logo. 

Now hopefully I can delete the forward slash, there you go. Type it back in, and it says, "Do you mean this guy?" You're like, "I do, that's the guy," or girl. Let's put in our semicolon, let's save, and let's see if it worked. And that might be enough, you might be like, "Done, move on", but if you're like me and you-- so at the moment, kind of doesn't do exactly what I want, you might like it, but at the moment I want it to kind of move. So if somebody's looking on a smaller screen, that image is still kind of centered. There's a really nice easy CSS class that we can use. Well, not class, attribute. So it is called background, another good one, background size. And there's one here called Cover. 

So background size cover will allow us to do that; let's give it a go. Resize. Oh, nice. See down the bottom there, it repeats, because the ratio of this thing is a bit weird. The image that we've got doesn't quite fit in there, so it has to do something, and at the moment it's repeating it. So let's have a look at getting rid of the repeat. It's going to be half our problem fixed, so we say 'background repeat'. So background repeat, I'd like to set it to no repeat, please. Awesome. Let's give it a go, and it's gone. 

This great box is there, you got to decide how much that annoys you, because the next thing we need to do is a little bit kind of strange. So to make this work, what you do is, the body's the tag we've been working with, right? but there is an overarching tag over everything, called HTML. And to make that go away, you've got to put in another tag just above it, and say, "I'd like the HTML tag to have a height of 100." Height of 100%. And that fixes that, I'm not even sure why. But that's what we do. Depends, like I said, sometimes in Web Design I can explain it to you and sometimes I can't. So there you go. It's covering, it fits, it's nice.

One more thing if you're getting picky is that, it's kind of centered and stays in there, but it's not quite centered, you're like, "Why aren't you quite centered?" You can mess around with the background, what is it called, background align. Have I spelled it all right, background alignment, no it's background, man, can't-- position, that's it. So background position, and we type in center, semicolon, ';' And that should fix that. So now instead of-- it will go from the center, rather than kind of left and right. Yeah, I like it. Now one thing, if you are following along on a really small screen, you're like, "I can't even make it do it that, Dan." It's true, I just thought of it then, I'm like, if you have a really small screen it's going to be, very hard to like drag it out big enough, to make it do all the kind of resize bits. That's something to know. 

I guess what you're doing then, for like-- you probably can get away with pretty much all of that gone. The trouble will be, is when somebody like me with a giant screen opens it up, and goes, "Hey it's looking, not looking nice." So if you are working on a really small laptop, and you're like, "I can't even test this stuff," later on in the course I'm going to show you how to fake being a really big screen, but yeah, I just thought I'd add that there, just in case you're like, "Can't make that work." But it is potentially necessary. All right, background images, done and dusted.