Chapter 4: Vanilla CSS
The chapter introduces direct use of modern CSS for Vanilla Web apps and gives principles for keeping styles maintainable.
After this chapter you will be able to
- Native CSS for maintainability. Explain why a Vanilla Web project can use modern CSS directly instead of relying on a CSS framework or preprocessor, and describe the roles of the native features introduced here.
- Semantic stylesheet conventions. Describe how naming classes by UI role and keeping selectors short and stable contribute to maintainable CSS.
- Modern CSS maintainability. Explain why Vanilla CSS need not rely on outdated techniques to produce readable, maintainable stylesheets, and state the section’s claim about the availability of recent CSS additions.
- Explicit cascade layer order. Explain how cascade layers order groups of CSS rules without relying on specificity tricks, and trace how the example’s layers affect a button.
- Reusable design values. Explain how custom properties replace hard-coded design values, how CSS references them with var(), and how inheritance and overrides make them reusable across the document.
- Scoped theme overrides. Describe how overriding a set of custom-property tokens themes either the whole document or a subtree, and how automatic defaults can give way to a selected theme.
- Container-Based Layout Decisions. Explain how container queries differ from viewport media queries and why that matters for a card placed in a narrow sidebar on a wide page.
- Container Query Setup And Scoping. Describe how to designate a parent as a size container, write a container rule, and optionally scope queries with container names.
- CSS nesting and parent selectors. Explain how nested selectors group related rules, what `&` represents, and why nesting can be clearer than separate root-level selectors.
- Selector grouping and specificity. Distinguish :is() from :where(): both group selectors, but :is() takes the highest specificity among its arguments while :where() has zero specificity.
- Content-dependent parent styling. Explain how :has() lets you style an element according to what it contains, and state the caution about using it on extensive documents.
- Contextual focus styles. Explain when :focus-visible applies focus styles and why that is useful for keyboard and mouse users.
- Consistent relative unit choices. Justify preferring relative units for scalable Vanilla CSS while retaining absolute units for fixed measurements. Describe how established units such as %, em, and rem still fit into that choice.
- Dynamic viewport height units. Explain the mobile sizing problem with the original viewport units and describe the section's suggested use of dvh.
- Modern layout choices. Distinguish when the section recommends Flexbox, grids or subgrids, and absolute positioning, and describe which legacy layout techniques it advises avoiding.
- Flex container and items. Explain how marking a parent as a flex container makes its direct children flex items, and describe their default direction and basic layout controls.
- Two-dimensional Grid layout. Distinguish Grid from Flexbox and describe how a grid container defines rows and columns while its children can be positioned, aligned, or span tracks without extra wrappers.
- Responsive grid tracks. Describe how named areas or explicit rows and columns establish a grid, and how responsive tracks and container queries let its layout adapt to available space.
- Inherited subgrid tracks. Explain how subgrid lets a nested grid share its parent's column or row tracks, and why that helps align repeated components and maintain their layout.
- Purposeful platform-native animation. Explain why Vanilla Web projects can use built-in animation tools instead of JavaScript libraries, and describe the section’s guidance for considerate motion.
- CSS transition behavior and controls. Explain that a CSS transition smoothly animates a property from its current value to a new value when state changes, and identify what can be controlled and where transitions are useful.
- Keyframe animation use. Explain when to use a keyframe animation rather than a simple transition, and name the CSS constructs the section identifies for it.
- View transition options. Explain that the View Transitions API animates changes between views or pages, and distinguish how SPAs and MPAs enable those transitions.
- Scroll and view timelines. Explain how scroll-driven animations differ from time-based animations, and distinguish using scroll() to scrub a keyframed animation from using view() and view-timelines as an element enters the viewport.
What it builds on
- Semantic HTML (chapter 3) · The preceding discussion of semantic HTML provides context for naming styles by meaning.
- Avoiding divitis and other problems (chapter 3) · The concern about divitis and unnecessary markup.
Section by section
Introduction
The chapter introduces direct use of modern CSS for Vanilla Web apps and gives principles for keeping styles maintainable.
- Native CSS for maintainability: Explain why a Vanilla Web project can use modern CSS directly instead of relying on a CSS framework or preprocessor, and describe the roles of the native features introduced here.
Why it matters: These built-in features can keep a project's styles small and understandable without adding styling tools. - Semantic stylesheet conventions: Describe how naming classes by UI role and keeping selectors short and stable contribute to maintainable CSS.
Why it matters: Naming by purpose and avoiding brittle selectors makes styles easier to change as a UI evolves.
Key terms: Vanilla CSS, custom properties, Cascade Layers, container queries, preprocessor, semantic classes, UI role, component, selectors
4.1 Maintainable and modular CSS
This section counters the idea that Vanilla CSS requires outdated workarounds and introduces modern CSS as a practical basis for maintainable stylesheets.
- Modern CSS maintainability: Explain why Vanilla CSS need not rely on outdated techniques to produce readable, maintainable stylesheets, and state the section’s claim about the availability of recent CSS additions.
Why it matters: You can approach Vanilla CSS as a maintainable option rather than assuming it requires outdated tricks.
Key terms: Vanilla CSS, modern CSS, Baseline
4.1.1 Cascade Layers
This section explains how cascade layers make CSS override order explicit, including how to keep third-party styles from unexpectedly overriding components.
- Explicit cascade layer order: Explain how cascade layers order groups of CSS rules without relying on specificity tricks, and trace how the example’s layers affect a button.
Why it matters: An explicit layer order makes intended overrides easier to reason about than specificity tricks. - Third-party style isolation: Describe why the section recommends putting third-party CSS in its own layer.
Why it matters: Separating third-party styles helps keep their effects on your components predictable.
Key terms: cascade layers, reset, base, components, utilities, third-party CSS, layer
4.1.2 Custom properties (variables)
Explains how CSS custom properties hold reusable design values and how scoped overrides create themes.
- Reusable design values: Explain how custom properties replace hard-coded design values, how CSS references them with var(), and how inheritance and overrides make them reusable across the document.
Why it matters: One shared value can control many uses while still allowing local customization. - Scoped theme overrides: Describe how overriding a set of custom-property tokens themes either the whole document or a subtree, and how automatic defaults can give way to a selected theme.
Why it matters: Changing token values at the right scope can retheme an interface without rewriting its CSS rules. - Stable theme token names: Justify semantic custom-property names and explain why a component should expose only the tokens intended for theming.
Why it matters: A small, purposefully named set of tokens makes themes easier to change without disturbing component internals.
Key terms: custom properties, variables, var(id), :root, inherit, theming, tokens, html, container, media queries
4.1.3 Container queries and units
This section explains how container queries and related CSS units make component layouts respond to their surroundings rather than only to the viewport.
- Container-Based Layout Decisions: Explain how container queries differ from viewport media queries and why that matters for a card placed in a narrow sidebar on a wide page.
Why it matters: A card can adapt to the space it actually occupies, not just the width of the page. - Container Query Setup And Scoping: Describe how to designate a parent as a size container, write a container rule, and optionally scope queries with container names.
Why it matters: Scoping responsive rules to components helps keep them portable and avoids unintended matches. - Container And Dynamic Viewport Units: Distinguish container-relative units from dynamic viewport units and state the small-screen reason for preferring dvh over vh.
Why it matters: Choosing the right reference for CSS measurements helps components fit their containers and small-screen layouts remain stable.
Key terms: container queries, media queries, viewport, container-type, inline-size, size, @container, container names, container units, cqw
4.1.4 Nested selectors
This section explains how CSS nesting organizes related selectors and reduces repetition.
- CSS nesting and parent selectors: Explain how nested selectors group related rules, what `&` represents, and why nesting can be clearer than separate root-level selectors.
Why it matters: It helps you keep related CSS rules together without repeating their parent selector.
Key terms: CSS nesting, nested selectors, parent selector, specificity
4.1.5 New pseudo-selectors
Introduces four modern CSS pseudo-selectors for writing concise, intentional styles, including their specificity, accessibility, and performance implications.
- Selector grouping and specificity: Distinguish :is() from :where(): both group selectors, but :is() takes the highest specificity among its arguments while :where() has zero specificity.
Why it matters: This distinction helps you choose between concise selectors and defaults that remain easy to override. - Content-dependent parent styling: Explain how :has() lets you style an element according to what it contains, and state the caution about using it on extensive documents.
Why it matters: It enables content-dependent styling without extra HTML hooks, provided the selector stays narrowly scoped. - Contextual focus styles: Explain when :focus-visible applies focus styles and why that is useful for keyboard and mouse users.
Why it matters: It preserves useful keyboard focus cues while avoiding unnecessary mouse-click outlines.
Key terms: :is(), :where(), specificity, :has(), parent selector, :focus-visible, focus styles, keyboard navigation
4.1.6 Modern CSS units
This section explains how modern CSS units size layouts and typography relative to the viewport, containers, text, and writing mode, and how to choose units consistently.
- Consistent relative unit choices: Justify preferring relative units for scalable Vanilla CSS while retaining absolute units for fixed measurements. Describe how established units such as %, em, and rem still fit into that choice.
Why it matters: A consistent sizing approach helps a design scale without making its CSS harder to change. - Dynamic viewport height units: Explain the mobile sizing problem with the original viewport units and describe the section's suggested use of dvh.
Why it matters: Visible-height sizing needs to account for a mobile browser's changing address bar. - Text metric sizing units: Describe how lh, rlh, cap, and ic relate sizing to text metrics and why that helps text-heavy interfaces.
Why it matters: Text-relative sizing can keep an interface visually consistent when its fonts or languages vary. - Container relative sizing units: Explain what container units measure relative to and how that differs from sizing solely by the viewport.
Why it matters: A component's available space may depend on its container, not just the browser window. - Writing mode logical units: Describe how vi and vb adapt measurements to the writing mode.
Why it matters: Writing-mode-aware sizing helps a layout adapt to different text directions.
Key terms: relative units, absolute units, px, %, em, rem, vh, vw, dvh, lvh
4.2 Modern Layout
Introduces modern CSS layout choices for clean, semantic Vanilla Web pages and distinguishes them from legacy layout techniques.
- Modern layout choices: Distinguish when the section recommends Flexbox, grids or subgrids, and absolute positioning, and describe which legacy layout techniques it advises avoiding.
Why it matters: These distinctions give you a starting point for choosing a layout approach without falling back on older workarounds. - Semantic HTML layout foundation: Explain why the section starts its discussion of modern layouts with clean, semantic HTML.
Why it matters: Keeping the document straightforward helps carry the same simplicity into its CSS.
Key terms: Flexbox, grids, subgrids, absolute positioning, float and clear, semantic HTML, divitis, classitis, stylesheets
4.2.1 1D layout with Flexbox
This section explains how Flexbox lays out items along one axis and why it is useful for adaptable UI components.
- Flex container and items: Explain how marking a parent as a flex container makes its direct children flex items, and describe their default direction and basic layout controls.
Why it matters: This gives you the basic model for reasoning about a Flexbox layout. - One-axis component layouts: Distinguish the UI layouts Flexbox suits from complex page grids, and explain its advantage over older layout workarounds.
Why it matters: Choosing Flexbox for the right kind of layout avoids brittle positioning tricks. - Flexible responsive behavior: Describe how growth and shrinkage let Flexbox items adapt to available space, using an example from the section.
Why it matters: Flexbox can handle some changes in available space without separate breakpoint rules.
Key terms: Flexbox, flex container, flex items, justify-content, align-items, UI components, complex page grids, growth and shrink values, media queries
4.2.2 2D layout with grids
This section explains how CSS Grid creates responsive two-dimensional layouts and how subgrid keeps nested layouts aligned with their parent.
- Two-dimensional Grid layout: Distinguish Grid from Flexbox and describe how a grid container defines rows and columns while its children can be positioned, aligned, or span tracks without extra wrappers.
Why it matters: Grid makes layouts that need alignment in both directions easier to express without complicating the markup. - Responsive grid tracks: Describe how named areas or explicit rows and columns establish a grid, and how responsive tracks and container queries let its layout adapt to available space.
Why it matters: A gallery can adjust its columns to the space its component actually has. - Spacing within grid layouts: Explain why the section recommends gap between grid items and reserves margins for spacing outside the layout context.
Why it matters: Choosing gap keeps internal spacing and alignment easier to manage. - Inherited subgrid tracks: Explain how subgrid lets a nested grid share its parent's column or row tracks, and why that helps align repeated components and maintain their layout.
Why it matters: Subgrid lets repeated components stay aligned without maintaining duplicate track definitions.
Key terms: CSS Grid, Flexbox, tracks, grid container, named areas, minmax, fractional units, auto-fit, auto-fill, container queries
4.3 Animations
Introduces platform-native animation as the Vanilla Web approach and gives guidance on motion preferences and performance.
- Purposeful platform-native animation: Explain why Vanilla Web projects can use built-in animation tools instead of JavaScript libraries, and describe the section’s guidance for considerate motion.
Why it matters: Built-in tools can provide motion without a library, while restraint and motion preferences help make it considerate. - Animation performance choices: Explain why the section recommends transform and opacity for transitions and animations, and how to check performance when uncertain.
Why it matters: Choosing properties that usually avoid layout work can make animations perform better.
Key terms: Vanilla Web, prefers-reduced-motion, transform, opacity, compositor, layout thrashing, DevTools
4.3.1 Transitions
This section explains how CSS transitions animate state changes and where they are useful.
- CSS transition behavior and controls: Explain that a CSS transition smoothly animates a property from its current value to a new value when state changes, and identify what can be controlled and where transitions are useful.
Why it matters: Transitions can make small interface state changes feel smooth.
Key terms: CSS transitions, duration, delay, easing, micro-interactions
4.3.2 Keyframe animations
This section introduces keyframe animations as a way to create multi-step CSS motion that can loop when a simple transition is insufficient.
- Keyframe animation use: Explain when to use a keyframe animation rather than a simple transition, and name the CSS constructs the section identifies for it.
Why it matters: It helps you choose the appropriate CSS animation approach for motion with more than one step.
Key terms: keyframe animations, @keyframes, animation shorthand, transition
4.3.3 Page transitions
Introduces built-in view transitions and the different ways SPAs and MPAs use them.
- View transition options: Explain that the View Transitions API animates changes between views or pages, and distinguish how SPAs and MPAs enable those transitions.
Why it matters: The approach depends on whether navigation updates one document or moves between documents.
Key terms: View Transitions API, SPAs, document.startViewTransition, MPAs, @view-transition, same-origin navigations
4.3.4 Scroll-driven animations
Introduces scroll-driven animations as motion tied to scrolling, with a brief distinction between scroll and view timelines.
- Scroll and view timelines: Explain how scroll-driven animations differ from time-based animations, and distinguish using scroll() to scrub a keyframed animation from using view() and view-timelines as an element enters the viewport.
Why it matters: It helps you recognize which kind of scrolling should drive an animation.
Key terms: scroll-driven animations, animation-timeline, scroll(), view(), view-timelines
4.4 Summary
Where this is used later
- (chapter intro) (chapter 5) · CSS as another part of the interface JavaScript enhances.
- Setting up the project (chapter 15) · The guidance to keep CSS maintainable and modular.
- Read custom properties for theming (chapter 7) · Components read inherited custom properties for theming.
- Working with styles (chapter 6) · Introduces CSS custom properties as variables.
- Expose custom properties for parametrization (chapter 7) · Components expose custom properties for parametrization.
- Motion and accessibility (chapter 11) · Provides the motion-preference principle for route-change animation.
- View Transitions API (chapter 11) · Builds on the introduction to the View Transitions API.
- Cross-document view transitions (chapter 11) · Builds on the MPA cross-document opt-in and its same-origin condition.
Concepts explained in this chapter
- @containerA container query is a CSS conditional rule that applies styles based on the size of an element's containing ancestor rather than the viewport.
- container-relative unitA container unit is a CSS length unit whose value is calculated from the size of a query container.
- CSS GridCSS Grid is a layout model for arranging elements in two-dimensional rows and columns.
- subgridSubgrid is a CSS Grid feature that lets a nested grid participate in the track sizing and alignment of its parent grid.
- CSS variableA CSS custom property is an author-defined property whose value can be inherited and reused in CSS declarations through the var() function.
- CSSVanilla CSS is an approach to styling web interfaces with modern CSS directly, without CSS pre-compilers or core styling libraries.
- Shadow DOMShadow DOM is a web platform feature for attaching an encapsulated DOM tree to an element.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
