Css 003

  • Upload
    kzelda

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

  • 8/9/2019 Css 003

    1/99

    CSSLINE-HEIGHT

  • 8/9/2019 Css 003

    2/99

    What is

    leading?

  • 8/9/2019 Css 003

    3/99

    Back in the good old days type

    was set by hand using printingpresses.

  • 8/9/2019 Css 003

    4/99

    Printed material was created by

    setting out letters in rows. Eachletter was created on an

    individual block.

  • 8/9/2019 Css 003

    5/99

    Leading, or lead strips were

    added between the lines of letterswhen additional vertical space

    was required.

  • 8/9/2019 Css 003

    6/99

    In CSS, line-height is used to

    control the vertical spacebetween lines.

  • 8/9/2019 Css 003

    7/99

    However,the terms leading and

    half-leading are still used inassociation with

    CSS line-height.

  • 8/9/2019 Css 003

    8/99

    So how do you

    apply

    line-height?

  • 8/9/2019 Css 003

    9/99

    By default, browsers use between

    1.0 - 1.2line-height. This isreferred to as an

    initial value.

  • 8/9/2019 Css 003

    10/99

    You can override this default line-

    height using the CSSline-height property.

    p

    {

    line-height: 140%;

    }

  • 8/9/2019 Css 003

    11/99

    Line-height can be specified

    with five different types of unit.

  • 8/9/2019 Css 003

    12/99

    1. Line-height can be specified as

    normal.

    body

    {

    line-height: normal;

    }

  • 8/9/2019 Css 003

    13/99

    2. Line-height can be specified as

    inherit.

    p

    {

    line-height: inherit;

    }

  • 8/9/2019 Css 003

    14/99

    3. Line-height can be specified

    using a percentage value.

    p

    {

    line-height: 120%;

    }

  • 8/9/2019 Css 003

    15/99

    4. Line-height can be specified

    using a length value(using px, em etc).

    p

    {

    line-height: 20px;

    }

  • 8/9/2019 Css 003

    16/99

    5. Line-height can also be

    specified using a number value(a unit-less value).

    p

    {

    line-height: 1.2;

    }

  • 8/9/2019 Css 003

    17/99

    Shorthand

    line-height

  • 8/9/2019 Css 003

    18/99

    These five line-height values can

    also be specified using thefont shorthand property.

  • 8/9/2019 Css 003

    19/99

    The line-height value is written in

    conjunction with the font-sizevalue. The values are separated

    by a slash:

    /

    For example

  • 8/9/2019 Css 003

    20/99

    Normal value

    body

    {

    font: 100%/normal arial,

    helvetica, sans-serif;}

  • 8/9/2019 Css 003

    21/99

    Percentage value

    body

    {

    font: 100%/120% arial,

    helvetica, sans-serif;}

  • 8/9/2019 Css 003

    22/99

    Length value

    body

    {

    font: 100%/20px arial,

    helvetica, sans-serif;}

  • 8/9/2019 Css 003

    23/99

    Numbervalue

    body

    {

    font: 100%/1.2 arial,

    helvetica, sans-serif;}

  • 8/9/2019 Css 003

    24/99

    Calculating

    line-height

  • 8/9/2019 Css 003

    25/99

    Some CSS properties are

    inherited (passed down todescendant elements).

  • 8/9/2019 Css 003

    26/99

    This is designed to make it

    easier for authors - so they donot have to specify properties for

    all descendants.

  • 8/9/2019 Css 003

    27/99

    EG. the color property is

    inherited. If a color is applied tothe body element, it will be

    passed down to all other

    elements on the page.

  • 8/9/2019 Css 003

    28/99

    For line-height, inheritance is a

    little more complicated...

  • 8/9/2019 Css 003

    29/99

    To see the various line-height

    options in action, we will use thefollowing HTML code:

    consect etuer adipi scing eli

    Lorem ipsum dolor sit amet co

    Duis autem vel eum iriure dol

  • 8/9/2019 Css 003

    30/99

    This produces the following

    document tree:

    div#footerph1

    body

    Descendant elements

    Parent element

  • 8/9/2019 Css 003

    31/99

    We will also use the

    following 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; }

  • 8/9/2019 Css 003

    32/99

    Example 1

    The percentage value

  • 8/9/2019 Css 003

    33/99

    The line-height has been set to a

    percentage value (120%).

    body

    {font-size: 16px;

    line-height: 120%;

    }

    h1 { font-size: 32px; }

    p { font-size: 16px; }

    #footer { font-size: 12px; }

  • 8/9/2019 Css 003

    34/99

    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 isinherited by descendant

    elements.

  • 8/9/2019 Css 003

    35/99

    All descendants, regardless of

    their size, receive the samecalculated 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

  • 8/9/2019 Css 003

    36/99

    The line-heights do not scale

    with the relevant font size.

    Too tight

    OK

    Too loose

  • 8/9/2019 Css 003

    37/99

    Example 2

    the length value

  • 8/9/2019 Css 003

    38/99

    The line-height has been set

    using a length value (20px).

    body

    {font-size: 16px;

    line-height: 20px;

    }

    h1 { font-size: 32px; }

    p { font-size: 16px; }

    #footer { font-size: 12px; }

  • 8/9/2019 Css 003

    39/99

    The length value (20px) is

    inherited by descendantelements.

  • 8/9/2019 Css 003

    40/99

    All elements, regardless of their

    font-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

  • 8/9/2019 Css 003

    41/99

    Again, the line-heights do not

    scale with the relevant font size.

    Too tight

    OK

    Too loose

  • 8/9/2019 Css 003

    42/99

    Example 3

    the normal value

  • 8/9/2019 Css 003

    43/99

    The line-height has been set to

    normal (which is approx 1.2).

    body

    {font-size: 16px;

    line-height: normal;

    }

    h1 { font-size: 32px; }

    p { font-size: 16px; }

    #footer { font-size: 12px; }

  • 8/9/2019 Css 003

    44/99

    In this case, the normal value

    rather than a calculated value isinherited by descendant

    elements. Browsers may interpret

    the actual normal value in slightlydifferent ways.

  • 8/9/2019 Css 003

    45/99

    All elements now have

    line-heights that are relative totheir 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

  • 8/9/2019 Css 003

    46/99

    Now, the line-heights scale with

    the relevant font size.

    OK

    OK

    OK

  • 8/9/2019 Css 003

    47/99

    But what if you want the flexibility

    of the normal value, but to beable to specify the factor used?

    This is where number values

    come in.

  • 8/9/2019 Css 003

    48/99

    Example 4

    the number value

  • 8/9/2019 Css 003

    49/99

    The line-height has been set to a

    number value (1.5).

    body

    {font-size: 16px;

    line-height: 1.5;

    }

    h1 { font-size: 32px; }

    p { font-size: 16px; }

    #footer { font-size: 12px; }

  • 8/9/2019 Css 003

    50/99

    In this case, the factor (1.5)

    rather than a calculated value isinherited by descendant

    elements.

  • 8/9/2019 Css 003

    51/99

    All elements now have

    line-heights that are relative totheir 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

  • 8/9/2019 Css 003

    52/99

    Again, the line-heights scale with

    the relevant font size.

    Too loose?

    OK

    OK

  • 8/9/2019 Css 003

    53/99

    So, which is the

    best method?

  • 8/9/2019 Css 003

    54/99

    Generally, a number value is the

    best method for setting line-heightas line-heights will then always

    scale with the relevant font-size.

  • 8/9/2019 Css 003

    55/99

    It is hard to determine a perfect

    line-height as each situation isdifferent. However, it is safe to

    assume that headings can have

    less relative line-height thanparagraphs of text.

  • 8/9/2019 Css 003

    56/99

    For example, all content could be

    set to 1.5, and then headingsredefined to 1.2.

    body{ line-height: 1.5; }

    h1, h2, h3, h4, h5, h6

    { line-height: 1.2; }

  • 8/9/2019 Css 003

    57/99

    The Web Content Accessibility

    Guidelines (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

  • 8/9/2019 Css 003

    58/99

    Looking deeper

    into line-height

  • 8/9/2019 Css 003

    59/99

    In order to understand

    line-height more fully, we need tolook atvarious types of CSS

    boxes.

  • 8/9/2019 Css 003

    60/99

    Lets start with a simple piece of

    HTML code.

    The emphasis

    element is defined as

    inline.

  • 8/9/2019 Css 003

    61/99

    The code should berendered

    like this in most browsers.

    The emphasis element is defined asinline.

  • 8/9/2019 Css 003

    62/99

    There arefour types of boxes

    that are relevant in this sample.

  • 8/9/2019 Css 003

    63/99

    Box type 1:

    containingboxes

  • 8/9/2019 Css 003

    64/99

    The paragraph is referred to as a

    containing box in this case - asit contains other boxes.

    The emphasis element is defined asinline.

  • 8/9/2019 Css 003

    65/99

    Box type 2:

    inline boxes

    I id th h i

  • 8/9/2019 Css 003

    66/99

    Inside the paragraph are a series

    ofinline boxes.

    The emphasis element is defined asinline.

    I li b d t f

  • 8/9/2019 Css 003

    67/99

    Inline boxes do not form new

    blocks of content; the content isdistributed in lines.

    Th h i l t i t

  • 8/9/2019 Css 003

    68/99

    The emphasis elementis a type

    of inline box.

    The emphasis element is defined asinline.

    Oth b ith t ifi

  • 8/9/2019 Css 003

    69/99

    Other boxes without specific

    markup are referred to asanonymous inline boxes.

    The emphasis element is defined asinline.

  • 8/9/2019 Css 003

    70/99

    Box type 3:

    line boxes

    Inline boxes sit side by side

  • 8/9/2019 Css 003

    71/99

    Inline boxes sit side-by-side

    within the containing box, formingline boxes.

    The emphasis element is defined asinline.

    Line boxes

  • 8/9/2019 Css 003

    72/99

    Box type 4:

    content area

    The content area is the invisible

  • 8/9/2019 Css 003

    73/99

    The content area is the invisible

    box that surrounds the text. Itsheight is determined by the font-

    size.

    Content area

  • 8/9/2019 Css 003

    74/99

    Inline boxes

    and line-height

    Line height is applied to inline

  • 8/9/2019 Css 003

    75/99

    Line height is applied to inline

    boxes using asimple formula

    1 Find the difference between

  • 8/9/2019 Css 003

    76/99

    1. Find thedifferencebetween

    the font-size and line-height. Thiswill determine the leading.

    For example:Font-size: 16px

    Line-height: 20px

    Difference: 4px (leading)

    2 Divide the leading in half to

  • 8/9/2019 Css 003

    77/99

    2.Dividethe leading in half to

    create a half-leading value.

    4px leading / 2 = 2px half-leading

    3 Apply this half-leading value to

  • 8/9/2019 Css 003

    78/99

    3. Apply this half-leading value to

    thetop and bottomof thecontent area.

    2px half-leading

    2px half-leading

    Content area

    But things sometimes become a

  • 8/9/2019 Css 003

    79/99

    But things sometimes become a

    little more complicated...

    The inline box generally wraps

  • 8/9/2019 Css 003

    80/99

    The inline box generally wraps

    around the content box. Half-leading sits above and below the

    content box.

    Inline box

    However the inline box can

  • 8/9/2019 Css 003

    81/99

    However, the inline box can

    sometimes be smallerthan thecontent area!

    For example if the line-height is

  • 8/9/2019 Css 003

    82/99

    For example, if the line height is

    smaller than the font size. Inthis 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

  • 8/9/2019 Css 003

    83/99

    The content area then pokes out

    the top and bottom of the inlinebox. The half-leading collapses

    together to form the inline box

    height.

    Inline box

    Content area Top half-leading Bottom half-leading

  • 8/9/2019 Css 003

    84/99

    Some notes on

    line box height

    The height of line boxes is

  • 8/9/2019 Css 003

    85/99

    The height of line boxes is

    determined by the tallest inlinebox (or replaced element) inside.

    The tallest inline box could be an

  • 8/9/2019 Css 003

    86/99

    anonymous inline box.

    Line box

    It could be an inline box with

  • 8/9/2019 Css 003

    87/99

    increased line-height.

    Increased line-height

    Line box

    Or an inline box with a

  • 8/9/2019 Css 003

    88/99

    larger font-size.

    Line box

    Larger font

    Or the presence of a superscript

  • 8/9/2019 Css 003

    89/99

    p p p

    or subscript.

    Line box

    Superscript

    Or even the presence of a

  • 8/9/2019 Css 003

    90/99

    p

    replaced element, such as animage.

    Line box

    An inline image aligned to the baseline

    Line boxes then stack on top of

  • 8/9/2019 Css 003

    91/99

    p

    each otherwithin the width of thecontaining box.

    Line box

    Line box

    Containing box

  • 8/9/2019 Css 003

    92/99

    Some final

    notes

  • 8/9/2019 Css 003

    93/99

    Superscript

    and subscript

    The superscript and subscript

  • 8/9/2019 Css 003

    94/99

    elements can sometimes forcethe line box to be taller than

    normal.

    Line box

    Superscript

    You can fix this by setting these

  • 8/9/2019 Css 003

    95/99

    elements to a line-height of 0which will remove all half-leading

    from the element.

    sup, sub

    { line-height: 0; }

    Hat tip: www.velvetblues.com/

  • 8/9/2019 Css 003

    96/99

    IE6, line-height

    and inline images

  • 8/9/2019 Css 003

    97/99

    This is a hard one to fix, but if

  • 8/9/2019 Css 003

    98/99

    needed, margin can be addedto the top of the image to fake

    the half-leading. This additional

    margin should only be shown toIE5/6

    (using conditional comments)

  • 8/9/2019 Css 003

    99/99

    Were done!