Lesson 1: Selectors and rules
CSS began at CERN in 1994
The saga of CSS starts in 1994, as its two creators put it. Håkon Wium Lie, then working at CERN, the cradle of the web, published a proposal called Cascading HTML style sheets on 10 October 1994. CSS level 1, by Lie and Bert Bos, became a W3C Recommendation on 17 December 1996.
Why it fits here: The style element and the stylesheet file of this topic are what that proposal set out to give the web.
Sources: Håkon Wium Lie and Bert Bos: The CSS saga (W3C); Håkon Wium Lie: Cascading HTML style sheets, a proposal (W3C); W3C: Cascading Style Sheets, level 1
Topic: What CSS is, and where it goes · history, people, internet
One page, many looks
In May 2003 Dave Shea launched the CSS Zen Garden, a demonstration of what can be accomplished through CSS-based design. Every design it shows dresses the very same HTML page. In the site's own words, the HTML remains the same, and the only thing that changes is the external CSS file.
Why it fits here: The external stylesheet of this topic is the Zen Garden's whole trick: keep the HTML, change the file, change the look.
Sources: CSS Zen Garden: The Beauty of CSS Design; Wikipedia: CSS Zen Garden
Topic: What CSS is, and where it goes · history, people, internet
Built to skip what it cannot read
CSS was built to survive its own future. The CSS 2.1 standard says browsers must ignore a declaration with an unknown property, so that new properties can be added later. Its own example is h1 { color: red; rotation: 70minutes }, which a browser treats as h1 { color: red }.
Why it fits here: This is why the browser of this topic skips a declaration it cannot read and keeps the rest of the rule.
Source: W3C: CSS 2.1, rules for handling parsing errors
Topic: A rule, taken apart · tools, internet
The 8 pixels around every page
A page with no CSS at all still has a small gap at its edges. The HTML standard lists the styles a browser is expected to use by itself, its user-agent style sheet. For the body, it expects a default margin of 8px, which is why plain text never quite touches the edge of the window.
Why it fits here: The browser's own stylesheet in this topic is where that 8px margin lives.
Source: HTML Standard (WHATWG): rendering
Topic: A rule, taken apart · tools, numbers
The same selectors in JavaScript
The selectors of CSS also work in JavaScript. The W3C's Selectors API, a Recommendation from 21 February 2013, defines methods such as querySelectorAll, which find elements with a selector. Its own example replaces a nested loop over a table's rows with one call, and says the script becomes much more concise.
Why it fits here: Every selector of this topic, from .offer to a child chosen with a greater-than sign, works the same way inside JavaScript.
Source: W3C: Selectors API Level 1
Topic: Selectors: choosing what to dress · tools, internet, history
Name a class for what it is
The HTML standard says a class attribute holds a set of space-separated tokens, so one element can belong to several classes at once. It puts no extra limits on the names, but it encourages names that describe the nature of the content, not the look you want. So .offer is a better name than .red: when the offers turn green, the name still fits.
Why it fits here: The class selectors of this topic choose by those names, so a name that says what a thing is keeps working when its look changes.
Source: HTML Standard (WHATWG): the class attribute
Topic: Selectors: choosing what to dress · words, tools
The reader can overrule the page
In CSS, the reader can win. Both the page's author and the reader may mark declarations !important, and the reader's important rules override the author's. The CSS standard says this improves accessibility, giving users with special requirements, such as large fonts or certain colour combinations, control. CSS1 had it the other way round, and CSS2 changed it.
Why it fits here: The !important of this topic sits at the top of the cascade, and a reader's own !important sits above the page's.
Sources: W3C: CSS 2.1, the cascade; W3C: CSS 2, the cascade
Topic: The cascade: which rule wins · history, people, tools
Specificity is counted in three numbers
The selectors standard counts a selector's specificity as three numbers: its ids, then its classes and the like, then its types. Two selectors are compared on the first number, and the next number counts only on a tie. So one id outweighs any number of classes, and one class outweighs any number of types.
Why it fits here: This is the counting behind this topic's rule that an id beats a class and a class beats a type.
Source: W3C: Selectors Level 4, calculating a selector's specificity
Topic: The cascade: which rule wins · numbers, tools
Every property says yes or no
Every CSS property is defined in a standard with a small table, and one line of it says Inherited: yes or no. The CSS Color standard says yes for color. The CSS Backgrounds standard says no for background-color, whose starting value is transparent, so a child's own background is see-through and shows its parent's.
Why it fits here: The two lists of this topic, what passes down and what does not, come straight from those tables.
Sources: W3C: CSS Color Module Level 4, the color property; W3C: CSS Backgrounds and Borders Module Level 3
Topic: Inheritance: what children take from parents · tools, words
Borders do not inherit, but they borrow
A border's colour is not inherited, yet a border given no colour still has one. In the CSS Backgrounds standard, the starting value of a border's colour is currentColor. The CSS Color standard defines currentcolor as the value of the color property on the same element, so the border takes the colour of its element's text.
Why it fits here: This topic's border stays on its own element, but it still matches that element's text colour unless you give it one.
Sources: W3C: CSS Backgrounds and Borders Module Level 3, border colours; W3C: CSS Color Module Level 4, the currentcolor keyword
Topic: Inheritance: what children take from parents · tools, words
Lesson 2: Colours and units
A colour named for a girl
The colour rebeccapurple, #663399, is named in memory of Rebecca Meyer, the daughter of Eric Meyer, a well-known author on CSS. She died in 2014, on her sixth birthday. When the web community proposed beccapurple, her father asked that the name be rebeccapurple: just before her birthday she had said that Becca was a baby name. The CSS Working Group approved it that June.
Why it fits here: The 148 names of this topic include this one, chosen to remember a person.
Sources: Eric Meyer: rebeccapurple (meyerweb.com); Wikipedia: Eric A. Meyer; W3C: CSS Color Module Level 4, named colors
Topic: Text colour and background colour · history, people, words
Names that do not add up
The CSS Color standard itself admits that its colour names are not consistent: darkgray is lighter than gray, and lightpink is darker than pink. The reason is history. Gray came from HTML, while dark gray came from X11, an older list of names. The standard also notes that indianred was originally named after a red pigment from India, and it does not encourage using the names at all.
Why it fits here: This is why this topic sends you to numbers for any colour that matters.
Sources: W3C: CSS Color Module Level 4, named colors; Wikipedia: X11 color names
Topic: Text colour and background colour · history, words, numbers
Nearly 1.7 crore colours
Each lamp's level is stored in eight bits, so a whole colour takes 24 bits. That gives 256 × 256 × 256 = 1,67,77,216 colours, nearly 1.7 crore. The CSS Color standard reads each hex pair as one lamp, where 00 is the minimum and ff, 255 in decimal, is the maximum.
Why it fits here: The rgb() and hex codes of this topic can name every one of those colours.
Sources: Wikipedia: Web colors; W3C: CSS Color Module Level 4, the hex notation
Topic: Colours as numbers: rgb and hex · numbers, science
Why two hex digits fit one lamp
Hex suits colours because of how computers store them. Each lamp's level takes eight bits, and two hexadecimal digits describe exactly eight bits, from 00 to ff. So six hex digits name a whole colour, one pair for red, then green, then blue, with nothing left over.
Why it fits here: The six digits of a hex code in this topic are those three pairs, in the order red, green, blue.
Sources: Wikipedia: Web colors; W3C: CSS Color Module Level 4, the hex notation
Topic: Colours as numbers: rgb and hex · numbers, tools
hsl came to CSS in 2011
CSS Color Level 3, a W3C Recommendation of 7 June 2011, added hsl() as a complement to rgb(). It gave its reason plainly: the advantage of HSL over RGB is that it is far more intuitive, so you can guess at the colours you want, and then tweak. It also makes matching sets of colours easier, by keeping the hue and varying the lightness.
Why it fits here: The shades of this topic, one hue with several lightnesses, are the matching sets that standard had in mind.
Source: W3C: CSS Color Module Level 3
Topic: Shades with hsl, and see-through colours · history, tools
Why see-through is called alpha
The idea of an alpha channel, a fourth number that says how see-through a colour is, came from Alvy Ray Smith and Ed Catmull in the late 1970s, at the New York Institute of Technology's Computer Graphics Lab. A 1984 paper by Thomas Porter and Tom Duff developed it further. Smith said they called it alpha because of the classic formula for blending two images, which uses the Greek letter alpha.
Why it fits here: The alpha after the slash in this topic is that fourth number.
Sources: Wikipedia: Alpha compositing; Alvy Ray Smith: Alpha and the History of Digital Compositing
Topic: Shades with hsl, and see-through colours · history, people, science
A CSS pixel is an angle
A CSS pixel is not one dot of your screen. The CSS standard defines a reference pixel as the visual angle of one pixel on a 96 dpi screen seen from an arm's length. For an arm of 28 inches, that is about 0.0213 degrees. It matches the rule that 1px is one ninety-sixth of an inch.
Why it fits here: The px of this topic is that agreed size, meant to look about the same on a phone held close and a laptop further away.
Source: W3C: CSS Values and Units Module Level 4, absolute lengths
Topic: Units: px, em, rem and % · science, numbers, tools
The em began as a square of metal
The em is older than computers. In metal type, the letter m was once cast on a square body, so a square space as wide as a line was tall came to be called an em-quad. CSS kept the name for a size measured by the font: 1em equals the computed font size of the element it is used on.
Why it fits here: The em of this topic still means what the printers meant: a size that follows the type.
Sources: Wikipedia: Em (typography); W3C: CSS Values and Units Module Level 4, font-relative lengths
Topic: Units: px, em, rem and % · history, words, books
Why 100vh was too tall on phones
On a phone, toolbars such as the address bar appear and disappear, so the viewport changes size, but vw and vh do not. As a result, an element 100vh tall was too tall when the page loaded, and part of it hid behind the bar. The dynamic units, such as dvh, fix this, and they shipped in Chrome 108, joining Safari and Firefox, which already had them.
Why it fits here: The dvh of this topic was made for exactly this problem.
Source: web.dev: The large, small, and dynamic viewport units
Topic: Sizing by the screen, and choosing a unit · tools, internet
Three heights, not one
A phone screen has three heights in CSS. Units with the sv prefix, such as svh, measure the small viewport, and units with the lv prefix, such as lvh, measure the large one; both stay fixed unless the viewport itself is resized. The dv units sit between them: 100dvh becomes either the large or the small size as the toolbars come and go.
Why it fits here: The dvh of this topic is the one of the three that moves, which is why it suits a full-screen section.
Source: web.dev: The large, small, and dynamic viewport units
Topic: Sizing by the screen, and choosing a unit · tools, internet
Lesson 3: The box model
As old as CSS itself
The four layers are as old as CSS. The first CSS standard, a W3C Recommendation of 17 December 1996, says that each element results in one or more rectangular boxes. It goes on: all boxes have a core content area, with optional padding, border and margin areas around it. The CSS 2.1 standard describes every box with the same four areas.
Why it fits here: This topic's four layers, content, padding, border and margin, are the very areas that first standard named.
Sources: W3C: CSS level 1 (1996); W3C: CSS 2.1, Box model
Topic: The four layers of every box · history, standards
Half of the width, not the height
A padding-top of 50% is half the parent's width, not half its height. The CSS 2.1 standard says padding percentages come from the width of the containing block, even for padding-top and padding-bottom. Developers once used this on purpose. A box with no height and a padding-top of 56.25% keeps the 16:9 shape of a video at any width, because 9 ÷ 16 = 0.5625.
Why it fits here: The lesson's part on padding in percent shows this rule; the old trick shows how it was put to use.
Sources: W3C: CSS 2.1, Box model; web.dev: The aspect-ratio property
Topic: The four layers of every box · numbers, standards
The Internet Explorer box model
When the first CSS standards came out, the two major browsers, Netscape 4.0 and Internet Explorer 4.0, both measured width from border to border. The W3C rule measured only the content, so the same box came out narrower in one than in the other. The browsers' way became known as the Internet Explorer box model, often called a bug. Today's standard offers both ways, and border-box puts the padding and border inside the width.
Why it fits here: This topic's box-sizing: border-box is that old browser rule, made standard and chosen on purpose.
Sources: Wikipedia: CSS box model; W3C: CSS Box Sizing Level 3
Topic: How wide is a box, really? · history, browsers
One famous line for every box
On 1 February 2012, the developer Paul Irish wrote a short post titled "* { box-sizing: border-box } FTW". He called the relationship of width and padding one of his least favourite parts of CSS layout, and he wrote that applying border-box to all elements is safe and wise. In August 2014 he updated the code: border-box on the html element, inherited by every other element.
Why it fits here: It is this topic's box-sizing: border-box, set once for a whole page instead of box by box.
Source: Paul Irish: * { box-sizing: border-box } FTW
Topic: How wide is a box, really? · people, history
Collapsing, by design, since 1996
Margin collapsing is no accident of browsers. The first CSS standard, of 1996, says that two or more adjoining vertical margins, with no border, padding or content between them, collapse to the largest of them. It even gives the reason: in most cases the result is visually more pleasing and closer to what the designer expects. The same standard says that horizontal margins are not collapsed, and CSS 2.1 repeats it.
Why it fits here: This is the rule this topic calls the most surprising one in the box model, with the standard's own reason for it.
Sources: W3C: CSS level 1 (1996); W3C: CSS 2.1, Box model
Topic: Margins: the space between boxes · history, standards
No collapsing in flex and grid
Margin collapsing happens between ordinary blocks, but not everywhere. The Flexbox standard says it plainly: the margins of adjacent flex items do not collapse. The Grid standard says the same of grid items. So two cards with 16px margins, stacked in a flex column, sit 32px apart, while the same two cards stacked as ordinary blocks sit only 16px apart.
Why it fits here: It explains why a gap can suddenly double when ordinary blocks are put into a flex or grid layout.
Sources: W3C: CSS Flexible Box Layout; W3C: CSS Grid Layout
Topic: Margins: the space between boxes · standards, layout
inline-block came later
The first CSS standard, of 1996, knew only four kinds of display: block, inline, list-item and none. The value inline-block, which this topic uses for a box that sits in a line but obeys width and padding, came later. The list of changes in CSS 2.1 records it as a new display value.
Why it fits here: Block and inline, two of this topic's three kinds, date from 1996; the third, inline-block, arrived with CSS 2.1.
Sources: W3C: CSS level 1 (1996); W3C: CSS 2.1, Changes
Topic: Block, inline and inline-block · history, standards
Two words inside every display
The newest CSS standard on display splits each display type into two qualities. The outer display type says how a box takes part in the flow among its neighbours. The inner display type says how it lays out its own children. Seen that way, inline-block is short for inline flow-root: inline on the outside, an ordinary block on the inside.
Why it fits here: It names both halves of this topic's inline-block: in the line like a word, sized like a block.
Source: W3C: CSS Display Level 3
Topic: Block, inline and inline-block · standards, words
A property with an old name
The property that lets one very long word break began as a Microsoft extension called word-wrap, and most browsers copied it under the same name. It has since been renamed overflow-wrap. The CSS Text standard tells browsers to keep treating word-wrap as a legacy name for it, so old style sheets still work.
Why it fits here: This topic's part on one very long word uses overflow-wrap, and many older pages still use its first name.
Sources: MDN: overflow-wrap; W3C: CSS Text Level 3
Topic: When the content is too big for its box · history, words, browsers
Hidden, but still scrollable
A box with overflow: hidden hides the extra content from the reader, but the content is still there. The CSS Overflow standard says it must still be scrollable by a program, so the box is still a scroll container. Another value, overflow: clip, forbids scrolling through any mechanism at all.
Why it fits here: This topic's table says hidden cuts the extra off; this shows what hidden really does, and the stricter clip.
Source: W3C: CSS Overflow Level 3
Topic: When the content is too big for its box · standards
Lesson 4: Typography
Web fonts: in, out and back again
Web fonts had a false start. The CSS2 standard, a W3C Recommendation of 12 May 1998, already had font descriptions and the @font-face rule. CSS 2.1 then removed the font descriptors and the @font-face declaration altogether. The rule came back in the CSS Fonts Level 3 standard, which lets a page link to fonts that are fetched and activated when needed.
Why it fits here: The @font-face rule that this topic uses to send a font with the page is the one that went away and came back.
Sources: W3C: CSS level 2 (1998); W3C: CSS level 2, Fonts (1998); W3C: CSS 2.1, Changes; W3C: CSS Fonts Level 3
Topic: Choosing the letters: font-family · history, standards
Arial was made to fit Helvetica's widths
Arial was designed in 1982 by Robin Nicholas of Monotype, for low-resolution laser printers, and later developed with Patricia Saunders into a full typeface. It was made to match the character widths of Helvetica, so a line set in one takes the same space in the other. In 1992 Microsoft made Arial one of the four core fonts of Windows 3.1, announced as an alternative to Helvetica.
Why it fits here: This topic's example list names Arial as its second wish; this is where that font came from.
Sources: Microsoft Typography: Arial; Wikipedia: Arial
Topic: Choosing the letters: font-family · history, people
Italic is named after Italy
Italic type is named after the country where it began. It was first used by an Italian printer, Aldo Manuzio, also known as Aldus Manutius, around 1500, and one of his early italics appears in a 1501 edition of Virgil. Italic letters are based on a stylised form of handwriting.
Why it fits here: This topic's font-style: italic chooses these letters, whose design began in that printer's books.
Sources: Wiktionary: italic; Wikipedia: Italic type
Topic: Size, weight and slant · history, words, people
Any weight from 1 to 1000
Font weights are numbers. The CSS Fonts standard says the keyword normal is the same as 400 and bold is the same as 700. Level 4 of that standard accepts any number from 1 to 1000, and each number is at least as dark as the one before it. So a font with a whole range of weights can show one such as 450.
Why it fits here: This topic's 400 for normal and 700 for bold come from this very rule.
Source: W3C: CSS Fonts Level 4
Topic: Size, weight and slant · numbers, standards
Leading is said like the metal
Printers say leading as "ledding", like the metal, not "leeding". In hand typesetting, leading was the thin strips of lead, or aluminium, put between lines of type to move them apart, and the practice became popular in the eighteenth century. In CSS, the space between lines is set with line-height.
Why it fits here: This topic's line-height is the modern name for those strips, and the old word still names line spacing.
Sources: Wikipedia: Leading; American Heritage Dictionary: lead
Topic: Space between lines and letters · history, words
The accessibility rule for blocks of text
WCAG, the web's accessibility standard, has a success criterion for blocks of text, 1.4.8 Visual Presentation. It asks that a reader can get lines no wider than 80 characters, text that is not justified, and line spacing of at least space-and-a-half within paragraphs. This topic's line-height of about 1.5 and lines near 65ch fit inside those limits.
Why it fits here: It is the standard behind this topic's numbers for line height and line length.
Source: W3C: WCAG 2.2
Topic: Space between lines and letters · accessibility, standards, numbers
ch is the width of a zero
The unit ch, which this topic uses for line length, is measured from one character. The CSS Values standard takes the width of the digit zero, 0, in the font in use, to stand for the typical width of European letters and digits. So max-width: 65ch is as wide as 65 zeros in that font.
Why it fits here: It explains what this topic's 65ch measures, and why the width changes when the font changes.
Source: W3C: CSS Values and Units Level 4
Topic: Space between lines and letters · numbers, standards
Two letters i in Turkish
Capital letters are not the same in every language. Turkish has a dotted i and a dotless ı, each with its own capital: İ and I. So the CSS Text standard says that, when the content language is Turkish, the usual mapping between I and i is replaced by the dotted and dotless pairs. This is one reason text-transform pays attention to the page's language.
Why it fits here: This topic's text-transform: uppercase follows these rules, which change with the language of the page.
Source: W3C: CSS Text Level 3
Topic: Aligning, decorating and transforming text · words, languages, standards
Text that blinks
The first CSS standard, in 1996, offered text-decoration: blink, which makes text flash on and off. The value is still in today's CSS Text Decoration standard, but it is deprecated in favour of animations. That standard lets browsers leave the text still, and it notes that not blinking meets a checkpoint of the accessibility guidelines for browsers.
Why it fits here: It is the one value of this topic's text-decoration that browsers are allowed to ignore.
Sources: W3C: CSS level 1 (1996); W3C: CSS Text Decoration Level 3
Topic: Aligning, decorating and transforming text · history, standards, accessibility
When visited links leaked your history
For years, visited links leaked browsing history. A page could ask the browser how its links were styled and learn which sites a reader had been to; Mozilla wrote that some tests could check 210,000 addresses a minute. On 31 March 2010 Mozilla announced the fix: scripts are always told that links are unvisited, and visited links may change only a few properties. Browsers still limit these styles, for privacy.
Why it fits here: This topic's :visited is the state with the fewest styles allowed, and this is the reason.
Sources: Mozilla Hacks: privacy-related changes coming to CSS :visited; MDN: :visited
Topic: Styling links, in all their states · history, privacy, browsers
Where the blue and the purple come from
The blue of a fresh link and the purple of a visited one are written down in the HTML standard. Its suggested style sheet for browsers gives :link the colour #0000EE and :visited the colour #551A8B, and a link being pressed turns #FF0000, red. Rules of your own for these states replace them.
Why it fits here: These are the defaults that this topic's link states start from, before any style of yours.
Source: WHATWG: HTML Standard, Rendering
Topic: Styling links, in all their states · standards, colour
Lesson 5: Flexbox
Flexbox changed its spelling twice
Flexbox changed its spelling twice before it settled. A W3C draft of 23 July 2009 switched it on with display: box. A draft of 29 November 2011 used display: flexbox instead. Then the Candidate Recommendation of 18 September 2012 used display: flex, the form this lesson uses.
Why it fits here: If you meet display: box or display: flexbox on an old page, it is an early spelling of this topic's display: flex.
Sources: W3C: Flexible Box Layout, Working Draft 2009; W3C: Flexible Box Layout, Working Draft 2011; W3C: Flexible Box Layout, Candidate Recommendation 2012
Topic: A flex container and its items · history, standards
A row starts where the writing starts
A flex row does not always run from left to right. The Flexbox standard says a row's main axis has the same orientation as the inline axis of the writing mode, so it runs the way lines of text run. On a page in a right-to-left script, such as Hebrew or Arabic, the first flex item sits at the right.
Why it fits here: This topic's main axis is a row by default, and this shows that a row follows the language of the page.
Sources: W3C: CSS Flexible Box Layout; W3C: CSS Writing Modes Level 4
Topic: A flex container and its items · languages, standards
The sixth value came from elsewhere
The Flexbox standard lists only five values for justify-content: flex-start, flex-end, center, space-between and space-around. The sixth value in this topic, space-evenly, is defined in a separate standard, CSS Box Alignment, which covers alignment for block, table, flex and grid layout. It spreads the items evenly, with a full-size space at either end.
Why it fits here: It is why space-evenly is missing from the Flexbox standard's own list of this topic's values.
Sources: W3C: CSS Flexible Box Layout; W3C: CSS Box Alignment Level 3
Topic: Sharing the space along the row: justify-content and gap · standards, history
gap started life in grid
The gap property began as grid-gap, with grid-row-gap and grid-column-gap, for grids only. The Grid standard's own list of changes records that those three were replaced with row-gap, column-gap and gap, now defined in CSS Box Alignment, the standard shared by block, table, flex and grid layout. Today the same gap property works in a flex row too.
Why it fits here: This topic's gap, between neighbouring flex items, is that renamed grid property.
Sources: W3C: CSS Grid Layout; W3C: CSS Box Alignment Level 3
Topic: Sharing the space along the row: justify-content and gap · history, standards
Equal heights, from the start
Equal-height cards are built into flexbox. The 2012 Candidate Recommendation's own example of a shop page sets display: flex with the comment "Flex layout so items have equal height". It works because align-items starts as stretch, so an item with no height of its own fills the height of the row.
Why it fits here: This topic's cards come out level for exactly this reason: stretch is the default.
Sources: W3C: Flexible Box Layout, Candidate Recommendation 2012; W3C: CSS Flexible Box Layout
Topic: Lining up across the row: align-items · history, standards
Safe centring
Centring can hide content. The Box Alignment standard warns that aligning exactly as asked can push an item out of its container and lose data: a centred sidebar's tall items might spill past the screen's start edge, where no one can scroll. So it adds the keyword safe. With align-items: safe center, an item that overflows is aligned to the start instead.
Why it fits here: This topic centres with align-items: center; safe is the same idea with a guard for items too big to fit.
Source: W3C: CSS Box Alignment Level 3
Topic: Lining up across the row: align-items · standards, real life
Shrink yes, grow no
The squeezing in this topic is the default on purpose. The Flexbox standard starts every item at flex: 0 1 auto, which means grow 0, shrink 1 and basis auto. So an item never grows into spare space unless asked, but it is allowed to shrink to its minimum size when there is not enough space.
Why it fits here: It explains why this topic's row of five items shrinks them instead of wrapping.
Source: W3C: CSS Flexible Box Layout
Topic: When the row is too long: flex-wrap · standards, numbers
Why a flex item stops shrinking
A flex item stops shrinking at the size of its content. The Flexbox standard gives min-width a new starting value, auto, and for a flex item that means a content-based minimum size. So one long word or a wide picture keeps its item from shrinking further, and the row overflows. Setting min-width: 0 on the item removes that floor.
Why it fits here: It is the limit on this topic's squeezing: items shrink, but not below their content.
Source: W3C: CSS Flexible Box Layout
Topic: When the row is too long: flex-wrap · standards
flex: 1 means grow from zero
flex: 1 is short for three values: grow 1, shrink 1 and a basis of zero. The Flexbox standard says this pattern sets the flex basis to zero, so each item gets its share of the free space. Because every item then starts from nothing, the whole row is shared out by the grow numbers, and equal numbers give equal columns.
Why it fits here: This topic's equal columns come from this rule: each item starts at zero and takes one equal share.
Source: W3C: CSS Flexible Box Layout
Topic: Sizing the items: grow, shrink and basis · numbers, standards
Seen in one order, read in another
Moving items with order changes only what the eye sees. The Flexbox standard says it leaves the source order intact for speech and for moving through the page with the keyboard. So it warns that authors must not use order, or the reverse directions, as a substitute for correct source order, because that can ruin the accessibility of the document.
Why it fits here: This topic's order moves items on the screen only; this is the standard's own warning about it.
Source: W3C: CSS Flexible Box Layout
Topic: Sizing the items: grow, shrink and basis · accessibility, standards
Lesson 6: Grid
Grid began with three editors at Microsoft
CSS Grid began with engineers at Microsoft. The first W3C Working Draft of the standard, titled Grid Layout and dated 7 April 2011, names three editors from Microsoft Corporation, Alex Mogilevsky, Phil Cupp and Markus Mielke, together with Daniel Glazman of Disruptive Innovations.
Why it fits here: This topic's grid of columns and rows grew out of that 2011 draft.
Source: W3C: Grid Layout, Working Draft 2011
Topic: A grid of columns and rows · history, people, standards
A grid ignores floats
A grid ignores the old float trick. The Grid standard says float and clear have no effect on a grid item, and neither does vertical-align. The reason is that a grid container is not a block container, so some properties that were designed for block layout do not apply inside a grid.
Why it fits here: It is one way this topic's grid items behave differently from ordinary blocks.
Source: W3C: CSS Grid Layout
Topic: A grid of columns and rows · standards
fr shares only what is left
The Grid standard defines fr as a fraction of the leftover space in the grid container: the room left after the fixed tracks and the gaps. Tracks sized in fr are called flexible tracks, because they flex as that leftover space grows and shrinks, while fixed tracks keep their size.
Why it fits here: This topic's 1fr 2fr 1fr shares exactly that leftover space, not the whole width.
Source: W3C: CSS Grid Layout
Topic: Sharing the width: the fr unit and repeat() · numbers, standards
An fr column keeps room for its content
An fr column will not shrink below its content. The Grid standard says that fr written on its own implies an automatic minimum, as if it were minmax(auto, 1fr). So one very long word can make a 1fr column wider than its share. Writing minmax(0, 1fr) instead gives strict, equal shares.
Why it fits here: It is the fine print of this topic's fr: the shares are fair until the content is too wide to fit.
Source: W3C: CSS Grid Layout
Topic: Sharing the width: the fr unit and repeat() · numbers, standards
-1 is the end of the grid you drew
Line -1 is the last line of the grid you drew, not of every row the browser adds. The Grid standard says line numbers count from the edges of the explicit grid: positive ones from the start, beginning at 1, and negative ones from the end, beginning at -1. Rows the grid makes on its own, past your template, are not counted.
Why it fits here: This topic's 1 / -1 runs from edge to edge of the grid you drew; rows made later lie outside it.
Source: W3C: CSS Grid Layout
Topic: Placing items by the grid's lines · numbers, standards
Named areas name their lines too
Naming an area also names its lines. For each named area, say main, the Grid standard creates four line names on its own: main-start for its first row line and first column line, and main-end for its last ones. So grid-column: main-start / main-end places an item exactly across the main area.
Why it fits here: It joins this topic's lines to the next topic's named areas.
Source: W3C: CSS Grid Layout
Topic: Placing items by the grid's lines · standards, words
Only rectangles allowed
A named area must be a rectangle. The Grid standard says that if an area's cells do not form a single filled-in rectangle, the whole grid-template-areas declaration is invalid, so the layout is not used at all. An L-shaped area is not allowed, though the standard notes that other shapes may be permitted in a future version.
Why it fits here: This topic's rule that every area is a rectangle is how the standard itself puts it.
Source: W3C: CSS Grid Layout
Topic: A page laid out by name: grid-template-areas · standards
A dot is an empty cell
In the picture of names, a full stop means an empty cell. The Grid standard says a run of one or more full stops, such as . or ..., is a null cell token: a cell that belongs to no area. Several dots in a row still make just one cell, so you can use as many as you need to line the picture up.
Why it fits here: It lets this topic's picture of names leave a cell empty.
Source: W3C: CSS Grid Layout
Topic: A page laid out by name: grid-template-areas · standards, words
Filling holes can shuffle the order
Filling holes has a cost. The Grid standard says dense packing fills holes earlier in the grid with smaller items that come up later, and that this may make items appear out of order. Without dense, placement only ever moves forward through the grid and never goes back to fill a hole.
Why it fits here: This topic's dense fills holes; this is its price, items seen out of their order.
Source: W3C: CSS Grid Layout
Topic: Grids that arrange themselves · standards, accessibility
Subgrid had to wait for Level 2
One grid feature had to wait. A subgrid is a grid inside a grid that takes its rows or columns from its parent, so the contents of separate cards can line up with each other. The first Grid standard deferred subgrid to Level 2, due to lack of implementation and a wish for further discussion; Level 2 now defines it.
Why it fits here: It is the next step after this topic's grids: a grid inside a grid that shares its parent's lines.
Sources: W3C: CSS Grid Layout; W3C: CSS Grid Layout Level 2
Topic: Grids that arrange themselves · history, standards
Lesson 7: Positioning
Positioning was written by rivals together
Positioning was written jointly by rivals. A W3C Working Draft of 31 January 1997, Positioning HTML Elements with Cascading Style Sheets, names two authors. They are Scott Furman of Netscape Communications and Scott Isaacs of Microsoft, the makers of the two major browsers of the time, with an editor from Hewlett Packard. It introduced the new position property.
Why it fits here: This topic's position property, with relative and absolute, first appeared in that joint draft.
Sources: W3C: Positioning HTML Elements with Cascading Style Sheets (1997); Wikipedia: CSS box model
Topic: The normal flow, and nudging with relative · history, people, standards
A nudge can overlap its neighbour
A relatively positioned box can land on top of its neighbour. The CSS 2.1 standard says that shifting a box this way has no effect on the box that follows: the next box is placed as if the first had not moved. So the two can overlap, and the space where the first box was stays empty.
Why it fits here: It is the other side of this topic's rule that a relative box keeps its place.
Source: W3C: CSS 2.1, Visual formatting model
Topic: The normal flow, and nudging with relative · standards
No positioned parent? A box the size of the screen
With no positioned ancestor, an absolute box is placed against the initial containing block. CSS 2.1 says that block has the size of the viewport and sits at the top of the page. So bottom: 0 puts the box at the bottom of the first screenful, not at the bottom of a long page.
Why it fits here: This topic says such a box is placed against the page; this is what that box really is.
Source: W3C: CSS 2.1, Visual formatting model details
Topic: Pinning a box: position: absolute · standards
inset is read like margin
The standard now calls top, right, bottom and left the inset properties. Their shorthand, inset, gives each side its value exactly as margin does: one value for all four sides, or up to four going round clockwise from the top. So inset: 0 pins all four edges at once.
Why it fits here: This topic's inset: 0 is this shorthand, read the way margin is read.
Source: W3C: CSS Positioned Layout Level 3
Topic: Pinning a box: position: absolute · standards, words
A transform can unfix a fixed box
A fixed box can stop being fixed. The CSS Transforms standard says that any transform on an element makes it the containing block for all its descendants, fixed ones included. So a fixed button inside a parent that has a transform is pinned to that parent instead, and it scrolls away with it.
Why it fits here: When this topic's fixed button scrolls away, a transform on a parent is the first thing to check.
Source: W3C: CSS Transforms Level 1
Topic: Staying on screen: position: fixed · standards
Fixed boxes print on every page
Print a page, and a fixed box does something new. CSS 2.1 says that, for paged media, boxes with fixed positions are repeated on every page. The standard even suggests a use for it: placing a signature at the bottom of each page.
Why it fits here: It is what this topic's fixed bar does on paper instead of on a screen.
Source: W3C: CSS 2.1, Visual formatting model
Topic: Staying on screen: position: fixed · standards, real life
Sticky only ever pushes one way
A sticky box with top: 0 only ever moves down. The Positioned Layout standard says an element with a top value and no bottom value will only ever be pushed down by sticky positioning, never upwards. It also notes that several sticky boxes in one container move independently, so they may overlap.
Why it fits here: This topic's top: 0 is the case the standard describes: the box is only ever pushed down.
Source: W3C: CSS Positioned Layout Level 3
Topic: Sticking at the edge: position: sticky · standards
A sticky box never leaves its parent
A sticky box never leaves its parent. The Positioned Layout standard moves a sticky box towards the edge of the view only insofar as it can while the box stays inside its containing block. That is why a sticky header stops sticking, and scrolls away, when the end of its own section arrives.
Why it fits here: It is the reason this topic's sticky box holds only while its parent is in view.
Source: W3C: CSS Positioned Layout Level 3
Topic: Sticking at the edge: position: sticky · standards
A see-through box starts its own pile
Making a box slightly see-through changes its layers. The CSS Color standard says an element with opacity less than 1 is drawn from a single image, so nothing outside it can slip in between the pieces inside it. That is why such an element creates a new stacking context: its children's z-index values now count only among themselves.
Why it fits here: It is another way to create this topic's ceiling, with no z-index written at all.
Source: W3C: CSS Color Level 3
Topic: Which box lies on top: z-index · standards
Flex and grid items need no position for z-index
z-index usually needs a positioned box, but flex and grid items are an exception. The Flexbox and Grid standards both say that on their items, a z-index other than auto creates a stacking context even if position is static, behaving as if position were relative.
Why it fits here: It is the one place where this topic's z-index works without any position.
Sources: W3C: CSS Flexible Box Layout; W3C: CSS Grid Layout
Topic: Which box lies on top: z-index · standards
Lesson 8: Responsive design
The article that named responsive design
The name responsive web design comes from one article. Ethan Marcotte published Responsive Web Design in the magazine A List Apart on 25 May 2010. In it he wrote that we need to practise responsive web design, and he named its three technical ingredients: fluid grids, flexible images and media queries.
Why it fits here: This whole lesson follows his three ingredients: fluid sizes, pictures that fit, and media queries.
Source: A List Apart: Responsive Web Design, by Ethan Marcotte
Topic: One page for every screen: the viewport meta tag · history, people, words
Why phones pretended to be 980 pixels wide
Apple's guide to Safari on iOS explains the 980 pixels in this topic. It says Safari sets the viewport to reasonable defaults that work well for most web pages, and that the default width is 980 pixels. The same guide says a page uses the viewport meta tag to set the viewport's width and initial scale instead.
Why it fits here: It is the default that this topic's viewport meta tag switches off.
Source: Apple: Safari Web Content Guide, Configuring the Viewport
Topic: One page for every screen: the viewport meta tag · history, browsers
A media type for phones, now deprecated
CSS2, in 1998, already had a media type for phones: handheld, meant for small screens, often monochrome, with limited bandwidth. Media Queries Level 4 now lists handheld among the deprecated media types that authors must not use. It even expects every media type to be deprecated in time, as media features such as width capture the differences better.
Why it fits here: It is why this topic asks about width, with min-width and max-width, instead of naming a kind of device.
Sources: W3C: CSS level 2 (1998); W3C: CSS level 2, Media types (1998); W3C: Media Queries Level 4
Topic: Rules for some screens: media queries · history, standards
Dark mode is a media query
Dark mode is a media query too. Media Queries Level 5 defines prefers-color-scheme, with the values light and dark, which reflects the user's wish for a light or a dark colour theme. So @media (prefers-color-scheme: dark) holds the rules for readers who have chosen dark mode on their device.
Why it fits here: It is this topic's @media, asking about the reader's choice instead of the screen's width.
Source: W3C: Media Queries Level 5
Topic: Rules for some screens: media queries · standards, real life
Mobile first began as a blog post
A short blog post of 2009 argued for mobile first. On 3 November 2009 the designer Luke Wroblewski wrote that, more often than not, the mobile version of a site was designed and built after the PC version was complete. He then gave three reasons to design for mobile first instead, the first being that mobile was exploding.
Why it fits here: This topic's order, small screens first and then wider ones, is the order he argued for.
Source: LukeW: Mobile First, by Luke Wroblewski
Topic: Small screens first: mobile-first design · history, people
Phones overtook computers in 2016
Designing for phones first matches how people browse. The analytics company StatCounter reported on 1 November 2016 that, worldwide, internet use on mobiles and tablets had exceeded desktop use for the first time that October: 51.3% against 48.7%. In other words, a little more than half of the world's web use that month came from phones and tablets.
Why it fits here: It is one reason this topic's base rules are written for the smallest screen.
Source: StatCounter: Mobile and tablet internet usage exceeds desktop for first time worldwide
Topic: Small screens first: mobile-first design · numbers, real life
clamp() settles even an impossible request
clamp() settles even an impossible request. The CSS Values standard says it takes a minimum, a central value and a maximum, and gives the central value clamped between the other two. If the minimum is bigger than the maximum, the minimum wins. So clamp(1rem, 4vw, 2rem) never goes below 1rem or above 2rem.
Why it fits here: It is the exact rule behind this topic's clamp(smallest, wanted, largest).
Source: W3C: CSS Values and Units Level 4
Topic: Sizes that stretch: fluid widths and clamp() · numbers, standards
100vw can be too wide
A width of 100vw can be too wide. The CSS Values standard sizes viewport units as if scrollbars did not exist. So on a computer that shows a vertical scrollbar, a box 100vw wide is wider than the space beside the scrollbar, and the page gains a sideways scroll. A width of 100% avoids it.
Why it fits here: It is a trap in this topic's vw: a share of the whole window, the scrollbar's strip included.
Source: W3C: CSS Values and Units Level 4
Topic: Sizes that stretch: fluid widths and clamp() · standards
Width and height stop the page jumping
Write width and height on an image, and the page stops jumping as it loads. The HTML standard maps those two attributes to the CSS aspect-ratio property, so the browser can reserve a box of the right shape before the picture arrives. The web.dev advice on layout shift is to always include width and height on images and videos.
Why it fits here: This topic's height: auto keeps that reserved shape while the picture shrinks to fit.
Sources: WHATWG: HTML Standard, Rendering; web.dev: Optimize Cumulative Layout Shift
Topic: Pictures that fit: responsive images · standards, real life
The browser picks the file
With srcset, the browser chooses which file to download, not the page. The HTML standard says it chooses depending on the screen's pixel density, the zoom level, and possibly whether a sharper image is already available. So two visitors to the same page can receive two different files.
Why it fits here: This topic's srcset offers the choices; the browser makes the choice.
Source: WHATWG: HTML Standard, Images
Topic: Pictures that fit: responsive images · standards, browsers
Lesson 9: Transitions and animations
Transitions began at Apple
CSS transitions began at Apple. On 31 October 2007 Dave Hyatt announced on the WebKit blog that WebKit now supported animation specified in CSS, starting with the simplest kind, called a transition. The CSS Transitions standard lists him and two other Apple engineers, Dean Jackson and Chris Marrin, among its former editors.
Why it fits here: This topic's transition property is the one that 2007 post introduced.
Sources: WebKit blog: CSS Animation, by Dave Hyatt; W3C: CSS Transitions
Topic: Changes that take time: transition · history, people
The way back is quicker
Point at a card, leave before its transition ends, and the way back is quicker. The CSS Transitions standard explains that if the return trip took its full time to cover the shorter distance, the effect would be distractingly lopsided. So it makes the second transition shorter, in step with how far the first one had got.
Why it fits here: It is a detail of this topic's both-ways transition: the way back shrinks when the way in was cut off.
Source: W3C: CSS Transitions
Topic: Changes that take time: transition · standards
ease is one exact curve
The default, ease, is one exact curve: the CSS Easing standard defines it as cubic-bezier(0.25, 0.1, 0.25, 1). In such a curve the two x values must lie between 0 and 1, but the y values may go beyond, so a change can overshoot its end and settle back, like a spring.
Why it fits here: It shows what this topic's ease really is, and how cubic-bezier() can make curves of your own.
Source: W3C: CSS Easing Functions Level 1
Topic: How a change moves: timing functions and delay · numbers, standards
Curves that came from car bodies
The curves behind cubic-bezier() came from cars. They are named after the French engineer Pierre Bézier, who used them in the 1960s to design the curves of Renault car bodies. Earlier, in 1959, the mathematician Paul de Casteljau had worked out a method for the same curves, and he was the first to use them in computer-aided design, at Citroën.
Why it fits here: Every curve this topic draws with cubic-bezier() is one of these car-body curves.
Sources: Wikipedia: Bézier curve; W3C: CSS Easing Functions Level 1
Topic: How a change moves: timing functions and delay · history, people, maths
The separate transform properties keep a fixed order
CSS also has separate translate, rotate and scale properties, and their order is fixed. The CSS Transforms standard builds the drawing by translating with translate, then rotating with rotate, then scaling with scale, and only then applying the transform property's steps, from left to right. So writing scale before translate in your rule changes nothing.
Why it fits here: In this topic's transform value the order you write matters; in these separate properties it does not.
Source: W3C: CSS Transforms Level 2
Topic: Moving, growing and turning: transform · standards
A full circle is 1turn
Angles in CSS are not only degrees. The CSS Values standard has a unit called turn, with exactly 1 turn in a full circle, so rotate(0.25turn) is a right angle, the same as rotate(90deg). It also accepts gradians, with 100grad in a right angle, and radians.
Why it fits here: This topic's rotate() accepts any of these units; turn is the easiest one to picture.
Source: W3C: CSS Values and Units Level 4
Topic: Moving, growing and turning: transform · numbers, maths, standards
Missing ends are filled in
A @keyframes rule does not need both ends. The CSS Animations standard says that if the 0% or from keyframe is missing, the browser builds one from the element's own current values, and the same for a missing 100% or to keyframe. So a single to keyframe animates from wherever the element already is.
Why it fits here: It lets this topic's keyframes list only the stops that differ from the element's normal look.
Source: W3C: CSS Animations Level 1
Topic: Moving by itself: @keyframes and animation · standards
Same name, last one wins
Two @keyframes rules with the same name do not combine. The CSS Animations standard says that the last one in document order wins, and all the earlier ones are ignored. So a stop written only in the first rule is lost, even though the second rule never mentions it.
Why it fits here: It is worth knowing when this topic's keyframes seem to ignore some of their stops.
Source: W3C: CSS Animations Level 1
Topic: Moving by itself: @keyframes and animation · standards
Why the rule against flashing exists
The rule against flashing has a famous reason. On 16 December 1997, a single broadcast of a Pokémon episode in Japan carried flashing scenes that set off photosensitive seizures in children across the country, and over 600 people, mostly children, were taken to hospitals. WCAG, the accessibility standard, asks that nothing on a page flashes more than three times in any one second.
Why it fits here: It is why this topic's careful page never flashes more than three times a second.
Sources: Wikipedia: Dennō Senshi Porygon; W3C: WCAG 2.2
Topic: Motion with care · history, accessibility, health
Less motion helps attention too
The reduced motion setting is not only about dizziness. Media Queries Level 5 says a reader who chooses it prefers an interface that removes or replaces the kinds of motion that either bring discomfort to those with vestibular motion sensitivity, or distraction to those with attention deficits.
Why it fits here: This topic's prefers-reduced-motion serves readers who are easily distracted as well as those who get dizzy.
Source: W3C: Media Queries Level 5
Topic: Motion with care · accessibility, standards, health
Lesson 10: CSS variables
Variables were once spelled var-
CSS variables were first spelled another way. A W3C draft of 10 April 2012 said that any property name starting with the prefix var- was a variable property, and it noted that others had suggested a dollar sign, as in $foo. Today's standard uses two dashes instead: a custom property is any property whose name starts with --.
Why it fits here: This topic's --brand is the final spelling, after var- and the dollar sign were tried.
Sources: W3C: CSS Variables Module, Working Draft 2012; W3C: CSS Custom Properties for Cascading Variables
Topic: A name for a value: custom properties · history, standards
CSS will never claim the double dash
The double dash is a promise. The Custom Properties standard says these properties are only for authors and users, and that CSS will never give them a meaning of its own, so a future version of CSS cannot clash with your names. Only the bare -- is kept back for future use. And --foo and --FOO are two distinct properties.
Why it fits here: It is why this topic's names are safe for good, and why their capital letters matter.
Source: W3C: CSS Custom Properties for Cascading Variables
Topic: A name for a value: custom properties · standards, words
var() works only in values
var() works only inside a property's value. The Custom Properties standard says it cannot be used as a property name, in a selector, or anywhere else besides a property value. That is why a media query's condition, such as (min-width: var(--wide)), cannot read a variable.
Why it fits here: It is the rule behind this topic's note that a query's condition cannot read a variable.
Source: W3C: CSS Custom Properties for Cascading Variables
Topic: How far a variable reaches · standards
Typed variables can glide
An ordinary variable cannot glide: the standard gives custom properties a discrete animation type, so they jump from one value to the next. The @property rule changes that. It registers a variable with a syntax, its type, and an inherits setting, and both are required. A registered variable then animates by its type, so a colour can fade smoothly.
Why it fits here: It extends this topic's variables: with @property, a variable can even be told not to inherit.
Sources: W3C: CSS Custom Properties for Cascading Variables; W3C: CSS Properties and Values API Level 1
Topic: How far a variable reaches · standards
Why the earlier line does not come back
The surprise in this topic has a reason. A normal mistake in CSS fails early, while the browser reads the style sheet, so the bad line is dropped and the earlier line stays. A variable cannot be checked until the page is styled, and by then the browser has already thrown away the other values. The standard calls this invalid at computed-value time.
Why it fits here: It explains why this topic's earlier line does not come back when a variable is missing.
Source: W3C: CSS Custom Properties for Cascading Variables
Topic: When a variable is missing · standards
A fallback can hold commas
A fallback may contain commas of its own. The Custom Properties standard says everything between the first comma and the end of var() is the fallback, so var(--foo, red, blue) has the fallback red, blue. That lets a whole font list, such as Arial, sans-serif, stand as one fallback.
Why it fits here: It shows how much this topic's fallback can hold: a whole list, not only one value.
Source: W3C: CSS Custom Properties for Cascading Variables
Topic: When a variable is missing · standards
The spaces around plus and minus
calc() is strict about spaces. The CSS Values standard requires whitespace on both sides of the + and - operators, though * and / may go without. The reason is the minus sign: in 50% -2px, the -2px reads as one negative number, not as minus 2px, so there is no sum to do.
Why it fits here: It is the rule behind this topic's warning that plus and minus need spaces around them.
Source: W3C: CSS Values and Units Level 4
Topic: Sums in CSS: calc() · standards, numbers
CSS knows pi
calc() grew into a full calculator. The CSS Values standard now has trigonometric functions, sin(), cos(), tan() and their inverses, along with pow() and sqrt(), and two constants: e, and pi, the ratio of a circle's circumference to its diameter, about 3.14159. The two constants work only inside a calculation.
Why it fits here: These sit beside this topic's calc(), in the same family of math functions.
Source: W3C: CSS Values and Units Level 4
Topic: Sums in CSS: calc() · numbers, maths, standards
color-scheme dresses the browser's own parts
Variables can repaint your own parts of a page, but not the browser's. The color-scheme property tells the browser which colour schemes a page is designed for. Matched with the reader's preference, it settles the used colour scheme, which affects the browser's own colours, such as those of form controls and scrollbars.
Why it fits here: It finishes this topic's themes: your variables dress the page, and color-scheme dresses the browser's parts.
Source: W3C: CSS Color Adjustment Level 1
Topic: A whole look in one place: themes · standards
light-dark() picks the colour for you
A newer function can replace a pair of theme values. CSS Color Level 5 defines light-dark(): given two colours, it gives the first when the element's colour scheme is light and the second when it is dark. So color: light-dark(black, white) follows the theme with no media query at all.
Why it fits here: It does in one function what this topic's themes do with role variables and a query.
Source: W3C: CSS Color Level 5
Topic: A whole look in one place: themes · standards
No fact matches. Try a shorter word, or take the tag off.