Selectors determine what style rules are applied to what
parts of the HTML document.
- Inline styles apply to the tag where the style attribute is
used. Here
<p style="color: red; font-weight: bold">Do not do that!</p>
the style applies only to this p.
- Styles in a style section or external style sheet have the form
target { style instructions }
The target is called the selector. It determines to what the
style instructions are applied.
- Type selector: Where
target is an HTML tag. The style applies to all of those.
We've been using this one.
- ID selector.
- Any HTML tag may be given an identifier, which is any alphanumeric
string. It is assigned with an id attribute.
- An id should be unique within the document: There can be only one
element with id="fred".
- Use a hash mark in front of the id to form a selector.
#fred { color: red; font-weight: bold }
applies to
<p id="fred">Do not do that!</p>
- The class selector. [ Style Classes Source ]
- The type selector applies to all tags of a certain type, and the id
applies to exactly one. The class selector gives us a “some”
that's not an “all”.
- You can create different style classes like this:
p.warning { color: red; font-weight: bold }
- You can refer to the class with the class attribute:
<p class="warning">Don't dare do that, either!!</p>
- You can create general classes without just a dot:
.barney { color: blue }
These can be used with class on any sort of tag.
- When the class attribute is not given, the base style is
used.
- The attribute selector.
- Selects based on an attribute value.
- [attribute=value] selects any element with the given
attribute, say [align="center"] { font-size: 130% }
- element[attribute=value] selects any of the
specified element with the given
attribute, say h1[align="center"] { font-size: 130% }
- The * is the universal selector. Useful in some combinations
discussed below.
- Group selector.
- Make a comma-separated list to apply a style to several selectors at once.
h1, h2, h3, p.title, #fred { color: green }
- Descendant selector.
- When selectors are listed without a comma, the style applies to the
last one when nested inside the one before.
p.index a { color: green }
- The above makes links green when they appear inside a class index
paragraph.
[ Some descendant selection Source ]
- Child selector.
- Similar to descendant, but uses a greater-than.
p.index > a { color: green }
- This turns green every a which is immediately inside an index paragraph.
- The difference from descendant:
- If you have
<p class="index">Go <a href="somewhere.html">over here</a></p>
both apply to a.
- If you have
<p class="index"><b>Go <a href="somewhere.html">over here</a></b></p>
only descendant applies to a.
- Treat the tag nesting as generations.
My grandson is my descendant, but is not my child. My son is both
my child and my descendant.
- Descendant is usually what you want.
- Because most CSS properties are inherited, the effects will often wind up
the same anyway.
[ Some child selection Source ]
- Adjacent sibling selector.
- The + sign is used to select an item when it immediately follows another
having the same parent.
p + p { margin-top: 5pt }
Applies the margin to a paragraph only when it immediately follows
another under the same parent.
- When you have a bunch of things in a row, this can apply a style to
all but the first one.
[ Some sibling selection Source ]
- Cascading.
- A given element may be subject to more than one selector.
- Generally, all of those styles apply.
- Except they could conflict, such as two different color specifications,
or two different font sizes.
- The cascade refers to the rules about who wins.
- Each specification has a precedence. The highest precedence wins.
Generally:
- Inline styles beat the style section, which beats external
sheets.
The designer can put general rules in a style sheet, but
override them in a particular page or element.
- Reference by id beats reference by class, which beats reference by
tag type.
The designer can give general rules for tags, but put some in
special classes which have other style, and still pick
specific elements (by id) for individual treatment.
- If equal on those terms, the last one wins. Unless one is marked
with a !important, in which case the last
important one wins.
- The book goes into much more detail about the precedence calculation.
- The span and div HTML tags.
- These are tags that don't do anything themselves. They exist to have
styles applied.
- Mostly useless without either an id, class or style attribute.
- Span is an inline element, div is a block element.
- So div will create a line break without styling. It does nothing else.
- <span style="font-style: italic"> is a long-winded way
to write <i>.
- <div style="margin-top: 1em; margin-bottom: 1em"> is verbose
version of <p>
- More likely: <span class="companyname">, which is styled
some special way in a style sheet.
[ Fancy Selectors Source ]
Some notes on the example