Demo

Adding machine in HTML and CSS

Set two binary numbers with switches, and CSS adds them up, carries and all.

What it is

An adding machine for two numbers, A and B, from 0 to 15. Click the switches to set them. CSS adds them in binary, one column at a time, carrying as it goes, and writes the answer underneath.

It starts on 2 + 2, for Ben Romy, who wrote that you can't compute it in HTML.

Working example

Carry A B Sum

How it works

In binary, each column is worth twice the one to its right: 1, 2, 4, 8, 16. Adding works the way you learnt at school, except that a column can only hold 0 or 1. Add the column's two bits and the carry from the right; write down whether the total is odd or even, and carry the rest to the next column.

HTML's part is to hold the eight bits of A and B. Each switch is a checkbox, hidden behind a label that's drawn as a 0 or a 1.

The HTML
<input class="bit" type="checkbox" id="a1" checked>
<label for="a1">A, the 2s bit</label>

From switches to numbers

CSS can't calculate with a ticked box, so it first turns each one into a number. Every bit has a custom property that starts at 0, and a rule that sets it to 1 when its box is checked. From here on, it's all arithmetic.

From switches to numbers source
.adder { --a1: 0; }
.adder:has(#a1:checked) { --a1: 1; }

Adding a column

A column adds three things: its bit of A, its bit of B, and the carry in. mod(…, 2) is the remainder after dividing by 2, which is the bit that's written down. Dividing by 2 and rounding down gives the carry. If all three are 1, for example, the total is 3: write down 1, and carry 1.

Adding a column source
--s1: mod(var(--a1) + var(--b1) + var(--c1), 2);
--c2: round(down, (var(--a1) + var(--b1) + var(--c1)) / 2, 1);

Passing the carry along

Each column's carry is the next column's carry in, so the four columns form a chain. Nothing is carried into the first, and whatever is carried out of the last becomes the fifth bit of the answer. CSS works out the order for itself: each property refers to the one before it, so the browser follows the chain from right to left.

Passing the carry along source
--c0: 0;
--s0: mod(var(--a0) + var(--b0) + var(--c0), 2);
--c1: round(down, (var(--a0) + var(--b0) + var(--c0)) / 2, 1);
--s1: mod(var(--a1) + var(--b1) + var(--c1), 2);
--c2: round(down, (var(--a1) + var(--b1) + var(--c1)) / 2, 1);
--s2: mod(var(--a2) + var(--b2) + var(--c2), 2);
--c3: round(down, (var(--a2) + var(--b2) + var(--c2)) / 2, 1);
--s3: mod(var(--a3) + var(--b3) + var(--c3), 2);
--c4: round(down, (var(--a3) + var(--b3) + var(--c3)) / 2, 1);
--s4: var(--c4);

Writing the answer

A custom property can't be printed directly, but a counter can. Each answer tile sets a counter to its bit and prints it. The same bit sets how much colour is mixed into the tile: 0% for a 0, and 100% for a 1.

Writing the answer source
.computed {
    counter-reset: bit var(--bit);
    background: color-mix(in srgb, var(--lit) calc(var(--bit) * 100%), var(--surface-alt));
}

.computed::before {
    content: counter(bit);
}

The sum underneath is written the same way, so a screen reader can read it, but may not announce it as it changes, which would take a script.

Checking it in decimal

The decimal totals, beside the bits on wider screens and in the sum underneath, are worked out by adding up the columns that are on. The sum's total comes from the answer's bits, not from A and B, so if the adder went wrong, it would show.

Checking it in decimal source
.value-sum {
    counter-reset: value calc(var(--s0) + 2 * var(--s1) + 4 * var(--s2) + 8 * var(--s3) + 16 * var(--s4));
}

.decimal::before {
    content: '= ' counter(value);
}

The end result

Whatever you set, from 0 + 0 to 15 + 15, each column passes its carry to the next, and the answer is written in binary and in decimal, exactly as an adding circuit in a processor would work it out.

Why it matters

So you can compute the sum of 2 + 2 with HTML and CSS. What makes it work is the split from part 4: HTML remembers the inputs, and CSS does the arithmetic.

It's also arithmetic the way a computer does it. Nothing here knows what 2 + 2 is; the answer comes out of a chain of columns, each passing a carry to the next, which is how adding is done in a processor.