CSS Overview
CSS Overview
CSS 4: 2012–Now
CSS 1 & 2 each were one big document
www.w3.org/Style/CSS/current-work
W3C CSS Working Group: Colors & Status Codes
Why CSS?
95.5% of all websites use CSS
Why?
As of September 2020
Separation of Concerns
Meaning: HTML
Presentation: CSS
Behavior: JavaScript
Separate content (HTML) from presentation (CSS)
Site-wide consistency: control how all content looks
using only 1 (or a few) CSS file/s
Apply different styles to same content in different
media:
1. Inline styles
2. Embedded styles
3. Linking to external styles
4. @import
Inline
Uses the style global attribute
Quick & easy to create, but difficult & time-consuming
to manage
HTML email
Embedded
Styles inserted inside <style> … </style>
<head>
<link rel="stylesheet" type="text/css"
href="/css/main.css">
</head>
HTML 5
<head>
<link rel="stylesheet" href="/css/main.css">
</head>
Start with a basic project 1
Create a css folder 2
Create /css/main.css 3
Link to /css/main.css 4
What should you name your CSS file?
It doesn’t matter
main.css
typography.css
client.css
search.css
navigation.css
Where should you place your CSS file?
/* Common */
blockquote, p, td {
font-family: Verdana, sans-serif;
font-size: 1em;
}
#footer {
font-size: .9em;
}
.emphasis {
font-weight: bold;
}
You can link to more than one style sheet, but you
should try to keep those links to a minimum
If you have more than one webpage, you really ought to
use an external style sheet
You might see advice telling you to add this as the 1st
line of your style sheet so the browser knows that
main.css is encoded as UTF-8:
@charset "utf-8";
* Unless you are using a build system like SCSS; more on that in CSS - Preprocessors
✏ SIDE NOTE
0–100 ms Instant
CSS CSSOM
To understand CSS, you have to understand
CSS CSSOM
Before a webpage appears in a viewport, the rendering
engine downloads the HTML & parses it to figure out
how to display the webpage on screen
No <tbody>
<tbody> added
Rendered DOM
<table> <table>
<tr> <tbody>
<td>Foo</td> <tr>
<td>Bar</td> <td>Foo</td>
</tr> <td>Bar</td>
<tr> </tr>
<td>Baz</td> <tr>
<td>Qux</td> <td>Baz</td>
</tr> <td>Qux</td>
</table> </tr>
</tbody>
</table>
CSS CSSOM
The CSSOM is built by the rendering engine using
specified stylesheet rules from:
CSS CSSOM
CSS CSSOM
CSS CSSOM
CSS CSSOM
HTML DOM
CSS CSSOM
JavaScript events can further change the DOM & the CSSOM
The big takeaway: when you’re working with CSS, you
are manipulating objects that will be rendered as boxes
Basic
Selectors
A CSS selector declares which DOM objects should
have particular styles applied to them
CSS 2: 13
CSS 3: 21
70 in total
1. Simple selectors 3. Complex selectors with
combinators
» Universal
» Type » Descendant
» Class » Child
» ID » Adjacent sibling
» Pseudo-classes » General sibling
» Pseudo-elements
» Attribute 4. Selector list
2. Compound selectors
Simple Selectors
A simple selector describes a single condition on an
element
HTML:
<p class="intro">…</p>
CSS:
.intro {
font-weight: bold;
}
Dot in front of the class name
in CSS, but no dot in HTML
You come up with the class names your project uses (or
you use those provided by a framework like Bootstrap)
Don’t use spaces
.📰 {
background-color: hsl(0,0%,76%);
}
.🤮 {
font-family: "Comic Sans", cursive;
}
The big rule for class names: describe function, not
appearance
Just be consistent!
ID
#id
IDs can make the cascade (more about that soon!) very
complicated
…target this ID
Other simple selectors
» Attribute selector
» Pseudo-class
» Pseudo-element
table.inventory matches
<table class="inventory">
table.inventory.northwest matches
<table class="inventory northwest">
.ws-gallery img { … }
ul > li { … }
h2 + p.lead { … }
Combinator Name Ex. Which B is selected?
␣ (space) Descendant A B Any descendant of A
> Child A > B Direct children of A
ul > li
ul > li a[title="home"]
CSS
.headshot {}
👎 👍
HTML Cleaner HTML
<aside> <aside>
<img <img src="…">
class="headshot" </aside>
src="…">
</aside> Better CSS using the
descendant combinator
CSS aside img {}
.headshot {}
Child Combinator
selectorA > selectorB
} 🤨
}
1st level changed from • to ▪,
but 3rd level remains ▪ because
that’s the default
Let’s change all 3 level defaults:
1st from • to ▪, 2nd from ◦ to •,
& 3rd from ▪ to ◦
An illustration of the difference between the descendant
& child combinators
Using > limits the scope of the styles
Selector List
selectorA, selectorB, selectorC
p { blockquote, p {
font-family: serif; font-family: serif;
font-size: 1em; font-size: 1em;
} }
blockquote {
font-family: serif;
font-size: 1em;
}
Any selector can be included in the list
blockquote, p {
font-family: Verdana, sans-serif;
}
p {
line-height: 1.5;
}
Turn this… …into this:
h1 { h1, h2 {
font-weight: normal; font-weight: normal;
font-size: 2.5em; font-family: serif;
font-family: serif; }
}
h1 {
font-size: 2.5em;
h2 {
}
font-weight: normal;
border-bottom: 1px h2 {
dotted black; border-bottom: 1px dotted
font-family: serif; black;
font-size: 1.8em; font-size: 1.8em;
} }
Good practice Elements, then IDs, then classes
blockquote,
Alphabetical order within
option,
p,
each grouping of selectors
td,
#sidebar,
.legalese {
font-family: Verdana, sans-serif;
font-size: 1em;
}
Formatting
Don’t do this:
h1 {color: dimgray;}
h1 {font-size: 1.4em;}
h1 {font-weight: bold;}
h1 {font-family: Verdana, sans-serif;}
Instead, combine related declarations
h1 {
color: dimgray;
font-family: Verdana, sans-serif;
font-size: 1.4em;
font-weight: bold;
}
Formatting CSS rulesets
selector {
property: value;
property: value;
property: value;
…
}
You do not actually have to put ; at the end of the last line in a ruleset, but that is a very bad
habit to get into
Use comments in CSS for the same reasons as in
HTML
HTML comments
<!-- blah blah html blah blah html -->
CSS comments
/* blah blah css css blah blah css css */
✏ SIDE NOTE
Design Pattern
@font-face
html
body
/* General */
<type selectors, A➝Z>
<ID selectors, A➝Z>
<class selectors, A➝Z>
/* <New Section> */
<type>
<ID>
<class>
…
<span>
&
<div>
HTML elements “work” without attributes & values
* & JavaScript
<span>
<span> is a text semantic element that creates an inline box &
does nothing else without CSS
<div class="lead-copy">
<p>
When a traveller in north central Massachusetts takes
the wrong fork at the junction of the Aylesbury pike just
beyond Dean’s Corners he comes upon a lonely and
curious country.
</p>
</div>
But what if the CSS rules conflict; e.g., what if CSS tells
the rendering engine to make all <p>s use serif and sans-
serif fonts?
If an element’s CSS declaration conflicts with another
declaration, the rendering engine uses the Cascade to
find a winner
2. Specificity
3. Order
Note: as of CSS Cascading and Inheritance Level 4 (August 2020), things have changed if the
Shadow DOM is involved; more on this in the Web Components presentation
Origin & Importance
CSS can originate from 3 places:
Why?
html {
font-family: "Source Sans Pro", sans-serif;
}
Do get:
p {
font-size: 36px !important;
}
html {
font-family: "Source Sans Pro", sans-serif;
}
AKA
Author
Author !important
User !important
Author & Author !important?
» Combinators: ␣, >, +, ~
» Universal selector: *
» Negation pseudo-class (but not the contents — that is
counted!): :not(.but-this-part-is-counted)
Inline styles (<style="foo">) always outweigh
everything else (a good reason to hate them 🤬)
a b c Total
* 0,0,0
li 1 0,0,1
.foo 1 0,1,0
#chapter1 1 1,0,0
ul li 1 × 2 0,0,2
.foo > li 1 1 0,1,1
ul ol li.steps 1 1 × 3 0,1,3
li.steps.mech 1×2 1 0,2,1
style="foo" ∞
A positive integer in a column
outweighs any positive
integer, even if it’s greater, in
a column to the right
26 in column b is outweighed
by 1 because it is in column a
Visual Studio Code shows you the specificity of a
selector when you hover over it & what it will look like
visually
Order
Later CSS in the stylesheet wins over earlier CSS
main.css:
index.html:
index.html:
index.html:
index.html:
<div class="callOut">
<p>
For the next 2 weeks…
</p>
</div>
.callOut {
background-color: #E6E8F2;
margin: 1em 1em 2em 1em;
padding: 1em;
border: 1px #ccc solid;
border-radius: 1em;
}
The result!
However…
That extra space at the
bottom really bothers me
That extra space at the
bottom really bothers me
To fix it, I put this in my CSS at lines 194–196:
.callOut > p {
margin-bottom: 0;
}
Lines 194–196:
.callOut > p:last-child {
margin-bottom: 0;
}
Lines 194–196:
.callOut > p:last-child {
margin-bottom: 0;
}
Lines 194–196:
.callOut > p:last-child {
margin-bottom: 0;
}
Specificity Specificity
Lines 194–196:
.callOut > p:last-child {
margin-bottom: 0;
}
Lines 194–196:
.callOut > p:last-child {
margin-bottom: 0 !important;
}
Never got to it
Lines 146–148:
#content p {
margin-bottom: 12px;
}
Lines 194–196:
#content .callOut > p:last-child {
margin-bottom: 0;
}
Adding #content to line 194 makes the 2nd ruleset win due
to specificity, so order & importance never comes into play
Lines 146–148: 100 + 1 = 101
#content p {
margin-bottom: 12px;
}
Adding #content to line 194 makes the 2nd ruleset win due
to specificity, so order & importance never comes into play
Nope—tied
Never got to it
Lines 146–148:
#content p {
margin-bottom: 12px;
}
Lines 194–196:
.callOut > p:last-child {
margin-bottom: 0;
}
Lines 194–196: 10 + 1 + 10 = 21
.callOut > p:last-child {
margin-bottom: 0;
}
Much better!
Then I realized that <div class="callOut"> doesn’t
always end with <p>
Look at <p>
What’s the use case for this? See all just ahead
revert value rolls back the property’s cascade,
depending upon who declared it: the browser, user,
or author
Author
If revert is rolling back a property’s
User style set by an author, & the user has
not set a style, it skips the user &
Browser rolls back to the browser, & so on
Spec
Most common use-case for revert: you have a heavily-
modified selector & you want to revert back to the
browser’s defaults (remember, most users never set any
styles, so that one is skipped over)
all
* all: unset is buggy in Safari & sets color to black, preventing you from setting another color; the
* workaround is to use -webkit-text-fill-color everyplace you also use color; Safari 14 fixes this
Tools
Books
Great overview of HTML5
& CSS2 (& some CSS3)
References
developer.mozilla.org/en-US/docs/Web/CSS/Reference
Color picker
theolabrothers.com
$0
annystudio.com/software/colorpicker/
ColorPro
www.iconico.com/colorpro/
$30
Thank you!
[email protected]
www.granneman.com
ChainsawOnATireSwing.com
@scottgranneman
[email protected]
websanity.com
CSS Overview
Selectors, Integration, Inheritance, Cascading
2015-05-10 1.8: Added info about CSS 4; clarified that <span> &
<div> draw boxes; added additional names of directories that are
always created; changed “What Google prefers” to “… uses”;
removed Hues & added Sip to Color Pickers; fixed URL &
screenshots for CSS Vocabulary; moved Viewport Resizer to
Bootstrap; for Separation of Concerns, added “& Meaning” to
HTML
Changelog
Attribution. You must give appropriate credit, provide a link to the license, and indicate if changes were made.
You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your
use. Give credit to:
Share Alike. If you remix, transform, or build upon the material, you must distribute your contributions under
the same license as the original.
No additional restrictions. You may not apply legal terms or technological measures that legally restrict others
from doing anything the license permits.