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

              
                //- pug index.pug -w
- const rows = 100;
- const columns = 100;
- const colors = 3;
<html lang="en" style="--rows: #{rows}; --columns: #{columns}">

<body>
	<aside class="info">
		<div class="inner-container">
			<h1>CSS Drawing Canvas</h1>

			<p>There is no JavaScript on this page. All logic is made 100% using pure HTML & CSS.</p>

			<p>
				| To edit the colors, simply type into the "Edit colors" input fiels.
				| Any valid CSS color syntax is valid, including <a href="https://benjaminaster.com/colors/">named colors</a> and hex notation.
			</p>

			<div class="mobile-warning">
				| This does not work on mobile. Please use a desktop computer instead.
			</div>

			<div>
				<p>For the best performance,</p>
				<ul>
					<li>close DevTools</li>
					<li>close other tabs</li>
					<li>close resource-intensive applications</li>
				</ul>
			</div>

			<p>
				| View on <a href="https://github.com/BenjaminAster/CSS-Drawing-Canvas">GitHub</a>,
				| <a href="https://codepen.io/Benjamin_Aster/pen/LYBLwNv">CodePen</a>,
				| <a href="https://benjaminaster.com/css-canvas/">benjaminaster.com</a></p>
		</div>
	</aside>

	<main>
		<div class="inner-container">
			each _, index in Array(colors)
				<input type="radio" name="color" id="color-#{index}" !{index === 0 ? "checked" : ""} />
			//- /each

			<div class="color-chooser">
				each _, index in Array(colors)
					<label for="color-#{index}"></label>
				//- /each
			</div>

			<div class="canvas-container">
				<div class="canvas">
					for _, row in Array(rows)
						for _, column in Array(columns)
							<div class="pixel"></div>
						//- /for
					//- /for
				</div>
			</div>

			<h2 class="edit-colors-heading">
				| Edit colors:
			</h2>

			<div class="color-settings-container">
				<div class="color-settings">
					- const defaultColors = ["red", "lime", "blue"];
					for _, index in Array(colors)
						- const style = `:root { --color-${index}:\n${defaultColors[index]}`;
						<div class="outer-style-container">
							<div class="middle-style-container">
								<div class="inner-style-container" id="color-#{index}-style">
									<style contenteditable="true">!{style}</style>
								</div>
							</div>
						</div>
					//- /for
				</div>
			</div>
		</div>
	</main>
</body>

</html>
              
            
!

CSS

              
                @use "sass:list";

/*
pug index.pug -w
sass main.scss:main.css -w
*/

$colors: 3;

@function repeat-string($character, $n){
	$c: "";
	@for $i from 1 through $n {
		$c: $c + $character;
	}
	@return $c;
}

@layer global {
	label {
		cursor: pointer;
	}

	h1, h2, h3, p, ul {
		margin: 0;
	}

	h1, h2, h3 {
		font-weight: 600;
	}
}

:root {
	color-scheme: dark;
	--pixel-size: 4px;
	user-select: none;

	--color-0: initial;
	--color-1: initial;
	--color-2: initial;

	--columns: initial;
	--rows: initial;

	--canvas-border-width: 1px;
	--canvas-margin-block: 1rem;
	--edit-colors-heading-height: 3rem;
	--edit-colors-margin-left: 2.5rem;
	--colors-line-height: 3rem;
	--colors-font-size: 2rem;

	--background: #111;
	color: white;

	-webkit-text-size-adjust: none;
	text-size-adjust: none;
}

@media (prefers-color-scheme: light) {
	:root {
		color-scheme: light;
		--background: white;
		color: black;
	}
}

body {
	background-color: var(--background);
	min-block-size: 100dvb;
	margin: 0;
	box-sizing: border-box;
	font-family: system-ui, sans-serif;
	padding: 1rem;
	gap: 1rem;
	display: grid;
	grid-template-areas: "aside main";
}


.info {
	background-color: var(--background);
	flex-basis: 20rem;
	flex-grow: 1;
	box-sizing: border-box;
	z-index: 1;

	grid-area: aside;
	max-inline-size: 20rem;

	> .inner-container {
		background-color: #1E90FF33;
		padding: 1rem;
		box-sizing: border-box;
		min-block-size: 100%;
		border-radius: 1rem;
		display: flex;
		flex-direction: column;
		gap: .8rem;
	}
}

.mobile-warning {
	background-color: #ff224455;
	background-color: color-mix(in srgb, #ff2244 30%, var(--background));
	padding: 1rem;
	border-radius: 1rem;
}

@media (hover: hover) {
	.mobile-warning {
		display: none;
	}
}

main {
	flex-grow: 10000;
	flex-basis: auto;
	grid-area: main;
	display: grid;
	place-items: center;

	> .inner-container {
		contain: paint;
	}
}

.canvas-container {
	background-color: var(--background);
	inline-size: 100%;
	z-index: 1;

	display: flex;
	justify-content: center;
	position: relative;

	.canvas {
		display: grid;
		grid-template-columns: repeat(var(--columns), 1fr);
		inline-size: fit-content;
		border: var(--canvas-border-width) solid #8884;
		margin-block: var(--canvas-margin-block);
	}
}

@media (max-width: 70rem) {
	body {
		grid-template-areas: "aside" "main";
	}

	.info {
		max-inline-size: revert;
	}
}

.pixel {
	inline-size: var(--pixel-size);
	block-size: var(--pixel-size);
	$animation-string: "";

	@for $index from 0 to $colors {
		--play-state-color-#{$index}: paused;
		@if ($index >= 1) {
			$animation-string: $animation-string + ", ";
		}
		$animation-string: $animation-string + "0ms linear 1ms forwards var(--play-state-color-#{$index}) color-#{$index}";

		@keyframes color-#{$index} {
			to {
				border-radius: 50%;
				background-color: var(--color-#{$index});
				box-shadow: 0 0 0 2px var(--color-#{$index});
			}
		}

		input#color-#{$index}[type=radio]:checked ~ .canvas-container .canvas:active &:hover {
			--play-state-color-#{$index}: running;
		}
	}
	animation: #{$animation-string};
}

input[type=radio][name="color"] {
	position: absolute;
	opacity: 0;
	pointer-events: none;
}

@for $index from 0 to $colors {
	.color-chooser > label[for="color-#{$index}"] {
		background-image: #{"-moz-element(#color-#{$index}-style)"};
		background-image: #{"element(#color-#{$index}-style)"};
		--color: var(--color-#{$index});
	}

	input[type=radio][name="color"]#color-#{$index}:checked ~ .color-chooser > label[for="color-#{$index}"] {
		box-shadow: inset 0 0 0 3px var(--color);

		&::before {

			box-shadow: inset 0 0 0 1rem var(--color);
		}
	}
	
	input[type=radio][name="color"]#color-#{$index}:focus-visible ~ .color-chooser > label[for="color-#{$index}"] {
		outline: 2px dotted #fffa;
		outline-offset: -7px;
	}
}

.color-chooser, .color-settings-container {
	--height: var(--colors-line-height);
}

.color-chooser, .color-settings {
	display: flex;
	gap: 1rem;
}

.color-settings-container .inner-style-container, .color-chooser > label {
	font-size: var(--colors-font-size);
	overflow: hidden;
	block-size: var(--height);
	line-height: var(--colors-line-height);
	border-radius: .3rem;
}

.color-chooser {
	z-index: 1;
	position: relative;

	> label {
		display: flex;
		align-items: center;
		border-radius: .6rem;
		padding: .7rem;
		box-sizing: border-box;
		inline-size: 15rem;
		box-shadow: inset 0 0 0 0px var(--color);
		transition: box-shadow .3s;
		background-repeat: no-repeat;
		background-position-x: var(--edit-colors-margin-left);

		&::before {
			content: "";
			display: block;
			border-radius: 50%;
			block-size: 100%;
			aspect-ratio: 1;
			box-sizing: border-box;
			box-shadow: inset 0 0 0 3px var(--color);
			transition: box-shadow .3s;
		}
	}
}

.edit-colors-heading {
	block-size: var(--edit-colors-heading-height);
	background-color: var(--background);
	padding-inline-start: var(--edit-colors-margin-left);
	z-index: 1;
	position: relative;
	display: flex;
	flex-direction: column;
	justify-content: center;
}

.color-settings-container {
	counter-reset: color-index;

	.color-settings {
		--distance: calc((var(--canvas-margin-block) + var(--canvas-border-width)) * 2 + var(--edit-colors-heading-height) + var(--rows) * var(--pixel-size));

		.outer-style-container {
			box-sizing: border-box;
			position: relative;
			margin-inline-start: var(--edit-colors-margin-left);
			inline-size: calc(15rem - var(--edit-colors-margin-left));

			outline: 2px solid #8883;
			outline-offset: -2px;
			border-radius: .3rem;

			&:focus-within {
				background-color: #8883;
			}

			&::before {
				content: counter(color-index) ".";
				counter-increment: color-index;
				position: absolute;
				inset-block: 0;
				inset-inline-start: calc(-1 * var(--edit-colors-margin-left));
				inline-size: var(--edit-colors-margin-left);
				font-size: var(--colors-font-size);
				line-height: var(--colors-line-height);
				text-align: end;
				padding-inline-end: .2em;
				box-sizing: border-box;
				opacity: .6;
			}
		}

		.middle-style-container {

			@supports #{"not ((background: element(#a)) or (background: -moz-element(#a)))"} {
				-webkit-box-reflect: above calc(var(--distance) + var(--height));

				.inner-style-container {
					-webkit-box-reflect: above;
				}
			}
		}

		.inner-style-container {
			> style {
				box-sizing: border-box;
				margin-block-start: calc(var(--colors-line-height) * -1);
				display: block;
				white-space: pre;
				overflow: hidden;
				padding-inline: .3em;
	
				&:focus {
					outline: none;
				}
			}
		}
	}
}

              
            
!

JS

              
                
              
            
!
999px

Console