This lesson is exclusive to members

Responsive Web Design Essentials - HTML5 CSS3 Bootstrap

How to vertically center content in a html div tag using flexbox 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 is about centering vertically. Super easy using Flexbox. This text here is inside this larger purple color here, and it's centered vertically, same with this image, it's kind of hovering here in the middle. Let's jump in now and work out how to do it. 

Remember, in the last video we kind of got this centered, we put padding at the top and the bottom to kind of fake it. So what we're going to do now is use Flexbox because it's super easy to do. So what we want to do-- where's our other mockup? We want the content - I want to delete these little boxes I added. - to be kind of centered in this kind of Main Div here. Now there's a couple of things you need to do. First of all, the Main Div that it's going inside of, so these two little guys who are inside, this Main Div needs a height, Min-height. It's got a Min-height of 300, which is going to work perfectly, and then what we need to do is we need to tell it to do something. 

We want to use the Align items, Align items to the center. Let's save it, let's check it out, cool, huh. By default-- we saw in the last video, if you don't, if you just turn the Flexbox on it kind of stretches to fit its parent. Remember that, let me just comment that out. I'll save it actually. Remember, our content is only that big but it stretches out. So you can kind of override it by doing different things. So we're saying, actually go center, so it assumes, all right, I will give up all the extra stuff, and will just be right in the middle here. 

Now this is the one to bookmark, this is by far the one I get emailed the most about. It's like, "Hey, how  did you center in the middle again, vertically?" Yeah, it's hard to remember to do, but anyway. How would you remember? Either bookmark this video, or in your cheat sheet, you kind of just go through this, and you have a look, and you're like, "Is this what I want?" Justify Content to the End, that's not what I want. Justify Content Center, it's an interesting one, because it centers all your content. Most people don't use that, they'll just do text-align:center. We did that earlier for our image, that works just as good, but anyway. Space in between, we did that, no. So you just kind of scroll through, and I love that this is all visual, because I'm like, "No, oh, look at that." See, we could do align, flex-end. but we're doing center, this test flex-end, just because. 

So our parent tag, let's do flex-end. I can do flex-start, flex start is by default. Cool, flex-end. Flex-start would be useful if you didn't want it to stretch out, let's have a look. You see, it goes to the top without kind of filling up the rest of this space. I'm going to undo that. There's a couple of things we want to do before we go, just to tidy up, because we just got placeholder text. Before we do that let's just recap, and for this to work it needs a parent. 

So these two girls in the middle here, or guys, they are my little children, and I want to do the effect to them, so you apply it to the parent. So you don't have a parent, and say you're watching this video later on, and you're like, "My website doesn't have a parent tag," just put one around. Just wrap it around and say, call it Wrapper tag, and all you're going to do is apply a height to it, and you're going to apply, where is he? Display Flex has to be on. We turned ours on by default in the last video, so if you're just watching this video, that has to be on as well. So it needs a height, display flex, and then align items to the center, and everything inside of it will do that. 

What do I want to do now? I want to start removing some of my colors. So, where is it? I want to get rid of the main color. Save it, we just keep testing this as we go along, and see the background, that's the color that we used for the container. I'm going to get rid of that now as well. If you're finding them not very useful anymore, we're going to start getting rid of bits. What else? We'll get rid of the blue and-- it's a very high color to see that one. Corn flower blue and mint green, cream; applying the cream. Cool, because that's what I want. 

We're going to fill in the background in a little bit  with this big background gradient and this image. Next I want to replace out the text here and the image. So the text is easy, it's in your Exercise Files. There's one in there, Project2, and there's one called Text; where is it? Project2 - Text. And in here, Nav - Mobile Bike Repairs. You could equally type that in. Save it, and this is the Body copy, kind of a bit underneath the P-tag. Make sure, when you are doing this, I often will accidentally lose, see that last angle brackets, and I do this, and then it all goes wrong. Just be careful. 

You can see, it's put in this weird brake in here, because I copied and pasted it from this-- because there's a soft return in there. It won't matter when it goes through to the other side, just kind of ignores it. You might not like that though, you can just tidy it up if you want. And if you want to be on two lines, that's a good point. We've never covered that, so if we want to be on two separate lines, because it won't, it will just be on one big line, even if you put a Return in, or ten Returns, it doesn't matter. So what you need to do is put it into two individual P-tags. So I'm going to cut that out, let's try and make it nice and clear. There's that guy. All right, we have to stop. 

So I need another P-tag in here, so P, I'm going to paste it in this. There you go, two separate tags. We're going to replace this text with an image; so where are we? I don't want any of that in there. We're going to put in an image tag, so 'img'. And where is it coming from? We haven't actually put it in our local folder yet, so you remember, in our Exercise Files there's one called, I have no idea what it's called. Wait there. There it is there. We're going to use this one here, the Service-A. Leave this one for the moment, we're going to look at that later on. Just use this one, Image Bike Repair - Service A. 

I'm going to copy it. Oh, A, actually no, use the one without the A. It doesn't really matter but, humor me for the moment, use this one. We're going to look at these when we do Responsive Images in a little bit. I'm going to put it in my Desktop, in my Project2. I want to put it in the Images folder. So a new folder in here, click Images, open it, paste it in. Don't worry about that, I'll remove that in a sec, he's not meant to be there, hang on. There you go, it's gone. And now, in VS code, we should be able to start images/, and there it is there. 

Don't forget to put in some Alt text. Remember the Alt text is what gets loaded, and what-- if the image doesn't load, but mainly is what Google likes to see. And I put in a person servicing a bike in Dublin, because it really does explain the photograph, but really I'm stuffing good keywords into my website, so that I can potentially rank better. So that's it, let's have a little look at it now. We got an image and we've got some text that we need to format, which we'll do in a little bit. It's looking terrible, Dan, let's-- it's going to get better, I promise, but for now that's it, I will see you in the next video.