Most people who've written CSS think of it as the paint: colours, fonts, spacing, the bit that makes a page look right. That's what it was made for. But to do that job in a world of different screens, settings and states, CSS has grown a surprising amount of logic. This part looks at CSS on its own, setting aside what the HTML around it can do, and scores it against the four tests from part 1.
One thing to admit up front: CSS can't be looked at entirely alone, because it needs a document to apply to. HTML works without CSS; CSS doesn't work without something to style. We'll come back to that at the end, because the something doesn't have to be HTML.
Every rule is an if
A CSS rule has two halves: a selector, and the declarations that follow it.
li:nth-child(3n) {
background: coral;
}
Read it as a condition: if an element is the third, sixth, ninth… child of its parent, then give it a coral background. The browser checks that condition against every element in the document, all the time, and applies the result. When several rules match the same element and disagree, the cascade decides which wins, by fixed rules of precedence. So a stylesheet isn't a list of colours. It's a set of conditions and outcomes that the browser keeps evaluating.
Choosing
Selectors are CSS's way of choosing. They can test an element's position (:nth-child(3n)), its
type and attributes, its state (:hover, :checked, :invalid), and, since
:has() reached every major browser in 2023, what it contains. They can be combined with "not"
(:not()), "or" (:is()) and "and" (simply writing two together), which is everything you
need to build any logical condition.
Here, the HTML is nothing but twenty-four empty list items. CSS numbers them, fills every third and rings every fourth. The twelfth and twenty-fourth match both conditions, so they get both.
li { counter-increment: position; }
li::before { content: counter(position); }
li:nth-child(3n) { /* every third: filled */ }
li:nth-child(4n) { /* every fourth: ringed */ }
CSS can also choose based on its surroundings. Media queries test the device and the visitor's preferences; container queries test the size of the space an element sits in. Each is a branch: if this is true, use these rules; otherwise, use those. The two sentences below are written by CSS, after checking your settings and the width of the box.
@media (prefers-color-scheme: dark) {
.scheme::before { content: "Your device is set to dark mode."; }
}
@container (width < 30rem) {
.box::before { content: "This box is narrower than 30rem."; }
}
Resize the window, or switch your device between light and dark, and the sentences change.
Calculating
CSS has been able to do sums since calc() arrived over a decade ago, so that a sidebar could be "a
third of the width, less a gap". Since then it has gained min(), max() and
clamp(); rounding and remainders with round(), mod() and
rem(); and powers, roots and trigonometry with pow(), sqrt(),
sin() and their relatives.
Custom properties (often called CSS variables) give values names, like --gap: 1rem, so they can be
reused and calculated with. And counters, made for numbering headings and list items, can hold the result of a
calculation and print it on the page. Put those together and CSS will do arithmetic in front of you. Each answer
below is worked out by the browser's CSS engine:
.times {
counter-reset: result calc(var(--a) * var(--b));
}
.times::after {
content: counter(result);
}
So, with respect to Ben Romy, CSS can work out 2 + 2. What it can't do is ask anyone which numbers to use: the 7 and the 6 are written into the page.
And CSS keeps gaining logic. Newer versions of the language add an if() function, for choosing a
value inline, and @function, for defining reusable calculations of your own, and both have begun
arriving in browsers. Each release moves CSS a little further from "style sheet" and a little closer to
"language".
What CSS can't do alone, usually
For all that, CSS as most people write it is missing two things that programs depend on.
- Input. CSS can only read the document it styles and the environment it's shown in: the screen, the settings, where the pointer is. It can't be handed new information to work on.
-
Memory. A rule applies while its condition is true and stops the moment it isn't: move the
pointer away and
:hoveris forgotten. CSS can't change the document to leave a note for itself, so in everyday use, nothing it works out is kept for a later step.
Without memory there's no way to repeat a step and build on the result, which is what computing anything requires. So for most of its life, CSS could decide and calculate, but only about a world it couldn't change.
The exception is animation. An animation is CSS changing values over time by itself, with nothing else involved, and with enough ingenuity, and the newest features above, animations can act as a clock and carry values from one step to the next. In 2026 that was enough for someone to build a working processor in CSS, which part 4 comes to. It's a long way from everyday CSS, and so far it only runs in some browsers, but it means "CSS has no memory" is no longer simply true.
Not just for HTML
CSS needs a document, but it isn't tied to HTML. Browsers apply CSS to SVG graphics, and to plain XML documents that point to a stylesheet, like this made-up recipe format:
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="recipe.css"?>
<recipe>
<title>Toast</title>
<step>Put bread in toaster.</step>
</recipe>
Open that in a browser and recipe.css decides how a <recipe> and its steps are
shown, though the browser knows nothing about recipes.
You can even give CSS almost nothing to work with. A page whose only content is a stylesheet still has a
document: the browser quietly adds the <html>, <head> and
<body> elements it expects, and CSS can draw on them.
<!doctype html>
<style>
html::before {
content: "There is no body text on this page.";
}
</style>
So the honest picture is this: CSS needs some document, but it treats that document as raw material, the way a program treats its input.
How CSS scores
Against the four tests, CSS on its own comes out like this:
| Test | CSS | Why |
|---|---|---|
| Computes anything | Partly | Everyday CSS can't: nothing it works out feeds the next step. But with the newest features, animations can carry state forward, enough to run an emulated processor (x86CSS), so far only in some browsers. |
| Gives instructions | Yes | Every rule is an instruction to the browser about how something should look and behave. |
| Decides and remembers | Partly | It decides constantly, through selectors, media queries and container queries, but in everyday use it remembers nothing: its decisions follow state that something else holds. |
| Builds programs | No | It styles and arranges documents; on its own it makes nothing that does a job. |
It's a closer result than most people expect, and a different shape from HTML's. HTML can hold state but can't make decisions about it; CSS can make decisions all day, but in everyday use has no state of its own to make them about. Each has exactly what the other is missing. Part 4 puts them together.