J. Blustein

Web-centric Computing

Some Example Code

Inheritance and Cascading in CSS

HTML Code

(Nested spans)

 <p class="example">
   <span >Quick
     <span >brown
       <span >fox
         <span >jumps
           <span >over
             <span >the
               <span >lazy
                 <span >dog. </span>
               </span> 
             </span> 
           </span> 
         </span> 
       </span> 
     </span> 
   </span> 
 </p>

CSS Rules

p.example > span:first-child
             {font-size:550%; font-variant:small-caps}
span > span  {font-size:smaller}

Looks like this...

Quick brown fox jumps over the lazy dog.

telescoping (large to small) words all in small caps

With these CSS Rules

span         {font-variant: normal}
p.example > span:first-child
             {font-size:550%; font-variant:small-caps}
span > span  {font-size:smaller}

The same HTML looks like this...

telescoping (large to small) words all in small caps

Does that mean that the first rule over-rides the later ones?

Questions

  1. Why do we need the CSS rule

    span {font-variant: normal}

    to cancel the effect of the

    p.example > span:first-child {font-variant:small-caps}

    on all of the words after the first?

  2. How is the cascading in CSS similar, but not the same as, inheritence in Java?
  3. What would happen if we used this CSS rule:

    span:first-child {font-size:550%}

    instead of

    p.example > span:first-child {font-size:550%}?

For more examples of selectors

See


http://www.cs.dal.ca/~jamie/course/CS/3172/examples/CSS/nesting/nesting.html
Version:
30 October 2007
CS 3172 Prof.:
J. Blustein <jamie@cs.dal.ca>

This document is written in valid XHTML 1.0 &
This document makes use of cascading style sheets