« resources

Company Workshop

Cascading Styles

The origins of HTML and CSS

workshops.oddbird.net/kc24/cascade/

Cmd/Ctr-K for Controls

Introductions…

  • Name & pronouns
  • Location
  • Brief relation to CSS (feeling, experience, role, etc)

Three Days Together

  1. CSS Mental Model
    cascading, declarative, and contextual
  2. CSS Layout
    flexbox, grid, and alignment
  3. CSS Architecture
    naming conventions & progressive enhancement

It’s a lot We’ll be Moving Quickly

  • It’s a lot to cover,
  • And we’ll move pretty quickly sometimes
  • Skimming past some demos that you can come back to,
  • But then really digging in where it makes sense

Takeaway A CSS Mindset

  • Don’t need to memorize everything
  • Understand the mental model, what’s possible
  • And I want to leave you with resources
  • You can come back to on your own time

While we’re together Let’s Discuss

  • Also feel free to stop me if you have questions
  • Or I’m skimming past something essential to you.
  • Q&A at end of each day
  • We can also stop along the way
  • I can’t see all of you, so you’ll need to interrupt me
  • (I don’t mind)

Cascading Styles

Let’s do this…

  • Any questions before we start?
  • We’re here to talk about…

CHSS*

Cascading HTML style sheets -- a proposal, Håkon W Lie, 10 Oct 1994
Håkon Lie, Cascading HTML style sheets – a proposal [w3.org]
  • CHSS
  • Håkon Lie, 1994
  • email, still archived online
Historical Style Sheet proposals
Historical Style Sheet proposals [w3.org]
  • not the only
  • not the first
  • solves a major problem for the new platform

Technology Is Political

  • not technical
  • but philosophical
  • political, like all decisions we make
  • designing a new platform or interface
The first world wide website in the WWW Hypermedia Browser/Editor, on the neXt desktop -- an exercise in global availability
  • Team at CERN
  • first web browser
  • Hypermedia WWW Browser and Editor
  • NeXt machine
  • graphic interface
Old IBM desktop with a text-only green on black display

You can’t make a web that’s world-wide, by saying it works on my machine, and everyone else is an edge-case.

The second browser, a text-only terminal with numbered links for keyboard control
Line Mode Browser, developed by Nicola Pellow
  • Nicola Pellow
  • Line Mode browser
  • text-only
  • no install
  • any device with an internet connection

Web for all. Web on everything.

— W3C Mission Statement

  • Mission statement of W3C
A zillion different devices of all sizes
Over the shoulder, a person wearing headphones, using a braille display attached to a laptop
  • includes a range of assistive technologies
  • like this braille display
  • or ‘screen readers’ which read what’s on the screen
Various home 'smart' speakers
  • also devices without any screen involved
Indiana Jones covered in cobwebs, looking frustrated.
  • The web is getting everywhere
  • And it’s kinda gross

Web for all. Web on everything.

— W3C Mission Statement

  • If we really want this to work
  • Everywhere for everyone
World Wide Web
World Wide Web Project [info.cern.ch]
  • leads to difficult constraints
  • documented on the original website
  • also still available
Resilient HTML [present, debug]
  • browsers ignore code they don’t understand

[DEMO]

  • individual attributes
  • entire HTML tags

Protect the Content

across devices, and over time

a web prime directive…
  • the code is optional
  • the content is essential
  • whatever else happens
  • browsers should protect the content
  • that’s why…
World Wide Web
World Wide Web Project [info.cern.ch]
  • we can still see
  • first website on modern browsers
Workshop page on oddbird.net displayed as black and white text on the WWW browser emulator
worldwideweb30.com c/o Remy Sharp [worldwideweb30.com]
  • modern website, on the first browser
  • (this is an emulator you can play with)
  • No css
  • No images
  • all the text is there

It is required that HTML be a common language between all platforms

— WWW Project, HTML constraints

  • for graphic design
  • limitation is more dramatic
  • to be a common language anywhere…

…This implies no device-specific markup, or anything which requires control over fonts or colors.

— WWW Project, HTML constraints

Web Design Will Never Happen

Viola WWW browser demonstrating early stylesheets
  • problem isn’t styles
  • early browsers internal stylesheets
The second browser, a text-only terminal with numbered links for keyboard control
Line Mode Browser, developed by Nicola Pellow
  • Even text-only browser
  • headings, paragraphs, etc
  • But the styles are different per browser
  • not per website
InDesign book-cover layout
  • not the way print works
  • define everything…
  • page size (static, forever)
  • export to PDF, jpg,
  • send it to the printer
Published book
Published book with badly super-imposed hand-pointer cursor and scribbled word click!
Published book
  • It just sits there
  • The same was true for other digital formats at the time…

PostScript, 1982 Page Description Language

The source code for PDFs
  • PostScript around almost a decade
  • page description language
  • Like print
  • describes a static page
  • how big is it? where does each thing go?
  • We set fonts, colors, etc.
  • We have complete control, but…

The fact we can control a paper page is really a limitation of that medium.

— John Allsopp, A Dao of Web Design

  • That control comes at a cost
  • The page, print or PDF, is a static medium
  • can’t adapt to context or preferences
  • a limitation of the medium

Web Styles Must Be Responsive

a radical shift!
  • web is different
  • for everyone, on everything
  • sites aren’t static pages
  • what we ship is not always what our audience sees
  • styles must adapt to situations…

@media min-width

Viewport dimensions
  • Not just the width of the viewport

@media prefers-reduced-motion

User needs & preferences
  • user needs & preferences

@media hover

Device interfaces
  • device interfaces

@supports container-type: inline-size

Browser capabilities over time
  • changing capabilities over time
  • Content that’s always changing

Web Design Seems Impossible

  • Under those constraints
  • Unknown content, unknown display, unknown overrides
  • web design (reasonably) seemed impossible, but…

HTML totally eliminates any visual creativity that a document’s designer might have.

— Roy Smith, www-talk “Re: Adobe’s PDF”

  • No one was happy with that outcome
  • When every site looks the same
  • That’s boring for both authors and users
  • People want visual creativity
Mosaic browser
Mosaic, 1993
  • Turning point, 1993
  • Mosaic added image tag to HTML
  • (Before web standards)
  • web styles suddenly possible
  • put everything in an image, and it never changes!
A pixelated broken-image icon, showing a photo torn at one corner
  • or it doesn’t show up at all
  • and we lose everything that makes HTML special
  • Can’t protect structured content in an image…
  • If it doesn’t load, it’s lost

The web would have become a giant fax machine where pictures of text would be passed along.

— Håkon Lie, 2014 interview

  • A real risk to the platform
Historical Style Sheet proposals
Historical Style Sheet proposals [w3.org]
  • 1993-1994
  • a rush of different proposals

…Allow the author to attach style hints.

— Håkon Lie, Cascading HTML style sheets – a proposal

  • author style hints

Styles are suggestions or hints about behavior, not rules

— Robert Raisch, Request for comments: STYLESHEETS

  • Not rules, but hints and suggestions.
  • (phrase shows up over and over)

Provide hints that the browser may or may not use.

— Håkon Lie, Cascading HTML style sheets – a proposal

A set of HINTS or SUGGESTIONS to the renderer which might be used

— Robert Raisch, Request for comments: STYLESHEETS

  • Sometimes all-caps
  • [slide]

Provide authors and publishers with stylistic influence without resorting to page description languages.

— Håkon Lie, Cascading HTML style sheets – a proposal

  • In Håkon’s proposal…
  • authors and publishers have
  • stylistic influence
  • without resorting to page description languages
  • without giving us full control
  • Because we’re not alone in this…
We're Not Alone (in big block letters, in space)
  • We’re part of an ecosystem
  • Our decisions impact other people
  • So we don’t get final say
Same We're Not Alone, with browser logos on top
  • We’re collaborating with browsers
  • (instructed to ignore us)
Same We're Not Alone, with browser logos and a guy in construction clothes carrying a laptop
  • Also the people who interact with our sites
  • (super cute in their handy tool belts)
  • On their chosen devices
  • (with varying capabilities)
  • And we’re also collaborating with…
We're Not Alone, with browser logos, a guy in construction clothes carrying a laptop, and ET
  • aliens 👽
  • Maybe, who knows?
  • We should collaborate with them…
ET and the kids flying their bicycles across the sunset
  • just trying to get home.

The CHSS proposal is explicitly designed around this collaboration

This proposal tries to soften the tension between the author and the reader.

— Håkon Lie, Cascading HTML style sheets – a proposal

  • Designed to soften the tension

The user/browser specifies initial preferences and hands the remaining influence over to the document [authors].

— Håkon Lie, Cascading HTML style sheets – a proposal

  • By allowing everyone to contribute styles
  • In an orderly fashion
  • Using the same language!
  • First the browser and user,
  • and then the document
css…
/* a 'declaration' */
property: value;
  • Each of us writing
  • our style hints as declarations,
  • a property and a value
css…
/* a 'declaration' */
color: blue;
css…
/* a 'declaration' */
color: blue 64%;
  • In the initial proposal
  • We could also add a percentage to every declaration
  • How much we care
  • Here we’re handing over exactly 36% of the influence
  • To our collaborators
css…
/* a 'declaration' */
color: blue !important;
  • That idea was simplified later
  • Into a binary
  • Every declaration has either 0% or 100% importance
  • But we’ll come back to that
css…
/* a 'declaration' */
color: blue;
css…
/* a 'block' */
{
  background: white;
  color: mediumVioletRed;
}
  • group them into blocks
html…
<!-- 'inline' (or 'element attached') styles -->
<p style="
  background: white;
  color: mediumVioletRed;
"></p>
  • attach directly to elements in HTML.
html…
<p style="color: mediumVioletRed;"></p>
<p style="color: mediumVioletRed;"></p>
<p style="color: mediumVioletRed;"></p>
<p style="color: mediumVioletRed;"></p>
<p style="color: mediumVioletRed;"></p>
  • but that can get repetitive
  • if we want to re-use the same styles on multiple elements
  • ideally, create design systems

…A simple mapping between HTML elements and presentation hints.

— Håkon Lie, Cascading HTML style sheets – a proposal

  • CSS helps by providing “a simple mapping…”
css…
/* a selector, with attached declarations… */
/* a 'style rule' (or 'rule set') */
p {
  background: white;
  color: mediumVioletRed;
}
  • Selectors
  • Which we can attach to declaration blocks,
  • forming rule-sets.
  • Each selector maps our rule set…
html…
<p>This is a short paragraph</p>
css…
p {}
  • … to elements in the document.
  • (this is the majority of the CSS we write)
  • Then we can…
html…
<!-- a 'style sheet' -->
<link rel="stylesheet" href="styles.css">
css…
/* another 'style sheet' */
@import url('styles.css');
  • Combine our rule sets into style sheets,
  • and import them from our document,
  • or other style sheets.
We're Not Alone, with browser logos, a guy in construction clothes carrying a laptop, and ET
  • Not just us as document authors…
  • Anyone involved can contribute style sheets
  • The users interacting with our document,
  • and the user agent (or browser) they use.

An ordered list (cascade) of style sheets.

— Håkon Lie, Cascading HTML style sheets – a proposal

  • Then all these style sheets get merged…
  • Into an ordered list (or ‘cascade’) of styles
  • that should be applied to the same document.
  • Of course, this leads to conflicts!…
html…
<button type="submit" id="send" class="action">
css…
button          { background: gray; }
.action         { background: darkBlue; }
[type=“submit”] { background: darkGreen;
                  background: var(--submit, black); }
#send           { background: maroon; }
💥 Conflicts!
  • We have any number of declarations
  • coming from different sources
  • likely applying different values
  • to the same properties, on the same elements!
  • What are we going to do?
  • Stack all these background colors on top of each other?
  • Create a rainbow gradient?

#484d63

  • Mix them together into a sludge?
  • This was the original idea,
css…
/* a 'declaration' */
color: blue 64%;
  • Remember those importance percentages?
  • We could use them for mixing!
  • But the results aren’t great
  • And it’s not clear how you would ‘mix’ something like a font
A comic-sans papyrus mashup font specimen with upper/lower latin characters and punctuation, on textured parchment with a drawing of Anubis laying face-up on snoopy's dog house, and a small tagline claiming the most beutifulest font ever created. ever. full special character set included (not shown here)
Comic Papyrus by Ben Harman
  • But I wish we had tried harder

For Browsers Every CSS Property
On Every HTML Element,
Must Have a Single Value

  • Browsers must resolve conflicts somehow
  • Before they can render the page
  • Every CSS property
  • on every HTML element
  • must have a single value

596 Distinct Property Names

* including Editor’s Draft proposals

w3c List of CSS Properties
  • All 596 distinct properties
  • (or at least the long-hands from that list)

Every <button> Needs One Background-Color
& One Text Color
& One Padding-Left
& One Margin-Inline-End
& …

  • Every button needs
  • one text color,
  • one padding-left,
  • one margin-inline-end, etc.

Input… A “Cascade of Style Sheets”

applying declarations to selectors

  • We start the process with a cascade of style sheets
  • (from authors, users, and browsers)

The Cascade (Process) Resolves Conflicts

removes extra values

  • what we call ‘the cascade’ now
  • is a browser process
  • merging all the stylesheets, rules, and declarations
  • (‘cascading’ them together)
  • and resolving conflicts
  • That process ensures
  • we don’t have extra values

Inheritance… Provides Dynamic Defaults

fills in missing values

  • And then inheritance helps clean up
  • By filling in some of the missing values
  • We’re not going to set all 596 ourselves!
  • So good defaults are essential,
  • And there’s a whole separate step for it
  1. Filtering
  2. Cascading (includes specificity)
  3. Defaulting (includes inheritance)
  4. Resolving
  5. Formatting
  6. Constraining
Value Processing (or ‘value resolution’)
  • After the cascade has completed
  • These are steps in a larger process called value resolution
  • Everything required for converting an incomplete set of (possibly-contradictory) style sheet declarations
  1. Filtering (0+ declared values)
  2. Cascading (0|1 cascaded value)
  3. Defaulting (1 specified value)
  4. Resolving (1 computed value)
  5. Formatting (1 used value)
  6. Constraining (1 actual value)
  • into fully fleshed-out and rendered styles
  • Each step gets us closer to that actual value
  • Filtering to get all the relevant declared values
  • Cascading to remove extra values
  • Defaulting to fill in anything missing
  • And then several steps that help us resolve relative or imprecise values
screenshot
[Cascading & Inheritance]
  • Cascading and Inheritance are central to all of this,
  • The spec is named for those two steps
  • But they aren’t the whole story

Cascade & Inheritance Internal Browser Algorithms

  • On the one hand,
  • This is all internal logic, right?
  • Just an algorithm that browsers use…
Tina Turner as Aunty Entity in the Mad Max Thunderdome, with the law 'Two styles enter, one style leaves' in bold text
Necessary Browser Algorithms
  • to thunder-dome our declarations
  • Two styles enter, one style leaves…
The Crab Nebula photo: Webb Space Telescope
Description of Web Reality
  • Almost like the laws of physics…
  • A way of describing the universe around us,
  • The way the world works.
  • That’s all true, but also…
Yoda reaching out to control the force, surrounded by jungle swamp
Expressive Authoring Tools
  • tools provided to us, intentionally
  • part of the language,
  • designed for us to communicate…
We're Not Alone, with browser logos, a guy in construction clothes carrying a laptop, and ET
  • all of us,
  • not just as authors,
  • but also as users…

Express… Purpose & Priority

  • At each step we get tools
  • to help us express purpose and priority
  • Of our hints and suggestions.
  1. Filtering

  2. Cascading
  3. Defaulting
  4. Resolving
  5. Formatting
  6. Constraining
Get a list of declared values (possibly empty)
  • That starts with filtering our stylesheets
  • To get all the declared values
  • that apply for a given property on a specific element
css…
.short { border: thin double; }
.long {
  border-top: thin double;
  border-left: thin double;
  border-bottom: thin double;
  border-right: thin double;
  border-image: none;
}
Expand Shorthands
  • This is also when we expand shorthand properties
  • Elements don’t have a border property
  • They have four border properties, and a border-image
  • Border is shorthand for setting all of them
css…
.short { border: thin double; }
.long {
  /* declared */
  border-top-width: thin;
  border-top-style: double;

  /* reset to 'initial' value */
  border-top-color: currentColor;
  border-image: none;
}
  • Four sides are also shorthand, including
  • The width, style, and color of each
  • Shorthands also reset the un-specified values
  • Initial value of border-color is currentColor
  • (We’ve set that implicitly)
  • Also border-image which is reset-only here

To start over Use Shorthand Properties

  • Shorthands are great for starting over
  • Reset everything
  • Build from the ground up
  • But they’re risky if…

To adjust details Use Longhand Properties

  • You just want to make small adjustments
  • Long-hand properties are more precise
  • And leave everything else intact
  1. Filtering (list of declared values)
  2. Cascading

  3. Defaulting
  4. Resolving
  5. Formatting
  6. Constraining
Get a single cascaded value (possibly empty)
  • Once we have our list of declared values for a property
  • We know what conflicts exists
  • And we can use the cascade to resolve those conflicts
Mechanical coin sorter on a table, with a ramp at the top and a small-to-large series of slots
  • Cascade works like a coin sorter
  • Instead of measuring the size of a coin
  • Measuring the priority of each declaration
  • See which ones make it farthest
Same coin sorter, with all slots crossed off except for the largest one with coins in it, and an arrow pointing at those coins that says 'move on'
  • And we discard everything smaller
  • Once the coins are sorted, we can discard any empty columns
  • We ignore all but the largest coins
Mechanical coin sorter duplicated multiple times in semi-transparent overlays
  • And then if we still have conflicts, we do it again
  • Over and over, until we have a single coin
  • Each time comparing different features
  • Not just size, but shape, width, metal, whatever
  • Ok, but not coins -
css…
/* a 'declaration' */
property: value;
  • We’re talking about declarations.
  • So what are the things we can compare?
    • &
  • There are 7 steps in the cascade
  • You can probably name a few of them
  • (the first step has two parts)
  • Any thoughts here?
  • What are some criteria we can look at, in order to prioritize declarations and resolve conflicts?
The Cascade (as a funnel) [present, debug]

this forms the series of steps in our funnel, asking…

  1. Who requested it? (origin)
    1. How much do they care? (importance)
  2. Does it come from the shadow DOM? (context)
  3. Is it an inline style? (element attached)
  4. What layer is it in? (cascade layers)
  5. How specific is the selector? (specificity!)
  6. How close is the scope root? (proximity, new)
  7. Which value was declared last? (order, single)

For Today

  1. Who requested it? (Origins)
    1. How much do they care? (Importance)
  2. Does it come from the shadow DOM? (Context)
  3. Is it an inline style? (Element Attached)
  • For today, only looking at the first three
  • (and especially the top one, in two parts)
  • We’ll come back to the rest on Thursday
  • Since they form the basis of most CSS conventions

❗Important Styles Always Win

  • We could put importance alone at the top
  • Important styles will always win over normal styles
  • But if we stop there, we’re missing the point
Darth Vader
choking a rebel soldier
as storm troopers watch
  • This isn’t a magic trump card
  • For stomping around and killing our enemies
We're Not Alone, with browser logos, a guy in construction clothes carrying a laptop, and ET
  • This is about collaboration

This proposal tries to soften the tension between the author and the reader.

— Håkon Lie, Cascading HTML style sheets – a proposal

  • Softening the tension
  • Between author and reader

The user/browser specifies initial preferences

— Håkon Lie, Cascading HTML style sheets – a proposal

  • So we take turns
  • First, the user and browser provide defaults

First… 🖥 User Agent Styles

from the user agent origin

aka Browser Defaults
  • User agent (browser) starting us off
  • Providing legible defaults…
screenshot
(Firefox DevTools: settings/inspector/show browser styles) [CHSS Proposal]

demo:

  • inspect styles
  • none from document, only from browser
  • remove: * { all: initial; }
  • Without browser styles,
  • Unreadable wall of content…
css…
/* initial values */
display: inline;
background: transparent;
color: CanvasText;
font-style: normal;
flex-basis: auto;
/* etc. */
Initial Values, from specifications
  • Initial values
  • Are defined for each property
  • The same on all elements
  • Always display:inline, even on divs…
css…
html { display: block; }
body { display: block; }
p { display: block; }
div { display: block; }
article, aside, footer, header, hgroup,
main, nav, search, section { display: block }
address { display: block; }
blockquote { display: block; }
/* etc etc */
  • Browsers add display:block
  • everywhere we expect it
css…
body { margin: 8px; }
a:link {
  color: blue;
  text-decoration: underline; }
h1, h2, h3, h4 /* etc */ {
  font-weight: bold;
  font-size: something big I dunno; }
  • They also add things like
  • 8px margin on the body
  • Link colors and underlines
  • Heading styles, large bold text
  • Space around paragraphs and lists, etc

User Agent Styles

Collected by Jens Oliver Meiert
  • These are the user agent styles
  • And they apply to every site on the web
  • With only minor differences between browsers

Second… 👥 User Styles

from the user origin

aka User Preferences
  • After browser defaults
  • Users apply their preferences

[CSS] provides author and reader with the same notation

— Håkon Lie, Cascading HTML style sheets – a proposal

  • Originally using CSS syntax as well
Safari settings panel, advanced tab, with a red scribble highlighting a style sheet option, with none selected by default.
Safari: settings / advanced / style sheet…
  • Safari still supports this option
  • In the settings panel
  • Upload a stylesheet
Language and appearance preferences in Firefox include light/dark mode, colors, fonts, and zoom level
Firefox appearance settings
  • Most other browsers have switched
  • To providing limited preferences through a graphic interface

User Styles

  • Safari/WebKit:
    Settings / Advanced / Style sheet…
  • Firefox/Gecko:
    about:preferences
  • Chrome/Blink:
    chrome://settings/appearance
  • The details are different
  • But the results are the same
  • Users get to have preferences
We're Not Alone, with browser logos, a guy in construction clothes carrying a laptop, and ET
  • And remember,
  • When we talk about ‘users’ or ‘readers’
  • on the web…

Remember… We Are All Users

all of us read other people’s web sites

  • We’re talking about all of us
  • We all use the web

We can all Have Preferences

I changed mine, and recommend it!
  • We’re all allowed to have preferences
  • I set a few, and recommend trying it
  • Worried as dev “but I’ll see something different”
  • That’s the point!
  • Remember to check with different settings

Finally… 🎨 Author Styles

from the author origin

  • Finally, ‘document’ or ‘author’ styles
  • (also us, making web stuff)

🗺 Cascade Origins

  1. 🖥 User Agent Defaults (applied first)
  2. 👥 User Preferences (override defaults)
  3. 🎨 Author Styles (too powerful)
  • We come last, and get highest priority
  • We override the user preferences
  • Which override the browser defaults
girl reaching into a white bucket, with paint all over her hands and the floor around her photo: Tanaphong Toochinda
  • So we can kinda just do whatever
  • Smear our brands all over everything
Darth Vader
choking a rebel soldier
as storm troopers watch
  • But we tend to stomp around
  • choking people with our special powers
  • So that’s not great

This proposal tries to soften the tension between the author and the reader.

— Håkon Lie, Cascading HTML style sheets – a proposal

  • That’s the tension Håkon wanted to soften
  • Between authors and readers,
  • And he was clear that…

If conflicts arise the user should have the last word

— Håkon Lie, Cascading HTML style sheets – a proposal

  • When conflicts arise
  • The user should have the last word

If a trade-off needs to be made, always put user needs above all.

— W3C Web Platform Design Principles

‘Priority of Constituencies’
  • This has also become a W3C principle
  • Always put user needs above all

🗺 Cascade Origins

  1. 🖥 User Agent Defaults
  2. 👥 User Preferences (too weak)
  3. 🎨 Author Styles (too powerful)
  • So at this point, everything is backwards
  • We have too much power

Why we have…Importance

  • That’s why we have importance
Kylo Ren and Rey
standing in front of an entire star war
with spaceships and lasers
and AT-AT walkers
  • A way for the other origins
  • To take back control when it matters,
  • and bring balance to the cascade…
  • When anyone adds !important to a declaration…
  1. 🖥 User Agent Defaults
  2. 👥 User Preferences
  3. 🎨 Author Styles
  4. ❗🎨 Author Important
  5. ❗👥 User Important
  6. ❗🖥 User Agent Important
  • It creates a new origin
  • One of three important origins
  • All higher priority than the normal origins
  • But with their order reversed…
Spock, and Spock with a goatee
!important mirror universe
  • It’s a mirror universe
  • We all have evil twins with goatees
  1. ❗🎨 Author Important (goes first)
  2. ❗👥 User Important (override authors)
  3. ❗🖥 User Agent Important (most power)
Flipped ❗Important Origins
  • In the important origins
  • We go first (lowest priority)
  • Users override us
  • Browsers have final say
  1. 🖥 User Agent Defaults
  2. 👥 User Preferences
  3. 🎨 Author Styles
  4. ❗🎨 Author Important
  5. ❗👥 User Important
  6. ❗🖥 User Agent Important
  • Important origins do always win,
  • But the real purpose is to keep us contained,
  • stuck there in the middle
  • Enough power to make the web interesting
  • But not enough to put us in control

User Agent Styles

Collected by Jens Oliver Meiert
  • If you’re curious, you can find where
  • browsers do use importance
UA Important Styles [present, debug]
  • Things that we just think of as ‘not allowed’
  • Are sometimes restricted by using importance
  • Come back to this if you’re interested
In the font preferences dialogue, a default-checked option: Allow pages to choose their own fonts, instead of your selections above
Firefox: un-check for !important behavior
  • Importance works in Safari user styles
  • Firefox provides a checkbox
  • Chrome provides a ‘minimum font size’

The purpose of…Importance

  • Importance doesn’t change specificity
  • It changes the origin of a style
  • And it flips the priority of origins.
  • The purpose…

Authors 👎🏼 Specificity Wars

With our own colleagues and libraries

  • Is not for fighting amongst ourselves
  • To override styles that came before us

Browsers/Users 👍🏼 Protect Styles

From higher origins breaking important things

  • The purpose is for users and browsers
  • To protect from future origins
  • Styles that might get added by the document author
Darth Vader
choking a rebel soldier
as storm troopers watch
  • We’re the baddies here

2 Bonus Origins… For Intermediate Styles

in transitions and animations

  • Technically two additional origins
  • For ‘intermediate’ or interpolated styles
  • Generated by animations & transitions

hotPink » teal

  • Take two colors (HotPink to Teal)
  • If we want to move between them,
  • The browser has to go through other colors…

hotPink
rgb(93 138 220) ?!
teal

  • (Depending on the color space we use)
  • Maybe we go through this bluish color
  • Along the way, that has to _override_both hotPink and teal
css…
button {
  background: teal;
  transition: background 1s;
}
button:hover { background: hotPink !important; }
  • For transitions,
  • The start and end values might come from any origin,
  • With any importance
  • The transition isn’t applied until after the cascade
  • So we’ve all had our say
  • We agree on the end points,
  • And we agree on transitioning between them
  1. 🖥 User Agent Defaults
  2. 👥 User Preferences
  3. 🎨 Author Styles
  4. ❗🎨 Author Important
  5. ❗👥 User Important
  6. ❗🖥 User Agent Important
  7. ➡️ Transitions
  • So the intermediate values have to override everything
  • Transitions are the most powerful ‘cascade origin’
css…
:target { animation: bg-change 3s ease-in both; }
@keyframes bg-change {
  from { background: pink; }
  to { background: cyan; }
}
  • Animations are a little different
  • Only the animation property is in the cascade
  • Where we can make sure we agree
  • Keyframes live outside the cascade, disconnected
No !important keyframes [present, debug]
  • Importance isn’t allowed in keyframes
  • will cause the declaration to be ignored
  1. 🖥 User Agent Defaults
  2. 👥 User Preferences
  3. 🎨 Author Styles
  4. 🏇🏽 Animations
  5. ❗🎨 Author Important
  6. ❗👥 User Important
  7. ❗🖥 User Agent Important
  8. ➡️ Transitions
  • So we still need a way to override specific parts of an animation
  • Animation origin slots in between author normal and important styles
The Cascade (as a funnel) [present, debug]
  • Important styles always win
  • Importance also splits us into a different path
  • Changes how every following step works
  • For example…
  • Light DOM styles override shadow DOM styles
  • Unless we add !important, and then it’s reversed

🌗 (Shadow) Context

styling web components

  • This is called Context in the cascade
  • It’s like ‘origin’
  • But for shadow DOM

Shadow Styles Are Custom Defaults

similar to user agent origin

  • Web components are designed to act like custom HTML
  • So shadow DOM styles act kinda like browser defaults
Important Shadow Context [present, debug]

(demo if needed)

Again… Importance is Defensive

and reverses priority

  • Again, importance is defensive
  • Allowing component authors to protect essential styles
The Cascade (as a funnel) [present, debug]
  • Element attached (or inline) styles are next
  • The exception to our importance reversal
  • Same on both tracks…
  • Inline styles win when importance is equal
  • But Cascade Layers reverse
  • for the same reason origins do

Cascade Layers

Cascading & Inheritance Level 5
  • Layers were proposed at the end of 2019, and already…
Support data from CanIUse.com on the css-cascade-layers feature
There’s also a polyfill [Can I Use]
  • Well supported across browsers
  • For almost 3 years
css…
@layer settings {}
@layer tools {}
@layer generic {}
@layer elements {}
@layer objects {}
@layer components {}
@layer overrides {}
  • They give us a way to organize our code
  • And help us manage the cascade

Layers of Specificity

  • Layers of specificity
  • The declaration in the highest layer wins
  • Before we bother to compare specificity
  • Within the winning layer
  • We’ll look at that more when we talk about organizing conventions

Like Origins, ❗️important Layers Reverse

  • But like origins and context
  • Important layers are reversed
  1. Resets (weakest)
  2. Themes
  3. Components (strongest)
  • So if we have three layers
  • Each one overriding the previous

!important

  • And then we add important styles
  • Inside each layer
  1. Resets
  2. Themes
  3. Components
  4. ❗important Components
  5. ❗important Themes
  6. ❗important Resets
  • We now have six layer
  • With important layers reversed
Kylo Ren and Rey
standing in front of an entire star war
with spaceships and lasers
and AT-AT walkers
  • Again, the goal is balance

Protect Styles From Future Layers

Prioritize -> Layers
Protect -> Importance

  • If we just want to override
  • Or manage priorities
  • That’s what layers are for
  • Importance only when essential
css…
@layer reset {
  [hidden] { display: none !important; }
}
  • Reasonable thing to put in a reset
  • Reasonable for reset to have low priority
  • But we don’t want future layers to override this one declaration
The Cascade (as a funnel) [present, debug]
  • There’s more in here to talk about
  • And we’ll come back throughout the workshops
  • Get into some of the weeds

CSS encourages Responsive Web Design

  • The Cascade is how
  • CSS encourages and even enforces
  • Our styles to be responsive
Over the shoulder, a person wearing headphones, using a braille display attached to a laptop
  • Across different browsers and devices
  • Different interfaces
  • The needs or preferences of different users

That means Our Needs & Preferences

  • Our needs
  • And our preferences
  • All of us

Web for all. Web on everything.

— W3C Mission Statement

  • Because the web is for everyone
The second browser, a text-only terminal with numbered links for keyboard control
Line Mode Browser, developed by Nicola Pellow
  • And is designed to work everywhere

CSS encourages Web Design Systems

  • By describing systems of design intent
html…
<p style="color: mediumVioletRed;"></p>
<p style="color: mediumVioletRed;"></p>
<p style="color: mediumVioletRed;"></p>
<p style="color: mediumVioletRed;"></p>
<p style="color: mediumVioletRed;"></p>
  • Not just one-offs that we copy-and-paste
Published book with badly super-imposed hand-pointer cursor and scribbled word click!
  • Or static documents that we export once
A pixelated broken-image icon, showing a photo torn at one corner
  • And then fax back and forth
We're Not Alone, with browser logos, a guy in construction clothes carrying a laptop, and ET
  • But something we build together
  • All of us styling the web in collaboration
  • Authors, browsers, users, and (maybe even) aliens

The cascade Is Expressive

  • By taking into account
  • the purpose and priority of each style
  • For the browser to use

Graphic design of unknown content with unknown collaborators, on an infinite and unknowable canvas, across operating systems, interfaces, languages, and writing modes

— me

  • The goal here is unlike any other medium.
  • We’re doing [quote]
  • It’s absurd, but the cascade helps make it happen
Yoda reaching out to control the force, surrounded by jungle swamp
  • It’s a really powerful tool
  • That we can learn to wield
  • So long as we follow the most important rule…
avatar

A Jedi uses the [Cascade] for knowledge and defense, never for attack.

— Yoda (basically)

« resources
Bring this workshop to your company.
Slide Controls

View:

Navigate slides using the arrow-keys.