Lecture 14: Classes, IDs, and Additional Text-Related Properties

Overview:

  1. Classes
  2. IDs
  3. Pseudo-Classes
  4. font-weight Property
  5. font-style Property
  6. font-variant Property
  7. line-height Property
  8. font Shorthand Property
  9. letter-spacing Property
  10. word-spacing Property
  11. text-align Property
  12. vertical-align Property
  13. text-decoration Property
  14. text-indent Property
  15. text-transform Property

Classes

IDs

Pseudo-Classes

Pseudo-Class Occurs Replaces Recommendations
:link This is the unvisited state (this state occurs before the document linked to has been viewed) Deprecated link attribute for <body> Blue links get the highest rate of click-through; users always associate blue with links.
:visited This state occurs after the document linked to has been viewed. Deprecated vlink attribute for <body> Make sure that visited links maintain acceptable contrast with background colors.
:hover This state occurs when the link (or other element, in the latest browsers) is moused over.   Avoid visual changes that cause surrounding content to shift left/right (boldface and italics would cause such a shift).

Less is more when using :hover

Not supported in old browsers, such as Netscape 4.x
:active This state occurs when the link is in the process of being selected, usually via clicking. Deprecated alink attribute for <body> Typically this will only be seen for a moment (such as when a link is being clicked), but in IE 5.x & 6 sometimes this can persist if the link is not fully clicked.

No support in IE 7.
:focus This state occurs when the link (or form element) has been selected by tabbing.   No support in Windows Internet Explorer.

Specifying Default Colors and Behavior

a:link {color: blue;}
a:visited {color: purple;}
a:hover {color: #000;}
a:focus {color: #000;}
a:active {color: #000;}

In this example all unvisited links are blue, all visited links are purple, and all links are black when moused over, tabbed to, and selected. Every link in the document is affected.

Specifying Multiple Sets of Link Colors

Two approaches can be taken to this:

a.example:link {color: blue;}
a.example:visited {color: purple;}
a.example:hover {color: #000;}
a.example:focus {color: #000;}
a.example:active {color: #000;}

or

#nav a:link {color: blue;}
#nav a:visited {color: purple;}
#nav a:hover {color: #000;}
#nav a:focus {color: #000;}
#nav a:active {color: #000;}

Important:

font-weight Property

Property Usage and Effect Values Accepted Recommendations
font-weight Specifies the amount of weight (lightness / boldness) to use for text inside an element. lighter | normal | bold | bolder | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900 Some elements come with a default setting of bold (such as headings, <strong>, and <b>), which can be overriden via 'font-weight: normal;'

Use boldface sparingly; a little bit of boldface draws the user's attention and too much makes them not want to look at the text.

Typically 'bold' is sufficient; 'bolder' can make letterforms too blocky/thick if the browser renders them as intended.

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Font-Weight</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body {font-size: 12px;
          font-family: verdana,geneva,lucida,arial,sans-serif;}
    h1 {font-size: 18px; font-family: georgia,serif;
        color: purple; font-weight: normal;}
    h2 {font-size: 16px; font-family: georgia,serif;
        color: green; font-weight: bolder;}
    .bld {font-weight: bold;}
    --></style>
</head>
<body>
<h1>A Sample Level 1 Heading</h1>

<h2>A Sample Level 2 Heading</h2>

<p>Sample text, some of which is <span class="bld">bold</span></p>

</body>
</html>

See how this example renders

font-style Property

Property Usage and Effect Values Accepted Recommendations
font-style Specifies whether the text inside an element should be italicized, oblique, or normal. italic | normal | oblique Some elements come with a default setting of italic (including <em> and <i>), which can be overriden via 'font-style: normal;'

Use italics and oblique text sparingly.

Old browsers (e.g., Netscape 4.x) do not support oblique text.

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Font-Style</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body {font-size: 12px;
          font-family: verdana,geneva,lucida,arial,sans-serif;}
    h1 {font-size: 18px; font-family: georgia,serif;
        color: purple; font-weight: normal; font-style: italic;}
    h2 {font-size: 16px; font-family: georgia,serif;
        color: green; font-weight: bolder; font-style: oblique;}
    .oblique {font-style: oblique;}
    .normal {font-style: normal;}
    --></style>
</head>
<body>
<h1>A Sample Level 1 Heading</h1>

<h2>A Sample Level 2 Heading</h2>

<p>Sample text, some of which is <span class="oblique">oblique</span></p>

<p><em class="normal">Emphasized text, with italics removed</em></p>

<p><em>Emphasized text, with no changes to default rendering</em></p>
</body>
</html>

See how this example renders

font-variant Property

Property Usage and Effect Values Accepted Recommendations
font-variant Specifies whether the text inside an element should be small-caps or normal. small-caps | normal Support for 'small-caps' is quirky in Windows IE 5.x (it capitalizes all the letters).

Mac IE 5.x typically inserts extra gapping between some letters.

Small-caps can be useful for headings/subheadings, to give them some visual distinctiveness, but the small-caps effect also shrinks the font size a bit.

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Font-Variant</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body {font-size: 12px;
          font-family: verdana,geneva,lucida,arial,sans-serif;}
    h1 {font-size: 24px; font-family: georgia,serif;
        color: purple; font-weight: normal; font-variant: small-caps;}
    h2 {font-size: 20px; font-family: georgia,serif;
        color: green; font-variant: small-caps;}
    --></style>
</head>
<body>
<h1>A Sample Level 1 Heading</h1>

<h2>A Sample Level 2 Heading</h2>

</body>
</html>

See how this example renders

line-height Property

Property Usage and Effect Values Accepted Recommendations
line-height Sets the height for each line of content in an element. Useful for giving content some extra negative spacing (empty spacing) for better readability. Number and a sizing unit (px, pt, em, %) for computed values or number alone (no unit) for inherited multiplier Keep in mind the differences that units make to line-heights. If there is a lot of nesting, you may not want to have a computed value from the outermost tag.

Expect to find cross-browser differences in default line-height values (Gecko-based browsers tend to render lines of text closer together than other browsers by default; of course CSS can change this default).

Sample code using inherited multiplier value:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Line-Height without Units</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body {font-size: 10px;
          font-family: verdana,geneva,lucida,arial,sans-serif;
          line-height: 2.0;}
    span {font-size: 25px;}
    strong {font-size: 40px;}
   --></style>
</head>
<body>
<p><strong>Definitely a strong impression with this text.</strong></p>

<p><span>A 25px span here, with 50px line-height.</span></p>

<p>20px line-height for this sentence.</p>
</body>
</html>

See how this example renders

Sample code using computed line-height from <body>:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Line-Height with Units</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body {font-size: 10px;
          font-family: verdana,geneva,lucida,arial,sans-serif;
          line-height: 2em;}
    span {font-size: 25px;}
    strong {font-size: 40px;}
   --></style>
</head>
<body>
<p><strong>Definitely a strong impression with this text.</strong></p>

<p><span>A 25px span here, with 20px line-height.</span></p>

<p>20px line-height for this sentence.</p>
</body>
</html>

See how this example renders

font Shorthand Property

letter-spacing Property

Property Usage and Effect Values Accepted Recommendations
letter-spacing Used to control the amount of empty space between letters. Number and a sizing unit (px, pt, em, %) Useful for making headings more visually distinctive.

Less is more; small amounts give the best visual result.

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Letter-Spacing</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body {font-size: 12px;
          font-family: verdana,geneva,lucida,arial,sans-serif;}
    h1 {font-size: 24px; font-family: georgia,serif;
        color: purple; font-weight: normal;
        font-variant: small-caps; letter-spacing: 3px;}
    h2 {font-size: 20px; font-family: georgia,serif;
        color: green; font-variant: small-caps; letter-spacing: 1px;}
   --></style>
</head>
<body>
<h1>A Sample Level 1 Heading</h1>

<h2>A Sample Level 2 Heading</h2>

</body>
</html>

See how this example renders

word-spacing Property

Property Usage and Effect Values Accepted Recommendations
word-spacing Used to control the amount of empty space between words. Number and a sizing unit (px, pt, em, %) Not frequently used.

Windows Internet Explorer 5.x lacks support; Mac IE 5.x and Windows IE 6 & 7 are supportive, as well as the Gecko-based browsers, Opera, and Safari.

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Word-Spacing</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body {font-size: 12px;
          font-family: verdana,geneva,lucida,arial,sans-serif;}
    h1 {font-size: 24px; font-family: georgia,serif;
        color: purple; font-weight: normal;
        font-variant: small-caps; word-spacing: 10px;}
    h2 {font-size: 20px; font-family: georgia,serif;
        color: green; font-variant: small-caps; word-spacing: 5px;}
    --></style>
</head>
<body>
<h1>A Sample Level 1 Heading</h1>

<h2>A Sample Level 2 Heading</h2>

</body>
</html>

See how this example renders

text-align Property

Property Usage and Effect Values Accepted Replaces Recommendations
text-align Controls the horizontal alignment of text inside a block-level element. left | right | center | justify Deprecated align attribute for headings, <p>, <img />, and <div>.

Also replaces align for <tr>, <td>, and <th> tags.
'justify' is useful for getting rid of jagged right edges in paragraphs.

Windows Internet Explorer 5.x misinterprets this property and will try to also align the element relative to its parent element (the effect should just be for the text content it contains, not the entire element).

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Text-Align</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body {font-size: 12px;
          font-family: verdana,geneva,lucida,arial,sans-serif;}
    h1 {font-size: 24px; font-family: georgia,serif;
        color: purple; font-weight: normal;
        font-variant: small-caps; text-align: right;}
    h2 {font-size: 20px; font-family: georgia,serif;
        color: green; font-variant: small-caps; text-align: center;}
    --></style>
</head>
<body>
<h1>A Sample Level 1 Heading (Right-Aligned)</h1>

<h2>A Sample Level 2 Heading (Centered)</h2>

</body>
</html>

See how this example renders

vertical-align Property

Property Usage and Effect Values Accepted Replaces Recommendations
vertical-align Controls the vertical alignment of content inside an element; sometimes will cause content to align in a certain direction relative to the element (for example, with images). top | middle | bottom Deprecated align attribute for <img />.

Also replaces valign for <tr>, <td>, and <th> tags.
Works well with images, table cells, and form elements (covered in INP 170: Web Coding II).

For other elements your results will be mixed.

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Vertical-Align</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body {font-size: 12px;
          font-family: verdana,geneva,lucida,arial,sans-serif;}
    .topAlign {vertical-align: top;}
    .middleAlign {vertical-align: middle;}
    --></style>
</head>
<body>

<table cellpadding="5" cellspacing="0" border="1">
<tr>
  <td>
  A cell with lots of content, setting the row height
  <p>A cell with lots of content, setting the row height</p>
  A cell with lots of content, setting the row height
  <p>A cell with lots of content, setting the row height</p>
  A cell with lots of content, setting the row height
  <p>A cell with lots of content, setting the row height</p>
  </td>

  <td class="topAlign">
  Not much content, but top-aligned!
  </td>
</tr>
</table>

<p>Some middle-aligned text
   <img src="sydney2.jpg" width="113" height="150" class="middleAlign"
    alt="A view of the Sydney Opera House from a distance"  />
    And even more middle-aligned text
</p>

</body>
</html>

See how this example renders

text-decoration Property

Property Usage and Effect Values Accepted Replaces Recommendations
text-decoration Used for text (often link) styling. none | underline | overline | underline overline | line-through Deprecated <s> and <strike> elements are replaced by 'line-through'.

Deprecated <u> element replaced by 'underline'.
Users assume that underlined text is a link, so only underline links!

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Text-Decoration</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body {font: 12px verdana,geneva,lucida,arial,sans-serif;}
    a:link {color: blue;}
    a:visited {color: purple;}
    a:hover, a:focus, a:active {text-decoration: none;}
    --></style>
</head>
<body>
<p>Google is located at:
<a href="http://www.google.com">www.google.com</a></p>
</body>
</html>

See how this example renders

text-indent Property

Property Usage and Effect Values Accepted Recommendations
text-indent Used to control the indent before the first line of text in the element. Number and a sizing unit (px, pt, em, %) Not frequently used, but better than the numerous non-breaking spaces used in years past.

Gecko-based browsers and Opera only support this for block-level elements; IE accepts it for inline as well as block-level elements.

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Text-Indent</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body {font: 12px verdana,geneva,lucida,arial,sans-serif;}
    .indent {text-indent: 30px;}
    --></style>
</head>
<body>
<p class="indent">The first line of text is indented<br />but not the
second line of text</p>

<p><span class="indent">Some browsers ignore text-indent when it is
applied to an inline element.<br />Others apply it normally.</span></p>
</body>
</html>

See how this example renders

text-transform Property

Property Usage and Effect Values Accepted Recommendations
text-transform Used to change text from its original case to another case. none | capitalize | uppercase | lowercase Not frequently used, but could be helpful in cases where users are entering data as free text and you want the end result to display consistently.

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Text-Transform</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body {font: 12px verdana,geneva,lucida,arial,sans-serif;}
    .upper {text-transform: uppercase;}
    .lower {text-transform: lowercase;}
    .capitals {text-transform: capitalize;}
   --></style>
</head>
<body>
<p class="upper">Original text is in sentence capitalization</p>

<p class="lower">Original text is in sentence capitalization</p>

<p class="capitals">Original text is in sentence capitalization</p>
</body>
</html>

See how this example renders