CSS and the question of typing

Languages are often sorted by how strongly or weakly typed they are, meaning whether variables are known at compile time. A familiar weak-typing case is adding an integer to a string that holds an integer:

result = 1 + "1";

That string might have come from a tangled chain of logic, or from a deliberate single source of truth. Despite what "weak" and "strong" suggest, strong typing is not automatically better: some situations call for flexibility over rigidity. There is also no formal definition of either term, so perceptions vary between people and shift over time.

Why TypeScript arrived

JavaScript is weakly typed, and that flexibility helped it spread on the web. As the web industrialized, workflows grew more complex, and TypeScript emerged to graft strong typing onto the language — functioning as a kind of "plugin." It is useful for complicated setups such as data-heavy e-commerce single-page applications, and its popularity means many new projects start with TypeScript by default.

Where the checking happens

Compile time is when a language is converted into machine code, before runtime, when the computer performs that code. On the web this gets murky. A TypeScript setup stitches together JavaScript pieces and compiles them into one JavaScript file for the browser. Compilation happens at combination, with TypeScript acting as an overseer that objects to broken typed conventions before that occurs.

A tooltip that reads, “var content: any. Property ‘content’ does not exist on type ‘PropsWithChildren<Props>’. ts(2339). View Problem (Option F8). No quick fixes available. Tooltip is pointing towards an argument called “content.”
A sample TypeScript error in VS Code.

The browser then ingests the combined file and applies its own compile time, which varies with the device, the browser's other work, and what other programs on the device are doing. TypeScript never reaches the browser directly, but its influence does: JavaScript is fragile, and TypeScript tries to catch errors upstream in the editor so they are less likely to stop a site or app from working.

CSS is declarative and strongly typed

CSS is a declarative, domain-specific programming language, and it is strongly typed. Values largely stay as authored, and when a value is invalid the browser discards the entire property.

The type system

The catalog of CSS types is extensive, and includes:

  • Textual types: globally scoped keywords (initial, inherit, unset, revert); custom idents, e.g. for grid-area names; strings like "hello"; URLs; and dashed idents (--), which create custom properties.
  • Numeric types: integers (0–9), real numbers such as 3.14, percentages such as 25%, dimensions (a number with a unit, e.g. 100px or 3s), ratios such as 16/9, and flex, a variable length used in grid calculation.
  • Quantity types: lengths — absolute (pixels, centimeters), relative (root ems, viewport height) and time such as 200ms — plus angles such as 15deg, frequencies such as 16Hz, and resolution such as 96dpi. Dimensions may contain percentages; lengths cannot.
  • Color types: keywords (named colors like papayawhip, transparent, currentColor); RGB in hex (#FF8764) or functional form (rgba(105, 221, 174, 0.5)); HSL/HSLA such as hsl(287, 76%, 50%); and system colors such as ButtonText.
  • Image types: image references to files or gradients, color-stop-list (two or more stops), linear-color-stop (a color and length), linear-color-hint (a length percentage used to interpolate color), and ending-shape, the circle or ellipse keyword for radial gradients.
  • 2D positioning types: the keywords top, right, bottom, left, center, and percentage lengths such as 25%.

What authoring CSS involves

Most CSS work is writing selectors and then declaring properties with their values; a collection of selectors gives content its visual form the way a collection of JavaScript logic creates a feature. CSS also has functions, and can do calculation, conditional logic, algorithmic expressions, state and mode-based behavior. Custom properties act as variables whose values can be updated dynamically. One can even solve fizzbuzz in CSS. And as with other languages, a "meta" layer exists in the thinking about how to organize and maintain code.

Silent failures, by design

CSS errors do not print warnings in the console: an invalid declaration simply produces visuals that fail to update as expected. This is resilience at work. CSS prioritizes showing content at all costs and renders every other valid declaration it can, in keeping with the language's design principles, the platform's principles, and the web's mission.

Demonstrations

Three examples show the guardrails holding: a simple declaration, a calculation, and a redefined custom property.

  1. Property/value mismatch. When a .banner block declares border-style: potato, the browser doesn't recognize the type. The other declarations in .banner still render, and swapping in the valid value dotted brings the border back. The property expects a globally scoped keyword or a dashed ident for a custom property.
  2. calc() with the wrong type. A p selector's font-size needs a numeric dimension like 1.5rem, but if the second calc() argument is the string "2rem", the function yields an invalid value for the property. The paragraph then falls back to the next applicable ancestor, the body's 1.5rem. Combining two custom properties inside calc() can also fail, since calc() does not accept dashed ident textual types — for instance multiplying --big: 500px by --small: 1em.
  3. A redefined custom property. In :root, --color-cyan is set to #953FE3; in .square it is redefined to top. top is a valid typed value, but not a type background-color accepts. The redefinition is scoped to .square, leaving other uses (the right-hand border on the phrase "Don't play to type") untouched, and removing it from .square restores the cyan background. Contrived, but it shows how redefining custom properties can slip out of control — a pattern seen in poorly communicated projects, larger CSS codebases, and preprocessing setups that generate custom properties at scale.

Tooling that narrows the gap

The absence of console warnings for CSS is arguably a flaw that has fueled negative perceptions of the language: expecting developers to spot a subtle visual shift asks too much and doesn't meet them where their other tools do. Two initiatives address this.

stylelint lints CSS and CSS-like preprocessor languages, and integrates with code editors, task runners, command line tools and GitHub actions:

A tooltip that reads, “var content: any. Property ‘content’ does not exist on type ‘PropsWithChildren<Props>’. ts(2339). View Problem (Option F8). No quick fixes available. Tooltip is pointing towards an argument called “content.”
stylelint terminal output.

Firefox's Developer Tools offer a strong suite of CSS inspection options, notably the ability to flag unused CSS — useful for finding selectors affected by a type mismatch:

Tooltip attached to an unused selector in the Developer panel. The tooltip reads, “vertical-align has no effect on this element since it’s not an inline or table-cell element. Try adding display: inline or display: table-cell. Learn more. Screenshot.”
Firefox Developer edition

The takeaway

CSS has been strongly typed for as long as it has existed, and it has matured considerably of late, with new features available today. As strongly typed JavaScript grows more common, the hope is that it helps developers get comfortable with CSS's firm but flexible approach.