CSS Basics



index
Disabled back button Next Section
printable version

Section 1: CSS Basics

CSS is the acronym of Cascading Style Sheets

Style sheets allow you to impose a standard style on a whole document, or even a whole collection of documents.


Recall that CSS works in conjunction with HTML, JavaScript, and XML to separate presentation from content, as seen in the figure below.

Components of a web page

Advantages of CSS

Why use CSS instead of embedding formatting tags in your HTML?

Section 2: Style Specification Formats

The CSS specification provides a set of properties that you can set for your html tags, as well as a set of allowable values for those properties.

Form of the rules:

selector {list of property/values}

–or–

selector
{
property₁: value₁;
property₂: value₂;
:
propertyₓ: valueₓ;
}

CSS Structure

A selector indicates the tag or tags affected.

Each property/value pair has the form:

property: value

Pairs are separated by semicolons.

Section 3: Basic Selector Forms

A selector, as its name implies, selects an element in your HTML document to which the specified formatting is applied.

Universal Selector

The universal selector is merely an asterisk (*) acting as a "wild card" to select any and all elements in the document.


Element Selector

An element selector selects all instances of an element, specified by its tag name.

Examples:

h1
{
   font-size: 20pt;
}
p, h2
{
   color: navy;
}

The last selector in the list above gives every paragraph element and h2 element the same blue foreground color even if there are thousands of them in a document.


Class Selector

A class selector targets any element that bears the given class name in its class attribute.

Example:

.highlighted
{
   background-color: yellow;
   display: inline;       /* makes background only the width of the text */
}
.redfont
{
   color: red;
}

A class is associated with a particular occurrence of an HTML tag by using the class attribute of the tag.

<h2 class="highlighted">Old Yeller</h2>
:
<p class="highlighted">This entire line should be highlighted.</p>
:
<ul>
<li>Item 1</li>
<li><span  class="highlighted">Item 2</span></li>
<li>Item 3</li>
</ul>
:
<p>This will <span class="highlighted">highlight</span> one word.</p>

This is the result:

You can also apply more than one class to a tag.

<h2 class="highlighted redfont">Old Yeller</h2>

This is the result:

Section 4: Formatting with Property:Value Pairs

Recall that the style format is

selector
{
property₁: value₁;
property₂: value₂;
:
propertyₓ: valueₓ;
}

CSS3 includes over 250 properties in a variety of categories.

Property Values

Property values can be expressed using the following approaches:

Section 5: Font Properties

Font properties can include the following:

Section 6: Colors

Colors can be specified using any of the following:

There are three color collections:

  1. There is a set of 16 colors that are guaranteed to be displayable by all graphical browsers on all color monitors.

    black 000000 green 008000
    silver C0C0C0 lime 00FF00
    gray 808080 olive 808000
    white FFFFFF yellow FFFF00
    maroon 800000 navy 000080
    red FF0000 blue 0000FF
    purple 800080 teal 008080
    fuchsia FF00FF aqua 00FFFF
  1. There is a much larger set, the Web Palette
    • 216 colors
    • Uses hex color values of 00, 33, 66, 99, CC, and FF
  2. Any one of 16 million different colors
    • The color property specifies the foreground color of elements

      p
      {
      color: red;
      }
      ul
      {
      color: orange;
      }

      <p>apple</p>
      <ul>
      <li>Orange</li>
      <li>Pumpkin</li>
      <li>Carrot </li>
      </ul>

    • The background-color property specifies the background color of elements

NOTE: Beware of using "odd" color names like "papayawhip" in your CSS. Some browsers will not accept them, and at one point they would not validate!

You can use all three approaches (hex, rgb triplet, or color name) in the same style sheet.

Color resources:

Section 7: Text Alignment
Section 8: Margins

Margin – the space between the border of an element and its neighbor element.

Example of heading with margins
Regular h5 with 0 margins

This particular word has big margins.

Section 9: Levels of Style Sheets

Styles are normally stored in style sheets, but there are three levels of styles: inline, document-level, and external.

  1. Inline style sheets appear in the tag itself; specified for a specific occurrence of a tag and apply only to that tag.
    Limit your use of these to debugging!

    General Form

    style = "property₁: value₁; property₂: value₂; …; propertyₓ: valueₓ"

    Example

    <p style = "float: left; width: 2.5in; margin-right: 10px;
    margin-bottom: 10px;
    border-style: dashed; border-width: thick;
    border-color: red; background-color: yellow;"
    >
    Some people are like Slinkies – it brings a smile
        to your face when you push them down a flight of stairs.
    </p>

    Some people are like Slinkies – it brings a smile to your face when you push them down a flight of stairs.








    Inline style sheets are much less useful than embedded or external style sheets because if you want to make a change, you have to do it EVERYWHERE that inline style occurs.


  1. Document-level style sheets (also called embedded style sheets) appear in the head of the document; they apply to the whole document in which they appear.

    A style sheet appears as a list of rules that are the content of a <style> tag.

    This is what we will use at first, but we'll later learn that it is better to use external style sheets.

    • Until recently, the recommendation was that the <style> tag must include the type attribute, set to "text/css". However, the validator now indicates that the type attribute for the style element is not needed and should be omitted.

    General Form

    <head>
    <style>
    – Insert rule list –
    </style>
    </head>

    Style Rule General Form

    selector {property₁: value₁; property₂: value₂; …; propertyₓ: valueₓ;}

    Example

    <head>
    <style>
    ol
    {
    list-style-type: upper-roman;
    }
    ol ol /* an ol inside an ol */
    {
    list-style-type: upper-alpha;
    }
    ol ol ol /* an ol inside an ol inside an ol*/
    {
    list-style-type: decimal;
    }
    </head>


  1. External style sheets are in separate files, potentially on any server on the Internet; can be applied to any number of documents <preferred option>.

    A <link> tag is used to specify that the browser is to fetch and use an external style sheet file:

    Example

    In the html document...

    <head>
    <link rel = "stylesheet" href = "styles/termpaper.css">
    </head>


    In the termpaper.css file linked above we might see...

    ol
    {
    list-style-type: upper-roman;
    }
    ol ol /* an ol inside an ol */
    {
    list-style-type: upper-alpha;
    }
    ol ol ol /* an ol inside an ol inside an ol*/
    {
    list-style-type: decimal;
    }

    External style sheets are written as text files with the MIME type text/css.


    Why are external stylesheets the preferred option?

    • When you use external CSS to control the basic cosmetics of your web site, you need only to edit those cosmetic properties in one file to update ALL of your web site's pages.
    • An external CSS page loads one time only and is cached, therefore the net weight of your individual pages decreases.
    • An external CSS page can be easier maintained.
    • Search engines can spider your site faster, because they do not have wade through all that unnecessary, redundant HTML code.
    • External CSS separates your page content from presentation.
Section 10: Comments, Validation, and Examples

Comments


Validation


Examples

Section 11: Let's Play

Click the "Edit and Click Me >>" to see the results of the css code in the left panel in the panel on the right.

Then, play around with the css code, making changes and seeing the effect of those changes in the right panel.

Section 12: Tutorials and Links

Online Video Tutorials


Links