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;
}
1.4.2 Understanding the order of shorthand values
TOP, RIGHT, BOTTOM, LEFT
padding: 1em 2em 1em
How to remember the three value given?
It surely consist of Top-Right-Bottom-Left, and left value is miss so its value will equal right:
padding: 1em 2em 1em (2em)
HORIZONTAL, VERTICAL
Two value represent Cartesian grid: x for horizontal and y for vertical:
#main-nav a {
color: white;
background-color: #13a4a4 !important;
padding: 5px;
border-radius: 2px;
text-decoration: none;
box-shadow: 10px 20px #6f9090; /* Shadow offset 10px to the right and 2px down*/
}
2 Working with relative units
2.1 The power of relative values
Relative unit: late-binding — the content and its style aren't pulled together until after the authoring of both is complete.
2.2 Ems and rems
em: multiple by the font-size
<span class="box box-small">Small</span>
<span class="box box-large">Large</span>
.box {
padding: 1em;
border-radius: 1em;
background-color: lightgray;
}
.box-small {
font-size: 12px;
}
.box-large {
font-size: 18px;
}
2.2.1 Using ems to define font-size
<body>
We love Coffee
<p class="slogan">We love Coffee</p>
</body>
body {
font-size: 16px;
}
.slogan {
font-size: 1.2em; /* Inherit from body*/
}
If font-size equal 16px, your want 12px, the value of ems is :12/16=0.75em
If font-size equal 16px, your want 18px, the value of ems is :18/16=1.125em
EMS FOR FONT SIZE TOGETHER WITH EMS FOR OTHER PROPERTIES
<body>
We love Coffee
<p class="slogan">We love Coffee</p>
</body>
<style>
body {
font-size: 16px;
}
.slogan {
font-size: 1.2em; /* Calculate : 16px * 1.2 = 19.2px */
padding: 1.2em; /* Calculate: 19.2px * 1.2 = 23.04px */
background-color: gray;
}
</style>
Element will calculate the value of font-size and uses that value for other properties.
THE SHRINKING FONT PROBLEM
<body>
<ul>
<li>Top Level</li>
<ul>
<li>Second Level</li>
<ul>
<li>Third Level</li>
<ul>
<li>Fourth Level</li>
<ul>
<li>Fifth Level</li>
</ul>
</ul>
</ul>
</ul>
</ul>
</body>
<style>
body {
font-size: 16px;
}
ul {
font-size: .8em;
}
</style>
2.2.2 Using rems for font-size
<body>
<ul>
<li>Top Level</li>
<ul>
<li>Second Level</li>
<ul>
<li>Third Level</li>
<ul>
<li>Fourth Level</li>
<ul>
<li>Fifth Level</li>
</ul>
</ul>
</ul>
</ul>
</ul>
</body>
<style>
:root {
font-size: 1em;
}
ul {
font-size: .8rem;
}
</style>
Pseudo-class:root
selector is identical to html
tag selector except that has higher specificity.
Suggestion:
- Rem for font-size
- Pixel for border
- Em for most properties