More experiments in visualizing with checkboxes.

This time I wanted to see if I could use the checkboxes to represent an 8-bit binary number and calculate the decimal number from which inputs were checked. Using pure HTML and CSS, no JavaScript.

I'm going to do this in 2 ways. The easy way and the hard way.

The easy way #

Using CSS content, custom properties, and calc().

The one drawback to using CSS content is that it is not good for accessibility.

Since this one is easy, I can show you the CSS:

fieldset#css-content {
  --b1: 0;
  --b2: 0;
  --b3: 0;
  --b4: 0;
  --b5: 0;
  --b6: 0;
  --b7: 0;
  --b8: 0;

  &:has(#bit-1:checked) {--b1: 1; }
  &:has(#bit-2:checked) {--b2: 2; }
  &:has(#bit-3:checked) {--b3: 4; }
  &:has(#bit-4:checked) {--b4: 8; }
  &:has(#bit-5:checked) {--b5: 16; }
  &:has(#bit-6:checked) {--b6: 32; }
  &:has(#bit-7:checked) {--b7: 64; }
  &:has(#bit-8:checked) {--b8: 128; }

  & output::after {
    --result: calc(var(--b1) + var(--b2) + var(--b3) + var(--b4) + var(--b5) + var(--b6) + var(--b7) + var(--b8));
    counter-reset: result-counter var(--result);
    content: "Value: " counter(result-counter);
    display: inline-block;
  }
}

The hard way #

The only other way I could think of doing this is by having 256 <span> elements and using a ridiculous amount of CSS to hide/show the correct number based on what inputs were checked.

And that's exactly what I did. I'm using a combination of :has() and :not(:has()) to detect every possible combination of checked inputs. Basically the CSS equivalent of brute forcing the solution.

Disclaimer: I wrote the HTML and most of the CSS for this one, but there's no way I was writing all of the central CSS logic by hand. I asked AI to output that part specifically. It would have taken me forever to do it myself.

Here it is in action:

Value:  0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

It works and is more accessible!

This CSS is way too big to show you, so you'll need to view the page source where the CSS is both minified and post-css processed.