Assignment 7 — Introducing CSS

Description:

CSS allows you to define the appearance and layout of your web pages. In this assignment, you will learn how to create an external style sheet, create and apply CSS rules to your content, and validate your CSS rules.

Where to specify style properties

You specify the appearance of your CSS by specifying rules that contains various properties. You can either specify the rules:

  • within the <style> element within the <head> element of your document

    <head>
    <meta charset="utf-8">
    <title>My page</title>
    <style>
    h1, h2 {color: red;}
    p {font-family: arial;}
    </style> </head>
  • by linking to an external file that contains all of your style rules in a <link> element

    <head>
    <meta charset="utf-8">
    <title>My page</title>
    <link href="myStyleSheet.css" rel="stylesheet" type="text/css"> </head>

If you are creating styles that you want to use for more than one web page, you should create an external style sheet file.

Selectors

Each CSS rule contains a selector and one or more declaration:

  • The selector identifies the HTML element to which the CSS rule is applied.
  • The declarations specify the CSS properties to use and the values for that properties. Each declaration is separated by a semi-colon (;). The set of properties are included within a starting and closing curly brace.

The following example shows includes a selector that identifies the <body> element. The rule contains declarations that specify the font-family (arial) and font-size (20px) for all text that appear within that paragraph.


Examples of selectors

A few simple examples of selectors include:

  • A simple property to change the color of paragraphs to red would be as follows:

    p {color:red;)
  • You can also apply a style rule to multiple elements if separate the names of the elements in the selector with a comma:

    h1, h2 {color:red;}

    Doing so would specify that both <h1> and <h2> elements would be red.

  • If you want to format a single occurrence of an element, you can attach an ID to that element and then specify the ID within the rule. For example, if you wanted the first paragraph in a topic to be blue, you could add an id attribute to that paragraph (<p id="first">This is the first paragraph</p>). Then in the styles, you would have a rule as follows:

    #first {color;blue;}
  • If you want to format multiple occurrence of elements but not all of them, you can attach a class value to the occurrences of the element that you want to format. You then then specify that class value within the selector of a rule. For example, if you wanted to flag any paragraph that had the class of warning in red text, you would add the class statement to every warning paragraph (<p class="warning">This is a warning.</p>). You would then specify the following style:

    p.warning {color:red;}

Validating CSS

Because CSS is also a standard, you can validate your CSS against the W3C standard by using a CSS validator that is available at:http://jigsaw.w3.org/css-validator/. This validator is very simliar to the one for HTML.

For more information, see Chapter 7 of the Head First HTML and CSS book.

HTML elements:

<style> <link>


In addition to html elements, this week also focuses on the id and class attributes.

CSS properties:

background-color background-image border color font-size
font-style font-weight left letter-spacing list-style
padding line-height top text-align  
Videos: Introducing CSS

Tasks:

  1. Read and complete the exercises from Chapter 7 of the Head First HTML and CSS book.
  2. Create a folder named Chapter7.
  3. In your Chapter7 folder, create your customized versions of the following files.
    • lounge.html
    • lounge.css
    • directions.html
    • elixirs.html
    • minimum of five images

    Duplicate the folder structure described in the book.

  4. Ensure that all of your links work correctly and display the appropriate file when clicked.
  5. Validate each of the three HTML pages at https://validator.w3.org/. Fix any errors that occur.
  6. Validate your lounge.css at http://jigsaw.w3.org/css-validator/.
  7. Upload the contents of your Chapter7 folder to your website.
  8. Zip your Chapter7 folder and name the file yourName_chapter7.zip.
  9. Submit the zipped file through Blackboard. In the comments section in Blackboard, enter a URL to the files on your website.
Grading criteria:

You assignment should demonstrate:

  • Revising the web pages that you created in Assignment 6.
  • Creating an external style sheet.
  • Adding a link in each web page to point to the external style sheet.
  • Defining a CSS style rule to add borders to <h1> headings.
  • Defining multiple CSS style rules to apply different colors to <p> elements based on class attributes.
  • Defining a CSS style rule to change the color of <h1> and <h2> headings.
  • Defining a CSS style rule to change the font-family to a sans-serif font.
  • Validating your HTML files with the HTML validator.
  • Validating your CSS file with the CSS validator.
  • Uploading the files to your web site.
Due:

By 10/30

Note: Assignment 6 is also due this week.

Example assignment:

Chapter 7 example