Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <meta charset="UTF-8" />
<title>3D Binary CSS Calculator</title>
//- <link rel="stylesheet" href="./style.css" />

- const checkboxCount = 16;
- const cubeSides = ["front", "back", "left", "right", "top", "bottom"];
- const hexDigits = Math.ceil(checkboxCount / 4);
- const hexCharsHTML = "0123456789abcdef".split("").map(c=>`<div>${c}</div>`).join("");

<main>
	<section class="info">
		<h2>There is <u class="no-javascript">no JavaScript</u> on this page!</h2>
		<p>
			| This binary 3D calculator can add two binary numbers and display the result in binary, decimal and hexadecimal &ndash; completely without JavaScript! <br>
			| All of the logic is made using pure HTML &amp; CSS. <br>
			| Click on the boxes below to use the calculator.
		</p>
	</section>

	<section class="addition">
		for row in ["first", "second"]
			for val, i in Array(checkboxCount)
				<input type="checkbox" id="check-#{row}-#{checkboxCount - 1 - i}" />
			//-}
		//-}

		<table>
			<thead>
				<tr>
					<td>Binary</td>
					<td>Decimal</td>
					<td>Hexadecimal</td>
				</tr>
			</thead>
			<tbody>
				for row, rowIdx in ["first", "second"]
					<tr class="numbers">
						<td>
							<div class="labels #{row}-labels">
								<span class="wrapper">
									<span class="placeholder cube">
										for cubeSide in cubeSides
											<div class="side #{cubeSide}"></div>
										//-}
									</span>
								</span>
								for val, i in Array(checkboxCount)
									<span class="wrapper">
										<label class="cube" for="check-#{row}-#{checkboxCount - 1 - i}">
										for cubeSide in cubeSides
											<div class="side #{cubeSide}"></div>
										//-}
										</label>
									</span>
								//-}
							</div>
						</td>
						<td>
							<div class="decimal number-display #{row}"></div>
						</td>
						<td>
							<div class="hexadecimal number-display #{row}">
								for val, hexDigit in Array(hexDigits)
									<span class="hex-digit digit-#{hexDigits - hexDigit - 1}">
										<span class="hex-chars">!{hexCharsHTML}</span>
									</span>
								//-}
							</div>
						</td>
					</tr>
					<tr class="operators #{rowIdx ? 'equals' : 'plus'}">
						<td class="operator">#{rowIdx ? "=" : "+"}</td>
						<td class="operator">#{rowIdx ? "=" : "+"}</td>
						<td class="operator">#{rowIdx ? "=" : "+"}</td>
					</tr>
				//-}

				<tr class="numbers sum">
					<td>
						<div class="result-display">
							for val, i in Array(checkboxCount + 1)
								<span class="wrapper">
									<span class="show-check cube check-#{checkboxCount - i}">
										for cubeSide in cubeSides
											<div class="side #{cubeSide}"></div>
										//-}
									</span>
								</span>
							//-}
						</div>
					</td>
					<td>
						<div class="decimal number-display sum"></div>
					</td>
					<td>
						<div class="hexadecimal number-display sum">
							for val, hexDigit in Array(hexDigits)
								<span class="hex-digit digit-#{hexDigits - hexDigit - 1}">
									<span class="hex-chars">!{hexCharsHTML}</span>
								</span>
							//-}
						</div>
					</td>
				</tr>
			</tbody>
		</table>
	</section>
</main>

<footer>
	<p>
		| Made with <span style="color:red">❤</span> by <a href="https://benjaminaster.com">Benjamin Aster .com</a>
		| &bull; view on <a href="https://codepen.io/Benjamin_Aster/full/mdMweWZ">CodePen</a>, 
		| <a href="https://benjaminaster.com/3d-css-calculator">benjaminaster.com</a>,
		| <a href="https://github.com/BenjaminAster/3D-CSS-Calculator">GitHub</a>
	</p>
</footer>

              
            
!

CSS

              
                @use "sass:math";

$checkboxCount: 16;
$debug: false;
$isCodepen: true;

@function pow($number, $exponent) {
	$value: 1;

	@if $exponent > 0 {
		@for $i from 1 through $exponent {
			$value: $value * $number;
		}
	}

	@return $value;
}

:root {
	color-scheme: dark;
	font-size: min(1.4vw, 3.3vh);
	overflow: hidden;

	@if $debug {
		border: 1px solid darkKhaki;
	}
}

body {
	font-family: sans-serif;
	background-color: black;
	color: white;
	perspective: 25em;
	perspective-origin: 50% calc(50% - 2em);
	margin: 0;
	display: flex;
	align-items: center;
	justify-content: center;
	flex-direction: column;

	min-height: 99vh;
	min-width: 99vw;

	@if $debug {
		border: 1px solid sandyBrown;
	}

	* {
		transform-style: preserve-3d;
	}
}

main {
	text-align: center;

	@if $debug {
		border: 1px solid paleGreen;
	}
}

section.info {
	a {
		color: lightSkyBlue;
	}

	.no-javascript {
		color: yellow;
	}
}

footer {
	// position: absolute;
	// bottom: 0;

	flex-grow: 1;
	flex-basis: 1;
	width: 100%;
	text-align: center;
	display: flex;
	align-items: flex-end;
	justify-content: center;

	@if $debug {
		border: 1px solid paleTurquoise;
	}

	a {
		color: lightSkyBlue;
		text-decoration: none;
	}
}

section.addition {
	counter-reset: first-number second-number sum-number 0;

	table {
		font-size: inherit;
		color: white;
		background: transparent;
		display: block;
		width: 68rem;
		// padding-top: 4rem;
		:is(thead, tbody) {
			display: block;
		}
		@if $debug {
			border: 1px solid darkSeaGreen;
		}
		thead,
		tbody {
			display: block;
		}
		tbody {
			user-select: none;
		}
		tr {
			display: block;
			color: white;
			background: transparent;
			@if $debug {
				border: 1px solid red;
			}
			td {
				padding: 0;
				&:nth-of-type(1) {
					width: 50rem;
				}
				&:nth-of-type(2) {
					width: 8rem;
				}
				&:nth-of-type(3) {
					width: 8rem;
				}
			}
			&.numbers {
				height: 4em;
				td:first-of-type {
					transform: translateX(-1em) translateY(if($isCodepen, 0, -1em)) translateZ(-0.9em);
				}
			}
			&.operators {
				height: 1em;
			}
			td {
				display: inline-block;
				@if $debug {
					border: 1px solid aqua;
				}
				color: white;
				background: transparent;
				text-align: center;
			}
		}
	}

	:is(.operator, .number-display) {
		font-weight: bold;
		font-family: monospace;
		color: white;

		&.decimal {
			&::before {
				position: absolute;
				inset: 0;
				display: flex;
				justify-content: center;
				align-items: center;
				@if $debug {
					border: 1px dotted yellow;
				}
			}
		}
		&.operator {
			transform: translateY(-0.5em);
			font-size: 2em;
		}
		&.number-display {
			width: 100%;
			height: 100%;
			// transform: translateY(0.4em);
			// min-width: 5em;
		}
		&.sum {
			// transform: translateY(4em);
		}
	}

	@each $row in ("first", "second", "sum") {
		.decimal.#{$row}::before {
			content: counter(#{$row}-number);
		}
	}

	input[type="checkbox"] {
		position: absolute;
		z-index: -1;
		visibility: hidden;
	}

	:is(.result-display, .labels) {
		display: inline-flex;
		flex-wrap: wrap;
		gap: 1em;

		.wrapper {
			display: inline-block;
			width: 2em;
			height: 2em;

			.cube {
				display: inline-block;
				--global-transform: rotateX(calc(var(--is-one) * 90deg));

				width: 2em;
				height: 2em;
				color: black;
				font-weight: bold;
				font-family: monospace;
				padding: 1em;

				--is-one: 0;

				.side {
					position: absolute;
					width: 2em;
					height: 2em;
					background: #{rgba(lightBlue, 60%)};
					box-shadow: 0 0 0.5em #000a inset;
					transition: transform 250ms ease-in-out;
					&::before {
						width: 100%;
						height: 100%;
						display: flex;
						justify-content: center;
						align-items: center;
					}
				}

				.front {
					transform: var(--global-transform) translateZ(1em);
					&::before {
						content: "0";
					}
				}
				.right {
					transform: var(--global-transform) rotateY(90deg) translateZ(1em);
				}
				.back {
					transform: var(--global-transform) rotateY(180deg) translateZ(1em);
				}
				.left {
					transform: var(--global-transform) rotateY(-90deg) translateZ(1em);
				}
				.top {
					transform: var(--global-transform) translateY(-1em) rotateX(90deg);
				}
				.bottom {
					transform: var(--global-transform) translateY(1em) rotateX(90deg) rotateZ(180deg)
						rotateY(180deg);
					&::before {
						content: "1";
					}
				}
			}

			.placeholder {
				.front::before {
					content: "";
				}
				.bottom::before {
					content: "";
				}
			}
		}
	}

	.hexadecimal {
		@for $i from 0 to $checkboxCount {
			--bit-#{$i}-is-one: 0;
			&.sum {
				--bit-#{$i}-is-one: var(--check-#{$i}-sum);
			}
		}
		display: flex;
		justify-content: center;
		align-items: center;
		@if $debug {
			border: 1px solid lime;
		}
		// height: 7em;
		// width: 100%;
		&.sum::before {
			counter-reset: highest-sum-bit var(--check-#{$checkboxCount}-sum);
			content: counter(highest-sum-bit);
			position: absolute;
			display: flex;
			justify-content: center;
			align-items: center;
			inset: 0;
			transform: translateX(-2.5ch) translateY(0.2ch);
			@if $debug {
				border: 1px dotted magenta;
			}
		}
		.hex-digit {
			font-family: monospace;
			display: inline-block;
			height: 1em;
			width: 1ch;
			clip-path: inset(0);
			@if $debug {
				border: 1px solid orange;
			}
			.hex-chars {
				position: absolute;
				color: white;
				left: 0;
				transition: transform 0.3s;
				transform: translateY(calc(-2em * var(--hex-digit)));
				div {
					height: 2em;
				}
			}
		}
		@for $hexDigit
			from 0
			to
			(if($isCodepen, ceil($checkboxCount / 4), math.ceil(math.div($checkboxCount, 4))))
		{
			.digit-#{$hexDigit} {
				--hex-digit: calc(
					var(--bit-#{$hexDigit * 4 + 0}-is-one) *
						1 +
						var(--bit-#{$hexDigit * 4 + 1}-is-one) *
						2 +
						var(--bit-#{$hexDigit * 4 + 2}-is-one) *
						4 +
						var(--bit-#{$hexDigit * 4 + 3}-is-one) *
						8
				);
			}
		}
	}

	@for $i from 0 through $checkboxCount {
		$isFirstBox: ($i == 0);
		input#check-first-#{$i} {
			@each $isFirstChecked in (true, false) {
				&#{if($isFirstChecked, ":checked", ":not(:checked)")} {
					& ~ input#check-second-#{$i} {
						@each $isSecondChecked in (true, false) {
							&#{if($isSecondChecked, ":checked", ":not(:checked)")} {
								& ~ table :is(.result-display, .number-display.hexadecimal.sum) {
									--check-#{$i}-sum: #{if(
											$isFirstBox,
											if(
												$isFirstChecked,
												if($isSecondChecked, 0, 1),
												if($isSecondChecked, 1, 0)
											),
											if(
												$isFirstChecked,
												if(
													$isSecondChecked,
													calc(var(--check-#{$i - 1}-carry)),
													calc(1 - var(--check-#{$i - 1}-carry))
												),
												if(
													$isSecondChecked,
													calc(1 - var(--check-#{$i - 1}-carry)),
													calc(var(--check-#{$i - 1}-carry))
												)
											)
										)};
									--check-#{$i}-carry: #{if(
											$isFirstBox,
											if(
												$isFirstChecked,
												if($isSecondChecked, 1, 0),
												if($isSecondChecked, 0, 0)
											),
											if(
												$isFirstChecked,
												if($isSecondChecked, 1, calc(var(--check-#{$i - 1}-carry))),
												if($isSecondChecked, calc(var(--check-#{$i - 1}-carry)), 0)
											)
										)};
								}
							}
						}
					}
				}
			}
		}

		@each $row in ("first", "second") {
			input#check-#{$row}-#{$i}:checked {
				counter-increment: #{$row}-number #{pow(2, $i)} sum-number #{pow(2, $i)};

				& ~ table .#{$row}-labels {
					label[for="check-#{$row}-#{$i}"] {
						--is-one: 1;
					}
				}

				& ~ table .hexadecimal.#{$row} {
					--bit-#{$i}-is-one: 1;
				}
			}
		}

		:is(.result-display, .number-display.hexadecimal.sum) {
			--check-#{$checkboxCount}-sum: var(--check-#{$checkboxCount - 1}-carry);

			.show-check.check-#{$i} {
				--is-one: var(--check-#{$i}-sum);
			}
		}
	}
}

              
            
!

JS

              
                
// No JavaScript!

              
            
!
999px

Console