Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now on the surface using the Line Height property seems pretty simple. Line Height controls the spacing between lines of multiple lines of text. If you're a graphic designer, you're probably used to calling it leading. It is exactly the same concept. Line Height can get pretty tricky however when you have to consider how the values are calculated and if Line Height is being inherited or not by child elements. So for this exercise, we're going to be using the line-height.htm file, which can be found in the 04_13 directory.
And I'm just going to Preview this in a browser before we begin working with it to sort of show you what the page structure is going to look like. So we have a couple of headings followed by paragraphs, and we have a span tag right here that has a background color that's going to help us illustrate exactly what Line Height is doing for us. Now, if I go back into my page, you can sort of see that structure within the code. So we just have some headings followed by paragraphs. We're also going to notice that we have, in this h2, a class called center, where we're going to vertically center the text and the element using Line Height as well. Okay.
So I'm going to go right up to my existing selectors and I'm going to find the paragraph selector, and we're going to play around with some of the initial values of what we can do with Line Height. So just below font-size, I'm going to create a new line for another property and here I'm just going to type in line-height, so it's line-height. Now, remember, from earlier when we were talking about the font shorthand notation, this is a property just like font-weight, font-size, font-style, that can be added to the font shorthand notation. All right. So the values that we have here, one of the possible values is normal, so let's just use that as the first value.
So if I say normal, I Save this and I go back into my browser and Refresh it, we're not really going to see any difference. That's sort of the default. Now, every browser is a little bit different, but typically the Line Height is 1 to 1.2 times the size of the font. So we can also use a specific number, a length, or a percentage. Let's take a look at what each one of those does for us. If I come in and do a percentage, let's say I do 140%. So if I Save that and test it, now what it's doing is it's basically taking a look at the size of the text, and then it's giving you a Line Height that is equal to 140% of that.
You can see that it opened up the spacing a little bit, but not a lot. All right. Now, we can also use a length value. If I use a length value, let's say I use something like, I don't know, 1.4ems, all right, so if I Save this and test it. Wow, no movement whatsoever. So 1.4ems is very similar to 140%. Really, a lot of times you're just kind of splitting hairs between one unit and the next. And then we have a third way that we can write this syntax. We can just go ahead and get rid of the unit of measurement altogether.
Now, Line Height is unique in the fact that it is the only property that allows us to do multiples like this and not a unit of measurement after that. Essentially this means 1.4 times the size of the text. If I Save this and test it, you've probably already guessed this, no change whatsoever. 1.4ems and 140% all pretty much render the same. There are really three different ways of writing a very similar value. However, there is a big difference and a big value in terms of how this property is inherited, in terms of how you write that.
So it does matter and we're going to get into that in just a moment. First, I want you to be able to visualize Line Height a little bit better than we're currently visualizing it right now. So here's what I want to do. I want to go down to the span rule, and I'm going to change it's value to display or actually add to its value the display property. Now, the display property is awesome and it's something that you'll learn more about in the CSS Layout course. But the display property allows you to change how an element is displayed within the browser. Most of our elements are either block- level elements, meaning they occupy their own space in the document flow, or they're inline elements, meaning they can appear side-by- side like images and spans.
Well, there's other values that we can use too. And here I'm going to use inline-block; Now, what that does for me is, it means that the span element should still behave as an inline character, but I want it to display its properties like a block-level element. Now what that's going to do for me is it's going to allow it to display its full height. So if I Save this and go back into my page and Refresh it, you can see there's a slight difference there. So let me go ahead and kick up the line- height a little bit more, let me take it up to 1.6. There we go. Now we can really see the difference. So what you're seeing here is you're seeing the exact line-height of this line of text.
You can see it extends down a little bit and extends above it and is based on the ascenders and descenders of the characters. So it just simply uses the baseline as sort of that starting point, and then it goes above and below based on the values that you give it. So the higher that value, obviously the higher that is. And if you could imagine these being boxes stacked on top of each other, the height of that box is controlling the space between the lines. So that helps me sort of visualize exactly what's happening here. You can even take that further, if I went down into my span and I did just that span element.
So if I did a line-height here of say 2 .6, that's really going to make a huge difference, trust me. If I test that, you can see now how different this line is compared to all of the other lines. And if you do have something like this where you have the span tag with one line-height and you have this line that actually has another line-height, the greater value is used. So in this case, the greater value is used for that line. Okay. I'm just going to delete that property. Now, I mentioned earlier that it really mattered in terms of how you write line-height if the value is being inherited, and I want to show you that.
I'm going to remove the line-height from the paragraph and I'm going to go up to our body selector. Now, again, we've been doing this pretty much the whole course, applying properties to the body selector and then sort of letting those be inherited by the elements below it. So if I come up here and I do a line- height and let's say I do a line-height of 1.6ems and I Save this and I test it. So we go back to a consistent line-height all the way through. Now what's happening here is a lot different than using a multiple.
When you use an em, here's what happens. It takes a look at the font-size, which in this case would be about 16 pixels. And then because it's using an em, it goes ahead and multiplies 1.6 times whatever the default would be, in this case 16 pixels, computes that value and then that is the value that's passed down to every single element, every one of them. They all get that computed value. That means that this relative unit of measurement is only relative for the body, everybody else gets a fixed unit. Now, on the other hand, if I take away the em and I just do a multiple, if I Save this, take a look at what happens.
When I Refresh the page, everything changes, everything moves down a little bit, everything gets spacing that's a little bit different. Because now what happens is instead of calculating the value first and having the calculated value pass down, what happens is it passes down a line height of 1.6. So every single one of these guys now gets 1.6 times its own font-size. So that gives you a little bit better spacing in terms of the relationship between those elements. So if you're ever going to pass line- height in as an inherited value, make sure that you use multiples and not ems.
It's very, very important that you do that. So now I'm going to Save this and we're going to move on to doing our last little thing, which is using line- height to force text to be centered vertically in an element. I use this technique a lot in menus, where I have sort of a horizontal menu and I want the text vertically centered. Really, it works if there's a single line of text. If you have multiple lines of text, this is a little bit harder to pull off. So what I'm going to do is I'm going to go down, again, just to remind you of who we're affecting here. We have the heading2 that has a class called center, so that's what we're going to work with.
So I'm going to go right back up here to my styles and I'm just going to create a new selector, .center. And then inside that, I'm going to give it a color of #fff That's white, by the way, that's actually a hexadecimal notation. You could do the exact same thing by just saying white. It doesn't really matter which one you do. For background, we're going to use more hexadecimal notation. Here we're going to do #666, which is not as ominous as it sounds. It's just a gray. And then below that, here's the magic, we're going to do line-height 50 pixels.
And then I'm going to do a padding-left 25 pixels. And all the padding to the left is going to do for me is make sure that the text is not butting up against the left edge of the element, the background. But line-height, here's what this does. If you haven't specified the height of an element, its line-height is going to generate that height for you. If you have a single line of text, that means that the line-height becomes the height of that entire element. The text itself, because it's centered on that baseline, will then be vertically centered in that box, and this is a very elegant way to do this.
If I Save this and go back in the browser and Preview it, now when I scroll down, notice that we have our Other uses headline here, the text is vertically centered in the box. And if I wanted to adjust the height of that, all I would need to do is come in here and adjust the line-height. So I can go to 75 pixels, Refresh it, and you can see it stays vertically centered, and now the height of the box is adjusted. Now, again, this works best when there's a single line of text in there. When you have multiple lines, then you have to really work to keep everything the way you need it to be. Okay.
That's the basics of line-height, and I know it seems like I've probably covered a lot there, but it is an incredibly powerful property. You're going to be using it probably over and over again to adjust the spacing of elements on the page. Now, I want to really encourage you to think carefully about how you're going to plan to use line-height in your overall site-wide typographic strategy. If you place line-height on the body selector, you do determine a starting baseline grid for the entire page. However, you need to think about how you might need to modify that spacing for certain elements or page regions and how those values are going to be calculated, so really sort of how they're going to inherit that property.
And if you do that, you're going to have a consistent vertical rhythm to your text and the spacing is going to look extremely natural. Now, if on the other hand, you simply just go ahead and add line-height values to any element without really considering how it might impact other elements or work with other elements on the page, your content is going to look a little disjointed and ragged. In other words, be extremely deliberate in how you use it.
Get unlimited access to all courses for just $25/month.Become a member
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.