This lesson is exclusive to members

Responsive Web Design Essentials - HTML5 CSS3 Bootstrap

How to nested divs inside of each other in HTML & CSS

Daniel Walter Scott

Download Exercise Files Download Completed Files



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.


I recommend hosting your new website with Bluehost, you can get a big discount by signing up with this link:

You need to be a member to view comments.

Join today. Cancel any time.

Sign Up
Hi there, this video we're going to talk about Nesting Div Tags. What that means is we're going to end up doing this, where we have our original yellow and pink box, and we put other boxes inside of it. It's called Nesting. It's not that fancy. You can see here, boxes inside of boxes. They end up looking like this, in the end we're going to put, text inside boxes, which are inside of boxes. Yeah, let's do that now, I'll show you in VS code; let's go. 

All right, what we're doing is, in your Wireframes folder, in your Exercise Files, we're looking to make this one here, HTML Div 2. So, same boxes. We'll use the same thing we've created so far. I'm going to put two boxes inside, a gray box and a purple box. Those are the heights, let's work out how to do it. 

So in VS code, we're going to do what's called Nesting. Just means we can put Divs inside of Divs. So we have one Div here, and a second Div. Let's work on that first one, which is going to be our gray, 100 x 300. So where does it go? It depends on where you want it, I guess. We've got this text in here, we don't really want anymore. That was just, I guess--

I put that P-tags in there to show you how we could highlight, and just so the box will have some content in it. Let's delete that for the moment, just to make it nice, and clean, and clear. So what we're going to do is put a Div, say just where that P-tag was. So if we go, angle brackets, div, '<div, Now we're not going to make a Div, then create a Class up here, then come back and name it, we're going to do it all in one go. So instead of just closing it off here now - I'm going to undo that - I'm going to put in 'class', before I finish it. Now I'm going to call this one 'box3', then I'm going to close it off. Just to save some time, rather than coming back and doing it later. 

I'm going to put -- do I put anything in it? No, just going to leave it empty, but remember, this is where it would go inside of that Div. Let's leave it empty. Let's save it, and it's not going to appear yet. 'Refresh', the text has gone, and there are Div tags in there, but I can't see it. So we need to style it. We do not need this anymore, because I got rid of it. I'm going to call this one '.box3', curly braces. And we're going to steal all of that. We're going to pick gray, which is spelt both ways. Wow, I get caught up with that every time. Gray, gray, and gray, I don't know why I find that amazing. It's the same gray, just spelt in different ways. 

This one was a width of 100, and a height of 300. Here we go. Well, 3000. That's not going to work. Let's save it, and let's check it out, let's see if we made it work. 'Refresh', look at that. So often when you're making a website, you'll make kind of like an overall website background, a big container, and inside of that container you'll nest lots of different Div tags. Like the Header, the Navigation, the images, and carousel, and all those things; that's Nesting. 

Let's do it with the second box, and I want to show you just a couple of things, like if you don't get it in the right spot, so let's delete that P-tag from here. That's where it should go a bit, let's say we do it just outside of here. So let's go, angle brackets, Div, space, Class, Return. This one's going to be called 'Box 4'. Close it off with the angle bracket. 'Save'. At the top here I'm going to copy and paste the whole thing; real lazy. 'Box 4', this one was purple. Purple. And it was 120 x 350. Well, it's actually 350 x 120. Height at 120. Let's give it a look, and preview in the browser; awesome. Well, not awesome, it's in the wrong place. 

So if you get it in the wrong place, it's really simple, in the code to go, actually you, 'Cut', and you, 'Paste'. I'm going to line it up, so it looks nice and all aligned. Hit 'Save', and now, 'Refresh', it should be in there; awesome. Now if I want say an H2 inside of that purple text, I could just put it here, there's nothing wrong with typing H2, closing it off, and typing it in here. 'Heading 2'. That's perfectly, grammatically, and syntactically - if that's a word - correct. Let's give it a preview, it's in there. 

What ends up people-- what ends up people doing?! What I like to do to make everything look nice, is put Returns in between all of this, to really expose the nesting, so I know that there's a Body tag. Inside of that is Box 1. Inside of that is Box 3, you can kind of see the indentation. Same with this, Box 2, and inside of that is Box 4, and inside of Box 4 is H2. Does that make sense? 

All right, so we're getting a hang of this Div tag division of space thing, and putting boxes inside of boxes. Let's get on to the next video where we set our class project. Homework time.