Can CSS remember a value? A two-cell experiment
Write 3 to address 00 and switch to 01. The display reads 0. Go back to 00 and 3 is still there. This can happen without JavaScript or a storage API because the browser keeps the formβs radio selections while the documen
Write 3 to address 00 and switch to 01. The display reads 0. Go back to 00 and 3 is still there. This can happen without JavaScript or a storage API because the browser keeps the formβs radio selections while the document is alive. Selecting an address and writing to its cell are separate actions.
01. Address selection leaves the stored value alone
The demo has one radio group for the selected address and one for each of the two cells. CSS displays the selected state in the addressed group. Moving from 00 to 01 does not change which radio is selected in the 00 group. Mutual exclusion applies within a group, so the cells need distinct group names.
| Action | Address | Read value | Write bit |
|---|---|---|---|
| Reset | 00 | 0 | 0 |
| Write 3 | 00 | 3 | 1 |
| Select 01 | 01 | 0 | 0 |
| Select 00 | 00 | 3 | 1 |
| Write 3 again | 00 | 3 | 0 |
| Reset | 00 | 0 | 0 |
We clicked through all six rows in cloud Chrome, at a 1363 CSS px viewport and in a 360 CSS px frame. The second write kept the value at 3 and flipped the write bit from 1 to 0. The narrow frame is not a physical phone test.
Figure 1. Write 3 to 00, read 0 at 01, then return to 00 and read 3 again.
02. Write changes a radio in the selected cell
This demo always writes 3 and gives each cell just three (value, write bit) states: (0, 0) after Reset, (3, 1) after the first write, and (3, 0) after the second. CSS :has() checks the address and the selected cell state, then shows the HTML label for the next radio. The browserβs default label action changes the selection. The alternating bit tells you the state changed even when the value stayed the same; it does not count writes.
Figure 2. Writing 3 twice to 00 keeps the value at 3 while the write bit steps through 0 β 1 β 0.
03. Reproduce it with one HTML file
Save the shared code below as memory-map.html and open it in a browser that supports :has(). Press Reset and follow the six rows in order. If switching to 01 after writing only to 00 shows anything other than 0, the two cells are not independent. On a repeated write, watch the bit on the right rather than the value.
HTML: radio groups and checkedness Β· HTML: labels Β· Selectors Level 4: :has()
<!doctype html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Two-cell form memory</title>
<style>
.write, .read { display: none }
form:has(#a00:checked):has(#s00z:checked) [for="s00a"],
form:has(#a01:checked):has(#s01z:checked) [for="s01a"],
form:has(#a00:checked):has(#s00a:checked) [for="s00b"],
form:has(#a01:checked):has(#s01a:checked) [for="s01b"],
form:has(#a00:checked):has(#s00b:checked) [for="s00a"],
form:has(#a01:checked):has(#s01b:checked) [for="s01a"],
form:has(#a00:checked):has(#s00z:checked) .read00z,
form:has(#a01:checked):has(#s01z:checked) .read01z,
form:has(#a00:checked):has(#s00a:checked) .read00a,
form:has(#a01:checked):has(#s01a:checked) .read01a,
form:has(#a00:checked):has(#s00b:checked) .read00b,
form:has(#a01:checked):has(#s01b:checked) .read01b { display:inline }
.write { border:1px solid; padding:.35em .7em; cursor:pointer }
</style>
<form>
<fieldset><legend>Address</legend>
<label><input id="a00" name="addr" type="radio" checked>00</label>
<label><input id="a01" name="addr" type="radio">01</label>
</fieldset>
<div hidden>
<input id="s00z" name="cell00" type="radio" checked>
<input id="s00a" name="cell00" type="radio">
<input id="s00b" name="cell00" type="radio">
<input id="s01z" name="cell01" type="radio" checked>
<input id="s01a" name="cell01" type="radio">
<input id="s01b" name="cell01" type="radio">
</div>
<p>Value / write bit:
<span class="read read00z read01z">0 / 0</span>
<span class="read read00a read01a">3 / 1</span>
<span class="read read00b read01b">3 / 0</span>
</p>
<p>
<label class="write" for="s00a">Write 3</label>
<label class="write" for="s00b">Write 3</label>
<label class="write" for="s01a">Write 3</label>
<label class="write" for="s01b">Write 3</label>
<button type="reset">Reset</button>
</p>
</form>
In a separate 360 CSS px check, writing only to 01 produced value 3 and write bit 1 there. Address 00 still read value 0 and write bit 0.
04. Use it to teach state, not to keep real data
This makes a few state transitions easy to see. It relies on current form selections, so do not expect values to survive reopening a page. Browsers can restore form state; the explicit Reset is a better baseline than a reload.
The standalone example is a pointer-driven demonstration. Keyboard and screen-reader operation for Write, which is a label for a hidden radio, is not verified. For production interfaces that need accessible controls, durable data, or a history of changes, use explicit application state and accessible buttons.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.
