Demo
Logic gates in HTML and CSS
AND, OR, NOT and their relatives, the building blocks of every digital circuit, wired up with checkboxes.
What it is
Boolean algebra is the branch of mathematics where every value is either true or false, usually written 1 and 0. Instead of adding and multiplying, its main operations are AND (written A • B), OR (written A + B) and NOT (written as a bar over A). George Boole introduced it in The Mathematical Analysis of Logic in 1847, and set it out more fully in An Investigation of the Laws of Thought in 1854.
A logic gate is one of those operations made real: inputs go in, and a single output comes out. Below are seven of them. Click the switches on the left of each row to set its inputs, A and B, to 0 or 1; CSS works out the output on the right.
Working example
AND
1 when A and B are both 1
OR
1 when A or B (or both) is 1
XOR
1 when exactly one of A and B is 1
NAND
0 only when A and B are both 1
NOR
1 only when A and B are both 0
XNOR
1 when A and B are the same
NOT
The opposite of A
How it works
Every gate is built on the same principle, with the work split between HTML and CSS.
HTML's part is memory. Each switch is an ordinary <input type="checkbox">, hidden behind its
<label>. Clicking the label ticks or unticks the box: ticked is 1, unticked is 0. The tile on the
right is an <output> element, which is empty in the HTML.
The HTML
<div class="gate gate-and">
<input class="bit a" type="checkbox" id="and-a">
<label for="and-a">AND, input A</label>
<input class="bit b" type="checkbox" id="and-b">
<label for="and-b">AND, input B</label>
<output class="output" for="and-a and-b"></output>
</div>
Drawing the tiles
CSS's part is logic. Each tile draws its number from a custom property, --bit, which starts at 0. A
gate's rules test its boxes with :checked and :not(:checked), and :has()
lets the gate as a whole see what's been ticked inside it. When a rule matches, it sets the output's
--bit to 1 and changes the tile's colour; when none does, the output stays at 0.
Drawing the tiles source
.output { --bit: '0'; }
.output::before { content: var(--bit); }
Only the rules differ from gate to gate. Here is each one, with its truth table: every combination of inputs, and the output it should give.
AND, written A • B
One rule. It looks for a gate that has a checked A and a checked B. Writing two :has() tests side by side means both must hold, so the rule only applies when both switches are on.
AND source and truth table
.gate-and:has(.a:checked):has(.b:checked) .output {
--bit: "1";
}
| A | B | Output |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
OR, written A + B
Two selectors, one for each input, separated by a comma. A comma in CSS already means "or": either selector can switch the output on, and if both match, it's simply on.
OR source and truth table
.gate-or:has(.a:checked) .output,
.gate-or:has(.b:checked) .output {
--bit: "1";
}
| A | B | Output |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
XOR, written A ⊕ B
Exclusive or: one or the other, but not both. Two selectors cover the two ways that can happen, A on with B off, and A off with B on. When both are on, neither matches, so the output falls back to 0.
XOR source and truth table
.gate-xor:has(.a:checked):has(.b:not(:checked)) .output,
.gate-xor:has(.a:not(:checked)):has(.b:checked) .output {
--bit: "1";
}
| A | B | Output |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
NAND, written A • B
Not and: the opposite of AND. The output is on whenever A is unchecked or B is unchecked, so there's a selector for each. Only when both are checked does neither match.
NAND source and truth table
.gate-nand:has(.a:not(:checked)) .output,
.gate-nand:has(.b:not(:checked)) .output {
--bit: "1";
}
| A | B | Output |
|---|---|---|
| 0 | 0 | 1 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
NOR, written A + B
Not or: the opposite of OR. One rule, which needs A to be unchecked and B to be unchecked at the same time. Checking either one switches it off.
NOR source and truth table
.gate-nor:has(.a:not(:checked)):has(.b:not(:checked)) .output {
--bit: "1";
}
| A | B | Output |
|---|---|---|
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 0 |
XNOR, written A ⊕ B
The opposite of XOR, so it's a test for equality. Two selectors cover the two ways the inputs can match: both checked, or both unchecked.
XNOR source and truth table
.gate-xnor:has(.a:checked):has(.b:checked) .output,
.gate-xnor:has(.a:not(:checked)):has(.b:not(:checked)) .output {
--bit: "1";
}
| A | B | Output |
|---|---|---|
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
NOT, written A
The simplest gate, with a single input and a single rule: the output is on when A is unchecked.
NOT source and truth table
.gate-not:has(.a:not(:checked)) .output {
--bit: "1";
}
| A | Output |
|---|---|
| 0 | 1 |
| 1 | 0 |
The end result
Seven gates, each giving the output its truth table asks for, from 11 selectors between them.
Why it matters
These gates are the vocabulary of digital electronics. NAND alone is enough to build every other gate, and from gates come adders, comparators and, eventually, a computer's processor. The adding machine takes the next step.
A processor needs two more things: memory to hold its results, and a clock to move it from one step to the next. Here, the checkboxes are the memory, and you are the clock.