IT581: Practicum in Web Development, Summer 2015 (Hybrid)

CSS selectors and Typography

IDs and styles

Example

Suppose a page contains:

    <p id="first">This is the first short paragraph</p>
    <p id="second">This is another brief paragraph</p>

the stylesheet could have

    #first { background-color: yellow; 
             color: blue; 
             font-size: x-large; 
             font-weight: bold; }
    #second {background-color: lime; 
             color: red;  
             font-size: xx-large; 
             font-style: italic; }

Link to complete example page illustrating styles and IDs.


Classes in HTML and CSS

What if you want to style two paragraphs the same, but different from all others?

Example

Suppose a page contains:

    <p class="special">This is the first short paragraph</p>
    <p class="special">This is another brief paragraph</p>

the stylesheet could have

    p.special { background-color: blue; 
                color: yellow; 
                font-size: large; 
                font-weight: bold; 
                font-style: italic
                }

Link to complete example page illustrating styles and classes.


Classes and different types of elements

Example

the stylesheet in this example has two rules for classes

    *.special { background-color: blue; 
               color: yellow; 
               font-size: large; 
               font-style: italic
               }
    *.extra_special { background-color: yellow; 
                      color: green; 
               }

The body contains

Link to complete example page illustrating classes.



Structural diagrams

The elements in an HTML document must be in the proper relationship to each other. If they are correctly nested, you can draw diagram which shows the inner elements as branches from the outer ones. This diagram is called a parse tree

The structure of a valid HTML document can be represented as a tree structure (usually drawn upside down.).


Example: Tree structure of a document

The following short HTML document

      <html>
        <head>

          <title>The Emmel's home page</title>
        </head>

        <body>
          <h1>The Emmel's home page</h1>

          <p>We are a multi-generation 
            <strong>family</strong>. Our names are

          <ul>
            <li>Essgy</li>
            <li>Aitchtee</li>
            <li>Exx</li>
          </ul>

        </body>
      </html>

can be illustrated as:

image missing


Parse Trees and selectors

The parse-tree determines the selectors for style rules and the structure for rendering the page. If the document is not valid, the parse-tree may not be constructable or usable. For example if tags are nested incorrectly, it is impossible to determine which is parent and which is child.

      <a href=gotopage.html"><strong>My homepage</a></strong>

Descendent selectors

In the HTML

<body>
<h1>The <em>beginning</em> <strong>of the <em>end</em></strong></h1>
<p><strong>And so it  <em>begins </em></strong>, 
the <strong>search</strong> for better <em>style</em></p>
</body>

[Image missing]

In the first line, the first em is a child of h1. The second em is a child of strong and a grandchild of h1.

In a descendent selector, the ancestor is separated from the descendent by whitespace.

    h1 { color: red }
    em { color: green }  /* select em */
    p  { color: #888 }
    p em { color: blue } /* select em that is a descendent of a p */

In the example, h1 text will be red and all em text will be green except for any em that is a descendent of p; that will be blue. Here is an example with that HTML and style rules.

The "ancestor" could be an id or class selector. For example, a rule selected by #demo strong would apply only to strong elements contained within the element with id "demo".


Grandchildren

A descendant selector of the form A B matches when an element B is any level descendant of ancestor element A. In the descendent example above, p em would match both of the em elements in the paragraph text. But what if you want only grandchildren - descendents with at least one intervening element.

In the specific example, where the only intermediate element is strong, we could write p strong em. But we can also allow for any intermediate element.

An asterisk in a selector is a wild card, representing any element. Use one to indicate intervening generations. For example

     p * em {background-color: yellow;}

Whitespace around the asterisk is a necessary part of the complete selector.

With this rule, only em elements that are a grandchild or later descendant of a paragraph element will be given a yellow background. Here is the example with that rule added.


Immediate Child selectors

A child selector matches when an element is the child of some element. A child selector is made up of two or more selectors separated by the character >. For example

     p>em {background-color: teal; color: lime}

This rule applies only to em elements that are an immediate child of a paragraph element, with no intervening descendants. Here is the example with that rule added.


Adjacent sibling selectors

An element B is an adjacent sibling of an element A if A and B share the same parent and A immediately precedes B. The adjacent sibling selector has the form A+B. For example,

      h1+p {color: orange; }
      p+p {font-size: 75%; }

In this case, the text color of the paragraph immediately following the h1 will be changed. Any pragraph that has a paragraph immediately before it will have smaller text. Here is the example with those rules and some extra paragraphs added.


Pseudo-elements and pseudo-classes

Pseudo-classes

A pseudo-class is used to define a special state of an element.

For example, it can be used to:

Pseudo-classes are written after the element using a colon between the element symbol and the pseudo-class name. The following table lists most of them.

pseudo-classdescription
:linkdesignates an unvisited link
:visiteddesignates a visited link
:hoverapplies while the user selects an element, but does not activate it;
for example, when the mouse pointer hovers over a link.
:activeapplies while an element is being activated;
for example, between the time the user presses the mouse button and releases it.
:focusapplies while an element has the focus (accepts keyboard events or other forms of input)
:first-childmatches an element that is the first child element of some other element.

Note: a:hover MUST come after a:link and a:visited in the CSS definition in order to be effective! a:active MUST come after a:hover in the CSS definition in order to be effective! Pseudo-class names are not case-sensitive.

Example of pseudo-classes

    <style type="text/css">
      a:link { color: red }
      a:visited { color: green }
      a:hover { color: yellow }
      a:active { color: blue }
      input:focus { background-color: yellow;}
      li:first-child { font-weight: bold }
    </style>

Pseudo-elements

A CSS pseudo-element is used to style specified parts of an element.

For example, it can be used to:

Pseudo-elements are written with a double colon separator for CSS3.

pseudo‑elementdescription
first-lineApplies to the contents of the first formatted line of a paragraph, division, or other block element.
The specific text identified by the selector styled may change if the window is resized or the view is zoomed.
first-letterApplies to the first character of text in an element

Example of pseudo-elelemts

    <style type="text/css">
      p::first-line { font-style: italic }
      p::first-letter { font-size: 200%; font-weight: normal }
    </style>

The first line of each paragraph will be italicized.

The first letter will be twice as large as the others.

The example also illustrates the text-transform attribute. Here is the example with those rules added


Some CSS properties for modifying text

text‑decorationdescription
noneProduces no text decoration.
underlineText is underlined.
overlineText has a line above it.
line-throughText has a line through the middle (word processing's strikethrough).
text‑transformdescription
noneNo change from original
uppercaseShow all characters in uppercase
lowercaseShow all characters in lowercase
capitalizeBegin each word in the text a capital letter.
font‑variantdescription
normalNo change from original
small-capsUses the small-caps font for the typeface (if available), or creates the effect.

Example: Creating Drop caps

Some paragraphs on this page begin with a drop cap - a large capital that drops below the baseline for the rest of the line. This simple version is produced by floating the first letter.

      h1+p:first-letter, h2+p:first-letter  {
        text-transform: uppercase;  /* Ensure that it's a capital. */
        font-family: "Brush Script", "Lucida Handwriting",  Impact, cursive;
        font-size: 2em; 
        font-weight: bold; 
        color: #777; 
        background-color: inherit; 
        display: block;
        float: left; 
        padding-right: 4px;
        padding-top: 4px;
        }

Notice the use of adjacent sibling selectors and the first-letter pseudo-element.

CSS properties that affect text spacing

Indentation

text‑indentdescription
length or percentageIndents the first line of the block. A negative value will produce a hanging indent.

Additional style properties

There are many properties that can be styled - 89 altogether - for visual screen display. For now, we have only looked briefly at the most common ones. Additional properties will be introduced in later lessons.


What is typography?

Typography is the practice of arranging type within a design. This not only includes the selection of a typeface, but also the size, spacing, color, and styles of the type. At a higher level, though, typography also deals with the design of type on the page and its interaction with other elements—such as photos, illustrations, interface chrome. This treatment of the text can be every bit as important to the message as the actual words themselves.
~Jason Cranford Teague in Fluid Web Typography


Legibility, Readability, and Scannability

Legibility refers to the actual type. It is well designed and clearly rendered. A legible font will improve readability.

Readability refers to the ease of reading text, especailly in paragraphs and other blocks. White space such as line height is important for readability.

Scannability refers to how easy it is to get information by scanning quickly over the text. Typographic design is important for scannability - the design can steer the user to important features, or interfere.


Baseline, cap height, and x-height.

Characteristics of a good screen typeface (Reference: "The Anatomy of Web Fonts")

Some popular typefaces that exhibit all or most of these properties are Verdana, Trebuchet, and Helvetica. The first two are designed specifically for screen display.

1502_goodscreenfont


Browser-Safe Fonts

Jason Cranford Teague, in Fluid Web Typography, notes that "Generally, if you are using proper encoding (UTF-8 is the safest) and your font has the correct glyph as a part of its character set, any character you can type or insert will be displayed in your Web page. This is obviously not the case if you are using a dingbat font or a font intended for a language other than the one you are using."

However that only applies if the font is available on the client computer. It is safest to use a font family that is know to be widely installed.

There are nine basic typefaces that are pre-installed on almost all computers.

  • Arial
  • Times New Roman
  • Georgia
  • Verdana
  • Trebuchet MS
  • Courier New
  • Comic Sans MS
  • Impact
  • Webdings*

*The Webdings font is not recognized by Firefox and Opera.

There are an additional 68 font families that are likely to be available both Mac and Windows computers. They are shown in the illustration. [Source: Fluid Web Typography by Teague.]


Google Web Fonts

There are several different systems for making downloadable fonts available on the Web.

  • You can provide your own font for use on a web page.
  • At this time, there are limitations to doing this:
    • Not all browsers support the preferred WOFF standard. (i.e., IE generally does not)
    • Not all fonts are made available for use.
  • However this situation may improve in the future.
  • Currently, the simplest solution is to use the Google Web Fonts embedding service
  • To use a Google Font, just
    • Go to http://www.google.com/fonts/
    • Click the "Quick Use" button
    • Copy & paste the link code into your document head.
      <link href="http://fonts.googleapis.com/css?family=Metrophobic" rel="stylesheet">
      
    • start to use it like this
      h1 {
      font-family: 'Metrophobic', arial, serif;
      }
      

Background images

  • Elements of a page use background images.
  • The property is background-image.
  • Instead of a color, its value is the URL of an image.
  • The location (URL) of the image file is written in parentheses:
    background-image: url(URL of the image);

Example

    body {background-image: url('clouds.gif');}

In this case, the image stored in clouds.gif will be used as the background image for the page.

Complete example page. Be sure to look at the source code.


Comments to: dong@hood.edu
Last Modified: 08 July 2015. 12:08