PART1 REVIEWING THE FUNDAMENTALS
1 Cascade, specificity, and inheritance
CSS is not a programming language.
1.1 The cascade
Basically, CSS is about declaring rules.
The code of CSS is becoming complex as project growing. To accomplish same thing we can use multiple way. So the key part of CSS development comes down to writing rules in such a way that they're predictable.
Example:
-
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="./style.css">
<title>Document</title>
</head>
<body>
<header class="page-header">
<h1 id="page-title" class="title">Wombat Coffee Roasters</h1>
<nav>
<ul id="main-nav" class="nav">
<li><a href="/">Home</a></li>
<li><a href="/coffees">Coffees</a></li>
<li><a href="/brewers">Brewers</a></li>
<li><a href="/specials" class="featured">Specials</a></li>
</ul>
</nav>
</header>
</body>
</html> -
CSS
h1 {
font-family: serif;
}
#page-title {
font-family: sans-serif;
}
.title {
font-family: monospace;
}
There are conflicting declaration for <h1>
tag.
When declarations conflict, cascade consider three things:
-
Stylesheet origin where the stylesheet come from?
-
Selector specificity which selector take precedence over which.
-
Source order order in which styles are declared in the stylesheet.
A quick review of terminology:
declaration a line of CSS code composed by a property and a value:
color: black;
declaration block: multiple declaration within a pair of curly brace:
{
color: black;
font-family: Helvetica;
}selector: prior to declaration block.
body {
//...
}ruleset: selector + declaration block, concisely call "rule".
body {
color: black;
font-family: Helvetica;
}at-rules: language constructs beginning with an "at" symbol, such as
@import
rules or@media
queries.
1.1.1 Understanding stylesheet origin
Type, or origin of stylesheet:
-
author styles: write by developer.
-
user agent styles: browser default style which priority lower than author style.
User agent can custom but it is rarely used and beyond your control.
USER AGENT STYLES
Besides author styles, other styles will apply user agent styles.
IMPORTANT DECLARATIONS
!important
mark the style is higher-priority.
color: red !important;
So the order:
- author-important
- author
- user agent
💡 The cascade independently resolves conflicts for every property of element. So you set the bold font, but will not effect margin or other properties.
1.1.2 Understanding specificity
specificity:
- inline style
- selector
INLINE STYLES
<a href="/specials" class="featured" style="background-color: orange">Specials</a>
To override the inline styles, we can using importance
to the declaration:
.featured {
background-color: red !important;
}
There are nothing happen if using !importance
in inline style:
<a href="/specials" class="featured" style="background-color: orange !important">Specials</a>
SELECTOR SPECIFICITY
<!DOCTYPE html>
<html lang="en">
<body>
<header class="page-header">
<h1 id="page-title" class="title">Wombat Coffee Roasters</h1>
</header>
</body>
</html>
html body header h1 { // Four tags
color: blue;
}
body header .page-header h1 { // One class + three tags
color: orange;
}
.page-header .title { // Two classes
color: green
}
#page-title { // One ID
color: red;
}
👋 Pseudo-class selectors and attribute selectors have the same specificity as a class selector.
👋 The universal selector(*
) and combinators(>
, +
, -
) have no effect on specificity.
There no effect happen probably since the declaration didn't overtake other specific rule.
A NOTATION FRO SPECIFICITY
Selector | IDs | Classes | Tags | Notation |
---|---|---|---|---|
html body header h1 | 0 | 0 | 4 | 0, 0, 4 |
body header .page-header h1 | 0 | 1 | 3 | 0, 1, 3 |
.page-header .title | 0 | 2 | 0 | 0, 2, 0 |
#page-title | 1 | 0 | 0 | 1, 0, 0 |
1.1.3 Understanding source order
If the origin and specificity are the same, later win!
1.1.4 Two rules of thumb
- Don't use ID selector
- Don't use
!importance
1.2 Inheritance
The last way to receive the style for the element: Inheritance
Only some style will be inherited:
- text:
color
,font
,font-family
,font-size
,font-weight
,font-variant
,font-style
,line-height
,letter-spacing
,text-align
,text-indent
,white-space
,word-space
- list:
list-style
,list-style-type
,list-style-position
,list-style-image
- table:
border-collapse
,border-spacing
- other...
🚀 Use your DevTools!
1.3 Special Values
Two special Values applying in any property to help manipulate cascade:
- inherit
- initial
1.3.1 Using the inherit keyword
inherit To inherit the declaration from its parent.
Implement the footer:
<footer class="footer">
© 2016 Wombat Coffee Roasters —
<a href="/">Term of use</a>
</footer>
a:link {
/*Global link color for the page*/
color: blue;
}
.footer {
color: #666;
background-color: #ccc;
padding: 15px 0;
text-align: center;
font-size: 14px;
}
.footer a {
color: inherit; /* Inherit from parent to override global link style*/
text-decoration: underline;
}
1.3.2 Using the initial value
initial Set the property to its initial value.
Set the property but not the element. For example,
display: initial
equal todisplay: inline
regardless of what type of element element apply it to.
1.4 Shorthand properties
Shorthand properties set the several value at one time:
font: italic bold 18px/1.2 "helvetica", "Arial", sans-serif;
1.4.1 Beware shorthand silently overriding other styles
<h1 class="title">Wombat Coffee Roasters</h1>
h1 {
font-weight: bold;
}
When using font
shorthand:
.title {
font: 32px Helvetica, Arial, sans-serif;
}
Shorthand silently override the other value:
h1 {
font-weight: bold;
}
/* Equivalent Code*/
.title {
font-style: normal;
font-variant: normal;
font-weight: normal;
font-stretch: normal;
line-height: normal;
font-size: 32px;
font-family: Helvetica, Arial, sans-serif;
}