CSS Line-Height
CSS Line-Height
LINE-HEIGHT
What is leading?
Back in the good old days type was set by hand using printing presses.
Printed material was created by setting out letters in rows. Each letter was created on an individual block.
Leading, or lead strips were added between the lines of letters when additional vertical space was required.
However,the terms leading and half-leading are still used in association with CSS line-height.
By default, browsers use between 1.0 - 1.2 line-height. This is referred to as an initial value.
You can override this default lineheight using the CSS line-height property.
p { line-height: 140%; }
p { line-height: inherit; }
p { line-height: 120%; }
Shorthand line-height
These five line-height values can also be specified using the font shorthand property.
The line-height value is written in conjunction with the font-size value. The values are separated by a slash: <font-size>/<line-height> For example
Normal value
Percentage value
Length value
Number value
Calculating line-height
This is designed to make it easier for authors - so they do not have to specify properties for all descendants.
EG. the color property is inherited. If a color is applied to the body element, it will be passed down to all other elements on the page.
To see the various line-height options in action, we will use the following HTML code:
<h1> consect etuer adipi scing eli </h1> <p> Lorem ipsum dolor sit amet co </p> <div id="footer"> Duis autem vel eum iriure dol </div>
h1
div#footer
Descendant elements
body { font-size: 16px; line-height: XXX; } h1 { font-size: 32px; } p { font-size: 16px; } #footer { font-size: 12px; }
The percentage value (120%) and the body elements font size (16px) are used to create a calculated value (16px x 120% = 19.2px). This calculated value is inherited by descendant elements.
All descendants, regardless of their size, receive the same calculated value line-height.
element body h1 p #footer font-size line height 16px 32px 16px 12px 120% inherits calculated value - 19.2px inherits calculated value - 19.2px inherits calculated value - 19.2px calculated line-height 16px x 120% = 19.2px 19.2px 19.2px 19.2px
Again, the line-heights do not scale with the relevant font size.
In this case, the normal value rather than a calculated value is inherited by descendant elements. Browsers may interpret the actual normal value in slightly different ways.
All elements now have line-heights that are relative to their font-size.
element body h1 p #footer font-size line height 16px 32px 16px 12px normal normal normal normal calculated line-height 16px x approx 1.2 = approx 19.2px 32px x aprox 1.2 = approx 38.4px 16px x approx 1.2 = approx 19.2px 11.2px x approx 1.2 = approx 13.44px
OK
OK OK
But what if you want the flexibility of the normal value, but to be able to specify the factor used? This is where number values come in.
In this case, the factor (1.5) rather than a calculated value is inherited by descendant elements.
All elements now have line-heights that are relative to their font-size.
element body h1 p #footer font-size line height 16px 32px 16px 12px 1.5 factor of 1.5 factor of 1.5 factor of 1.5 calculated line-height 16px x 1.5 = 24px 32px x 1.5 = 48px 16px x 1.5 = 24px 12px x 1.5 = 18px
Too loose?
OK OK
Generally, a number value is the best method for setting line-height as line-heights will then always scale with the relevant font-size.
It is hard to determine a perfect line-height as each situation is different. However, it is safe to assume that headings can have less relative line-height than paragraphs of text.
For example, all content could be set to 1.5, and then headings redefined to 1.2.
body { line-height: 1.5; } h1, h2, h3, h4, h5, h6 { line-height: 1.2; }
The Web Content Accessibility Guidelines (WCAG) 2.0 state: line spacing is at least spaceand-a-half within paragraphs. This means that to be AAA compliant, paragraph line-height should be set to 1.5
In order to understand line-height more fully, we need to look at various types of CSS boxes.
There are four types of boxes that are relevant in this sample.
The paragraph is referred to as a containing box in this case - as it contains other boxes.
Inline boxes do not form new blocks of content; the content is distributed in lines.
Other boxes without specific markup are referred to as anonymous inline boxes.
Inline boxes sit side-by-side within the containing box, forming line boxes.
Line boxes
The content area is the invisible box that surrounds the text. Its height is determined by the fontsize.
Content area
1. Find the difference between the font-size and line-height. This will determine the leading.
For example: Font-size: 16px Line-height: 20px Difference: 4px (leading)
3. Apply this half-leading value to the top and bottom of the content area.
2px half-leading
The inline box generally wraps around the content box. Halfleading sits above and below the content box.
Inline box
However, the inline box can sometimes be smaller than the content area!
For example, if the line-height is smaller than the font size. In this case, the inline box will honor the line height.
For example: Font-size: 16px Line-height: 12px Inline box size: 12px
The content area then pokes out the top and bottom of the inline box. The half-leading collapses together to form the inline box height.
Inline box
Content area
Top half-leading
Bottom half-leading
The height of line boxes is determined by the tallest inline box (or replaced element) inside.
Increased line-height
Larger font
Superscript
Line boxes then stack on top of each other within the width of the containing box.
Containing box Line box
Line box
The superscript and subscript elements can sometimes force the line box to be taller than normal.
Line box
Superscript
You can fix this by setting these elements to a line-height of 0 which will remove all half-leading from the element.
IE5/6 incorrectly removes the top half-leading when an inline image is present.
Line box
An inline image
This is a hard one to fix, but if needed, margin can be added to the top of the image to fake the half-leading. This additional margin should only be shown to IE5/6 (using conditional comments)
Were done!