the need for selectors¶
there is a need for more accurate / selective settings
remember when we styled our first hyperlink ? our CSS clause was something like this:
a {
color: red;
font-family: times;
}but the settings apply on ALL <a> elements
that may be what we want, but in some cases we also need more selective mechanisms
id= : assign a unique identifier¶
an element that is unique in your document can be attached a unique identifier
class= : styling elements by class¶
it is also possible to create arbitrary groups of elements
so that they can be styled together
this is simply done by setting a
classattributean element can - and often has - several classes
e.g.
class="one-class another-class"
summary of basic selectors¶
let’s summarize
| selector | applies to elements |
|---|---|
p | any element tagged <p> |
#someident | that have id='someident' |
.someclass | that have someclass in their class attribute |
and you can create combinations, e.g.
| selector | applies to elements |
|---|---|
h1.someclass | tagged <h1> and of class someclass |
h1.someclass#someid | tagged <h1> and of class someclass and with id='someid' |
.yes.no | any element that has class yes and class no |
cascading and inheritance¶
cascading:
what happens if several rules define, say, thecolorproperty on an element ?inheritance:
what happens if no rule defines thecolorproperty on an element ?
in a nutshell :
| cascading | inheritance |
|---|---|
| the most specific rule wins | if not set on me, take the value from my parent |
cascading & specificity¶
in a nutshell, the intuition behind the specificity rules is
the
styleattributealways winsotherwise if the selector specifies an
id, it winsotherwise if you have specified a
class, it will applyotherwise rules based on the element’s tag will apply
otherwise, use wildcard rules (you can use
*as the selector)and finally, use the browser’s default properties if set
how to compute specificity for combined selectors
for combined selectors, specificity can be reasonably approximated as follows :
for each property setting, compute a tuple with
1 or 0 whether the property setting is in a
style=attributenumber of applicable
ids in the selectornumber of applicable
classes in the selectornumber of applicable
elements in the selector
compare the tuples - like Python would do
specificity example¶
in the 4 examples below, the CSS is unchanged throughout; we will see
the
<p>element with astyle, anidand aclassattributes
so all the CSS rules match, but thestyle=clause winsthen we drop the
styleattribute, and theidrule winsand we drop the
idattribute, so theclassrule winsand finally when we drop the
classattribute, and there is only one rule left to apply
#(1) embedded style= wins¶
#(2) then id= wins¶
#(3) then class= wins¶
#(4) then the element’s tag wins¶
inheritance¶
inheritance - why¶
the point is that we do not style the <p> and <li> elements specifically
so in this case the properies are fetched from their parent (the <div> element)
(note that not all properties behave that way though)
inheritance - what¶
Not all properties are inherited though
In CSS, typography and color properties usually inherit by default (making it
easy to style a whole page at once), while box-model properties (like border,
margin, padding, and width) do not. If borders inherited, every single nested
element would get its own box, creating a visual nightmare!
color, font-family, font-size,
line-height, text-align,
visibility, cursor,
border, margin, padding, width, height, background, position, display, z-index
inheritance & the body rule¶
so, it is common practice to create a rule for the body element
this way we can tweak the global style of the page
1 2 3 4body { font-family: Times; font-size: 18px; }
see also¶
here is some further reading on these topics
list of properties and terms
reference (hard to read) : detailed description of cascading and inheritance
more readable explanations on specificity :