HomeОбразованиеRelated VideosMore From: Filip Delac

How to Vertically Align Text in CSS

14 ratings | 1816 views
Hi, vertically align text in CSS? No problem, I prepared five methods to use when you want your text to be centered. Which one is the best? Follow the wisdom of Tech Tzu: https://twitter.com/Tech_Tzu https://www.facebook.com/profile.php?id=100013382105892&fref=ts Project Resources: http://www.mediafire.com/download/j5xc5sr6wst27dl/Project_Files.7z Tools: Brackets, FREE code editor http://brackets.io/ Emmet, super speed coding addon http://emmet.io/ Beautify, turning your ugly code into beautiful script https://github.com/brackets-beautify/brackets-beautify Transcription Hello hello, and welcome to another tutorial. Today we will learn how to vertically align text in CSS using five methods. Method one is a classic one, it will only vertically align one row of text so to see this let's make one div with the class container, it will be container one and one p tag with the class text1. Okay, and let's add some text inside Lorem Ipsum... and let's trim a bit so we have only one line. And the in our main CSS we will add some styling for our container1, we will add a background color of white, just to better see in our preview pane on the right side. And this is very simple method, so just use on your text text1, just use just use height of a 100 pixels, for example, and line-height, height of 100 pixels, and as you can see this is, our text is in the center, but if we add another line of text over here, it will break. As you can see over here, So this works only for one row. Okay, I just add the method2, method2 will be the upgrade of a method one so we just add again our container and text paragraph. Okay, just copy this from the method1, and paste it down there and change the number two and text two, so it will be easy to do work with. In our CSS we got our container2, container2, and text2. As you can see, we will now add to the container to this, this code will add height and line-height of 100 pixels and as you can see, our text is in the center, aligned in the center, but as you can see if we also add one additional row it will break, so, to be able to add additional row we need to add to our text2 display in-line, in-line block, and this say: vertical align middle, as you can see, nothing still happens because nothing will happen until you add a line-height: normal. And as you can see on the right side, our method works, and it's in the center so let's check with another row, just say Lorem... Tad-da :) as you can see our text is the in the center again. In method three we will use table illusion to vertically align our text. So, just copy this code up here, your container and text2, and paste it down here, and just change the the numbers here 3, and to do this we need to set for our container3 to display a table, table, and now we said to our container to act as a table is a table. We can say our height is 300 pixels and the background color to be to also be white. And on our text3 we add display table cell, as you can see. We now treat our text3 as a table cell, and the only thing you need to add is a vertical, vertical-align: middle; and this is it. You can change the height over here for 100 pixels and so on. As you can see our text is in the middle, vertically aligned perfectly, and we can have multiple rows. Method 4 is my favorite one next to a method five, that's why I left them for the end of this tutorial, and because I like them so much because they need so little coding, so, let's for example, add in our index.html let's just copy paste, as usual, o our code, and paste it down there change this to number four, so we don't have to write all over again, and we use padding. So, it will be just in our container, container4, we just add a background color, just to see it better. These pretty white color, and just add padding. Padding-top and bottom padding to be the same, for example, 50 pixels, and the right and the left to be zero, and as you can see, our content is centered! It is my favorite method because next to a flexbox it's the easiest method and it works everywhere. As you can see, you can also add this padding in percents, for example, 5% so it will be always responsive. Method five is a flexbox, it's my personal favorite next to padding.
Html code for embedding videos on your blog
Text Comments (0)

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.