Website Development I

Course Info Class Notes Resources Student Websites

Class No. 12:

CSS Inheritance and Priority System

Today’s Goal

To get a feel for CSS inheritance and the priority system.


Class Resources

CSS: The Missing Manual (PDF) – pages 103-113

Web Programming book reading for THIS class

Web Programming book reading for NEXT class


Preambles

Collect one last Mid-Course Evaluation form

Questions from our last class or the readings?


Understanding CSS Inheritance

First, let’s begin by defining “inheritance” ...

Second, let’s talk about “streamlining” style sheets ...

 1  /* the following styles were added by JMH to make the sample code look as
 2     it does in the slides, that is, white text on a black background */
 3  body {  
 4    background-color: black ;
 5    color: white ;
 6  }
 7  h1, h2, .jmhcomment, h1+p, ul,
 8  a:link, a:hover, a:active, a:visited {
 9    color: lime;
10  }
11  h2 {
12    margin-top: 3em;
13  }
14  .jmhcomment {
15    font-style: italic;
16  }
17  /* end of JMH styles */

But be careful! — some properties do not get inherited

“When styles conflict, the more specific style wins out.” (McFarland, p. 85)

Two situations in which CSS properties conflict (p. 92)

  1. “through inheritance when the same property is inherited from multiple ancestors”
  2. “when two or more styles apply to the same element”


Cascading Rules


The “Point” System  (McFarland, p. 108)

CSS Missing Manual Fig. 5-3
[figure source:  CSS: The Missing Manual, by David Sawyer McFarland, fig. 5-3, p. 109]

It is impossible to memorize all these rules

Addressing specificity issues


Advanced Material

Information on advanced selector techniques for students who are interested can be found after the exercise or by clicking here.


Today’s Exercises

Goal:  To delve more deeply into CSS by looking at the code needed to render PRO Manual page 7 as a web page.

    Step 1:  Study the resources provided

  1. Begin by studying the layout of PRO Manual page 7, which is truly quite complex.
  2. Look at the rendering of Mr. Heines’s version of this page to see how many of the original page’s visual elements he was able to recreate on the web.
  3. Now look at for his version (by displaying the page and typing CtrlU) to see the CSS that he used to render the page as desired.
  4. Step 2:  Copy Mr. Heines’s code into your own Dropbox so that you can modify it

  5. Open a new, blank file in UltraEdit.
  6. Display Mr. Heines’s version of page 7 in your browser and then type CtrlU to display its source code.
  7. Click anywhere in the source code and type CtrlA to select it all.
  8. Type CtrlC to copy what you have selected.
  9. Return to your blank page in UltraEdit and type CtrlV to paste in the copied code.
  10. Type CtrlS to save a the copied code into your own Dropbox so that you can work with it.
  11. Step 3:  Experiment with modifying the CSS

  12. A good way to learn the complexities of CSS is to play with modifying code that you get from others.  Try commenting out various CSS rules to see how the page renders when those rules are no longer active.
  13. Try modifying rules that you are familiar with to achieve different effects.
  14. Step 4:  Try to streamline the CSS by modifying it to take advantage of inheritance and the cascade

  15. Be careful, this is tricky!  Before you attempt this, make a backup copy of your work so that you can return to a working version of the code if you screw up. 🙃
  16. Instead of cutting-and-pasting rules to different locations, copy-and-paste them and then comment them out from their original location.  This will help you recover if you screw up!


 


This is Class No. 12.  It was last modified on Saturday, October 21, 2023 at 4:38 PM.  Copyright © 2010-2024 by Jesse M. Heines.  All rights reserved, but may be freely copied or excerpted for educational purposes with credit to the author.

Advanced Selector Techniques for Advanced Students

This material is for advanced students, so we will not take the time to discuss it during class.

Achieving a clear understanding of how selectors work is critically important not only for working with CSS, but also because the techniques discussed here are also used with JavaScript, which we’ll look at later in this course.

In addition to applying styles to tags, it is common is to apply a style to an element with a specific ID attribute.

You can apply a style to groups of tags:

You can apply a style to an element using the Document Object Model (DOM):

You can even use an asterisk as a “wildcard” to apply a style to all HTML tags:

Pseudo-classes

Separating content from rendition