You starting a web site and want to style this header
h1 {
font-size: 1.5em;
color: rebeccapurple;
}
<h1>My Title</h1>
But then you want to use an h1 somewhere else and it needs different styles.
So you give this one a class name.
.title {
font-size: 1.5em;
color: rebeccapurple;
}
<h1 class="title">My Title</h1>
Latter your system has grown and you want to add a title to some other component.
You forget about the first one and give the new one the class of title also.
.title {
font-size: 1.5em;
color: rebeccapurple;
// more styles
}
<h1 class="title">My Title</h1>
// later you add this
.title {
font-size: 1em;
color: notpurple;
}
<h1 class="title" >Some Component Title</h1>
Body
Header
Main
Footer
Nav
Search
Login
Ordered List
Unordered List
List Items
Form
Button
Form
Button
Content
Paragraph
Ordered List
Unordered List
List Items
Video
Form
Inputs
Img
Paragraph
Ordered List
Unordered List
List Items
How do styles cascade?
Body
Header
Main
Footer
Nav
Search
Login
Ordered List
Unordered List
List Items
Form
Button
Form
Button
Content
Paragraph
Ordered List
Unordered List
List Items
Video
Form
Inputs
Img
Paragraph
Ordered List
Unordered List
List Items
.header {
font-family: Arial;
}
Body
Header
Main
Footer
Nav
Search
Login
Ordered List
Unordered List
List Items
Form
Button
Form
Button
Content
Paragraph
Ordered List
Unordered List
List Items
Video
Form
Inputs
Img
Paragraph
Ordered List
Unordered List
List Items
.header {
font-family: Arial;
}
.login {
font-family: Helvetica;
}
Body
Header
Main
Footer
Nav
Search
Login
Ordered List
Unordered List
List Items
Form
Button
Form
Button
Content
Paragraph
Ordered List
Unordered List
List Items
Video
Form
Inputs
Img
Paragraph
Ordered List
Unordered List
List Items
body {
font-family: Arial;
}
Body
Header
Main
Footer
Nav
Search
Login
Ordered List
Unordered List
List Items
Form
Button
Form
Button
Content
Paragraph
Ordered List
Unordered List
List Items
Video
Form
Inputs
Img
Paragraph
Ordered List
Unordered List
List Items
p {
font-family: Arial;
}
Global element selectors
Body
Header
Main
Footer
Nav
Search
Login
Ordered List
Unordered List
List Items
Form
Button
Form
Button
Content
Paragraph
Ordered List
Unordered List
List Items
Video
Form
Inputs
Img
Paragraph
Ordered List
Unordered List
List Items
.title {
// styles
}
Global class selectors
.title {
// styles
}
Name collisions.
It's the place to normalize your styles
Styles applied here apply to everything.
Class Naming collision.
Put all normalizations in one place
And keep them minimal
A scoped container of JavaScript and CSS
var shadow = someNode.createShadowRoot();
shadow.innerHTML = '<p>Here is some new text</p>';
Only in Chrome and Safari 10
Shadow DOM CSS can't affect global space
Can only style it externally through inheritance of the host
(Block Element Modifier)
Class naming convention
block__element--modifier
We scope our CSS to the components that use them. By hashing classes and class selectors when we bundle up the app.
// app.css
.title{
//some style
}
1. Author your css strictly with class selectors
// app.js
import styles from "./app.css";
// will be a hash of the title class
styles.title
2. Import those .css files into your .js files and compose them.
// app.css
.title_4hfkw{
//some style
}
// app.js
import styles from "./app.css";
styles.title // title_4hfkw
3. CSS Modules Converts the class names in both places to unique hashes.
You get Webpack for free in the JS starter
npm i -g ldsjs --registry http://icsnpm.ldschurch.org
Note: CSS-Modules does hash animation names