J. Blustein

Web-centric Computing

Some Example Code

Experiments with CSS Selectors

Using sibling selectors (span + span) ...

Quick brown fox jumps over the lazy dog.

[see the sibling code]

But by making the elements nest, and using the parent selector (span > span) ...

Quick brown fox jumps over the lazy dog.

... we get an interesting telescoping effect.

[see the parent code]

This example has boxes drawn around each span within a span to show the nesting more clearly:

Quick brown fox jumps over the lazy dog.

[a screen capture of the boxes]

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%}?

Help with selectors

If you have trouble remembering what the selectors mean then see the following chart and the excellent Selectoracle website.

Some CSS selectors
Example
Pattern Means Selector Matches
(blank) ancestor p.example span spans that are between <p class="example"> and its </p>
> parent p.example > span spans that are immediate children of <p class="example">
+ sibling h2 + p any p that immediately follows a h2
. class attribute p.example any p that has example as a word in its class attribute
# id attribute span#warning only the span that includes id = "warning" in its attributes
:first-child itself a first child span:first-child any span that is the first child of its parent

For more examples of selectors

See


http://www.cs.dal.ca/~jamie/course/CS/3172/examples/CSS/nesting/nesting.html
Version:
01 November 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