line height

99
CSS LINE-HEIGHT

Upload: russ-weakley

Post on 28-Jan-2015

137 views

Category:

Technology


0 download

DESCRIPTION

A simple, step-by-step presentation on CSS line-height. Covers leading, how to apply various line-height values, as well as line-height and the inline box model. Hope you find it useful!

TRANSCRIPT

Page 1: Line Height

CSSLINE-HEIGHT

Page 2: Line Height

What isleading?

Page 3: Line Height

Back in the “good old days” typewas set by hand using printing

presses.

Page 4: Line Height

Printed material was created bysetting out letters in rows. Each

letter was created on anindividual block.

Page 5: Line Height

Leading, or lead strips wereadded between the lines of letters

when additional vertical spacewas required.

Page 6: Line Height

In CSS, line-height is used tocontrol the vertical space

between lines.

Page 7: Line Height

However,the terms leading andhalf-leading are still used in

association withCSS line-height.

Page 8: Line Height

So how do youapply

line-height?

Page 9: Line Height

By default, browsers use between1.0 - 1.2 line-height. This is

referred to as an initial value.

Page 10: Line Height

You can override this default line-height using the CSSline-height property.

p{

line-height: 140%;}

Page 11: Line Height

Line-height can be specifiedwith five different types of unit.

Page 12: Line Height

1. Line-height can be specified asnormal.

body{

line-height: normal;}

Page 13: Line Height

2. Line-height can be specified asinherit.

p{

line-height: inherit;}

Page 14: Line Height

3. Line-height can be specifiedusing a percentage value.

p{

line-height: 120%;}

Page 15: Line Height

4. Line-height can be specifiedusing a length value(using px, em etc).

p{

line-height: 20px;}

Page 16: Line Height

5. Line-height can also bespecified using a number value

(a unit-less value).

p{

line-height: 1.2;}

Page 17: Line Height

Shorthandline-height

Page 18: Line Height

These five line-height values canalso be specified using thefont shorthand property.

Page 19: Line Height

The line-height value is written inconjunction with the font-size

value. The values are separatedby a slash:

<font-size>/<line-height>

For example…

Page 20: Line Height

Normal value

body{

font: 100%/normal arial,helvetica, sans-serif;

}

Page 21: Line Height

Percentage value

body{

font: 100%/120% arial,helvetica, sans-serif;

}

Page 22: Line Height

Length value

body{

font: 100%/20px arial,helvetica, sans-serif;

}

Page 23: Line Height

Number value

body{

font: 100%/1.2 arial,helvetica, sans-serif;

}

Page 24: Line Height

Calculatingline-height

Page 25: Line Height

Some CSS properties areinherited (passed down to

descendant elements).

Page 26: Line Height

This is designed to make iteasier for authors - so they do

not have to specify properties forall descendants.

Page 27: Line Height

EG. the color property isinherited. If a color is applied to

the body element, it will bepassed down to all otherelements on the page.

Page 28: Line Height

For line-height, inheritance is alittle more complicated...

Page 29: Line Height

To see the various line-heightoptions 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>

Page 30: Line Height

This produces the followingdocument tree:

div#footerph1

body

Descendant elements

Parent element

Page 31: Line Height

We will also use thefollowing CSS

(pixels used for font-size to simplify - though not recommended!):

body {

font-size: 16px; line-height: XXX;

}

h1 { font-size: 32px; }p { font-size: 16px; }#footer { font-size: 12px; }

Page 32: Line Height

Example 1The percentage value

Page 33: Line Height

The line-height has been set to apercentage value (120%).

body {

font-size: 16px; line-height: 120%;

}

h1 { font-size: 32px; }p { font-size: 16px; }#footer { font-size: 12px; }

Page 34: Line Height

The percentage value (120%)and the body element’s font size

(16px) are used to create acalculated value (16px x 120% =19.2px). This calculated value is

inherited by descendantelements.

Page 35: Line Height

All descendants, regardless oftheir size, receive the same

calculated value line-height.element font-size line height calculated line-height

body 16px 120% 16px x 120% = 19.2px

h1 32px inherits calculated value - 19.2px 19.2px

p 16px inherits calculated value - 19.2px 19.2px

#footer 12px inherits calculated value - 19.2px 19.2px

Page 36: Line Height

The line-heights do not scalewith the relevant font size.

Too tight

OK

Too loose

Page 37: Line Height

Example 2the length value

Page 38: Line Height

The line-height has been setusing a length value (20px).

body {

font-size: 16px; line-height: 20px;

}

h1 { font-size: 32px; }p { font-size: 16px; }#footer { font-size: 12px; }

Page 39: Line Height

The length value (20px) isinherited by descendant

elements.

Page 40: Line Height

All elements, regardless of theirfont-size, receive the same line-

height.element font-size line height calculated line-height

body 16px 20px 20px

h1 32px inherits 20px 20px

p 16px inherits 20px 20px

#footer 12px inherits 20px 20px

Page 41: Line Height

Again, the line-heights do notscale with the relevant font size.

Too tight

OK

Too loose

Page 42: Line Height

Example 3the normal value

Page 43: Line Height

The line-height has been set tonormal (which is approx 1.2).

body {

font-size: 16px; line-height: normal;

}

h1 { font-size: 32px; }p { font-size: 16px; }#footer { font-size: 12px; }

Page 44: Line Height

In this case, the normal valuerather than a calculated value is

inherited by descendantelements. Browsers may interpretthe actual normal value in slightly

different ways.

Page 45: Line Height

All elements now haveline-heights that are relative to

their font-size.

element font-size line height calculated line-height

body 16px normal 16px x approx 1.2 = approx 19.2px

h1 32px normal 32px x aprox 1.2 = approx 38.4px

p 16px normal 16px x approx 1.2 = approx 19.2px

#footer 12px normal 11.2px x approx 1.2 = approx 13.44px

Page 46: Line Height

Now, the line-heights scale withthe relevant font size.

OK

OK

OK

Page 47: Line Height

But what if you want the flexibilityof the normal value, but to be

able to specify the factor used?This is where number values

come in.

Page 48: Line Height

Example 4the number value

Page 49: Line Height

The line-height has been set to anumber value (1.5).

body {

font-size: 16px; line-height: 1.5;

}

h1 { font-size: 32px; }p { font-size: 16px; }#footer { font-size: 12px; }

Page 50: Line Height

In this case, the factor (1.5)rather than a calculated value is

inherited by descendantelements.

Page 51: Line Height

All elements now haveline-heights that are relative to

their font-size.

element font-size line height calculated line-height

body 16px 1.5 16px x 1.5 = 24px

h1 32px factor of 1.5 32px x 1.5 = 48px

p 16px factor of 1.5 16px x 1.5 = 24px

#footer 12px factor of 1.5 12px x 1.5 = 18px

Page 52: Line Height

Again, the line-heights scale withthe relevant font size.

Too loose?

OK

OK

Page 53: Line Height

So, which is thebest method?

Page 54: Line Height

Generally, a number value is thebest method for setting line-height

as line-heights will then alwaysscale with the relevant font-size.

Page 55: Line Height

It is hard to determine a “perfectline-height” as each situation isdifferent. However, it is safe to

assume that headings can haveless relative line-height than

paragraphs of text.

Page 56: Line Height

For example, all content could beset 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; }

Page 57: Line Height

The Web Content AccessibilityGuidelines (WCAG) 2.0 state:

“line spacing is at least space-and-a-half within paragraphs”.

This means that to be AAAcompliant, paragraph line-height

should be set to 1.5

http://www.w3.org/TR/WCAG20/ - 1.4.8 Visual Presentation

Page 58: Line Height

Looking deeperinto line-height

Page 59: Line Height

In order to understandline-height more fully, we need to

look at various types of CSSboxes.

Page 60: Line Height

Let’s start with a simple piece ofHTML code.

<p>The <em>emphasis</em> element is defined as“inline”.

</p>

Page 61: Line Height

The code should be renderedlike this in most browsers.

The emphasis element is defined asinline.

Page 62: Line Height

There are four types of boxesthat are relevant in this sample.

Page 63: Line Height

Box type 1:containing

boxes

Page 64: Line Height

The paragraph is referred to as acontaining box in this case - as

it contains other boxes.

The emphasis element is defined asinline.

Page 65: Line Height

Box type 2:inline boxes

Page 66: Line Height

Inside the paragraph are a seriesof inline boxes.

The emphasis element is defined asinline.

Page 67: Line Height

Inline boxes do not form newblocks of content; the content is

distributed in lines.

Page 68: Line Height

The emphasis element is a typeof inline box.

The emphasis element is defined asinline.

Page 69: Line Height

Other boxes without specificmarkup are referred to as

anonymous inline boxes.

The emphasis element is defined asinline.

Page 70: Line Height

Box type 3:line boxes

Page 71: Line Height

Inline boxes sit side-by-sidewithin the containing box, forming

line boxes.

The emphasis element is defined asinline.

Line boxes

Page 72: Line Height

Box type 4:content area

Page 73: Line Height

The content area is the invisiblebox that surrounds the text. Its

height is determined by the font-size.

Content area

Page 74: Line Height

Inline boxesand line-height

Page 75: Line Height

Line height is applied to inlineboxes using a simple formula…

Page 76: Line Height

1. Find the difference betweenthe font-size and line-height. This

will determine the leading.

For example:Font-size: 16px

Line-height: 20pxDifference: 4px (leading)

Page 77: Line Height

2. Divide the leading in half tocreate a “half-leading” value.

4px leading / 2 = 2px half-leading

Page 78: Line Height

3. Apply this half-leading value tothe top and bottom of the

content area.

2px half-leading

2px half-leading

Content area

Page 79: Line Height

But things sometimes become alittle more complicated...

Page 80: Line Height

The inline box generally wrapsaround the content box. Half-

leading sits above and below thecontent box.

Inline box

Page 81: Line Height

However, the inline box cansometimes be smaller than the

content area!

Page 82: Line Height

For example, if the line-height issmaller than the font size. In

this case, the inline box will honorthe line height.

For example:Font-size: 16px

Line-height: 12pxInline box size: 12px

Page 83: Line Height

The content area then pokes outthe top and bottom of the inlinebox. The half-leading collapsestogether to form the inline box

height.

Inline box

Content area Top half-leading Bottom half-leading

Page 84: Line Height

Some notes online box height

Page 85: Line Height

The height of line boxes isdetermined by the tallest inline

box (or replaced element) inside.

Page 86: Line Height

The tallest inline box could be ananonymous inline box.

Line box

Page 87: Line Height

It could be an inline box withincreased line-height.

Increased line-height

Line box

Page 88: Line Height

Or an inline box with alarger font-size.

Line box

Larger font

Page 89: Line Height

Or the presence of a superscriptor subscript.

Line box

Superscript

Page 90: Line Height

Or even the presence of areplaced element, such as an

image.

Line box

An inline image aligned to the baseline

Page 91: Line Height

Line boxes then stack on top ofeach other within the width of the

containing box.

Line box

Line box

Containing box

Page 92: Line Height

Some finalnotes

Page 93: Line Height

Superscriptand subscript

Page 94: Line Height

The superscript and subscriptelements can sometimes force

the line box to be taller thannormal.

Line box

Superscript

Page 95: Line Height

You can fix this by setting theseelements to a line-height of “0”which will remove all half-leading

from the element.

sup, sub { line-height: 0; }

Hat tip: www.velvetblues.com/

Page 96: Line Height

IE6, line-heightand inline images

Page 97: Line Height

IE5/6 incorrectly removes thetop half-leading when an inline

image is present.

Line box

An inline image

Top half-leading removed

Page 98: Line Height

This is a hard one to fix, but ifneeded, margin can be addedto the top of the image to fakethe half-leading. This additionalmargin should only be shown to

IE5/6(using conditional comments)

Page 99: Line Height

We’re done!