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 blocks = ["air", "stone", "grass", "dirt", "log", "leaves"];
- const layers = 24;
- const columns = 48;
<html lang="en">
	<meta charset="UTF-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1" />
	<meta name="description" content="A 2D Minecraft clone made with pure HTML &amp; CSS &ndash; no JavaScript." />

	<title>2D CSS Minecraft</title>

	<link rel="icon" href="./assets/icon.png" />

	<link rel="stylesheet" href="./main.css" />

	<style>
		-
			const sourceMap = {
				version: 3,
				sources: ["./index.pug"],
				mappings: "",
			};
		| /*# sourceMappingURL=data:application/json;charset=utf-8,#{encodeURIComponent(JSON.stringify(sourceMap))} */
	</style>

	<section class="info">
		<div class="text">
			<h1>2D CSS Minecraft</h1>
			<p>
				| <strong>There is no JavaScript on this page.</strong> All the logic is made 100% with pure HTML & CSS. <br />
				| View on <a href="https://github.com/BenjaminAster/2D-CSS-Minecraft">GitHub</a>, <a href="https://codepen.io/Benjamin_Aster/pen/QWrbLLj">CodePen</a>, <a href="https://benjaminaster.com/2d-css-minecraft/">benjaminaster.com</a>
			</p>
			<p class="not-supported">
				| ⚠︎ Your browser does not support the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:has">CSS <code>:has()</code> pseudo-class</a>, which is needed for this site to work.
				| If you use Firefox, this is not yet implemented, so you have to either wait or try it out in a Chromium- or WebKit-based browser like Google Chrome, Microsoft Edge or Apple Safari.
				| If you don't use Firefox, you are probably using an outdated version of your browser. Please update it: Chromium version ≥ 105 or Safari version ≥ 15.4 is required.
			</p>
		</div>
		<label class="close" title="close">
			<input type="checkbox" />
		</label>
	</section>

	<section class="controls">
		<div class="block-chooser">
			each block in blocks
				<label class="#{block}" title="#{block}">
					<input type="radio" name="block-type" !{block === "stone" ? "checked" : ""} />
				</label>
			//- /each
		</div>

		<div class="move-buttons">
			<button class="up" title="move up">↑</button>
			<button class="down" title="move down">↓</button>
			<button class="left" title="move left">←</button>
			<button class="right" title="move right">→</button>
		</div>
	</section>

	<main class="content">
		<div class="up">
			<div class="down">
				<div class="left">
					<div class="right">
						<div class="blocks">
							for _, layer in Array(layers)
								<div class="layer">
									for _, column in Array(columns)
										<div class="block">
											each block in blocks
												<label class="#{block}">
													<input type="radio" name="block-l#{layer}-c#{column}" !{block === (layer < layers - 5 ? "air" : (layer > layers - 5 ? "dirt" : "grass")) ? "checked" : ""} />
												</label>
											//- /each
										</div>
									//- /for
								</div>
							//- /for
						</div>
					</div>
				</div>
			</div>
		</div>
	</main>
</html>
              
            
!

CSS

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

$blocks: (
	"air",
	"stone",
	"grass",
	"dirt",
	"log",
	"leaves",
);

:root {
	color-scheme: dark;
	--block-size: 3rem;

	--air-image: none;
	--stone-image: url("https://benjaminaster.com/2d-css-minecraft/assets/stone.png");
	--grass-image: url("https://benjaminaster.com/2d-css-minecraft/assets/grass_side_carried.png");
	--dirt-image: url("https://benjaminaster.com/2d-css-minecraft/assets/dirt.png");
	--log-image: url("https://benjaminaster.com/2d-css-minecraft/assets/log_oak.png");
	--leaves-image: url("https://benjaminaster.com/2d-css-minecraft/assets/azalea_leaves.png");
}

body {
	color: white;
	background-color: lightSkyBlue;
	block-size: 100vh;
	block-size: 100dvb;
	margin: 0;
	box-sizing: border-box;
	font-family: system-ui, sans-serif;
	overflow: hidden;

	display: grid;
	place-content: end center;
}

label, button {
	cursor: pointer;
}

h1, p {
	margin: 0;
}

h1, strong {
	font-weight: 600;
}

a {
	color: inherit;
}

.info {
	z-index: 1;
	position: absolute;
	inset-block-start: 0;
	inset-inline: 0;
	padding: .75rem;
	margin-inline: auto;
	inline-size: fit-content;
	max-inline-size: min(60rem, 100vw - 10rem);
	background-color: #1117;
	--backdrop-filter: blur(.4rem);
	-webkit-backdrop-filter: var(--backdrop-filter);
	backdrop-filter: var(--backdrop-filter);
	border-radius: .6rem;
	border-start-start-radius: 0;
	border-start-end-radius: 0;
	text-align: center;
	box-shadow: 0 0 1.5rem #0006;
	display: flex;
	align-items: center;
	gap: 1rem;

	& strong {
		color: gold;
	}

	& .text {
		display: flex;
		flex-direction: column;
		gap: .3rem;
	}

	& .close {
		padding-inline: .3rem;

		&::before {
			content: "⨉";
			font-size: 1.5rem;
		}

		& > input[type=checkbox] {
			position: absolute;
			opacity: 0;
			pointer-events: none;
		}
	}

	&:has(.close > input[type=checkbox]:checked) {
		display: none;
	}

	& .not-supported {
		border: 2px solid red;
		background-color: #f004;
		border-radius: .7rem;
		padding: .6rem .4rem;
	}

	@supports selector(:has(a)) {
		& .not-supported {
			display: none;
		}
	}
}

.controls {
	z-index: 1;
	position: absolute;
	display: flex;
	flex-wrap: wrap;
	justify-content: center;
	align-items: center;
	gap: 1.25rem;
	border-radius: 1rem;
	box-shadow: 0 0 1.5rem #0006;
	background-color: #2228;
	--backdrop-filter: blur(.3rem);
	-webkit-backdrop-filter: var(--backdrop-filter);
	backdrop-filter: var(--backdrop-filter);
	padding: .75rem;
	margin-block-end: .75rem;
	inset-block-end: 0;
	inset-inline: 0;
	margin-inline: auto;
	inline-size: fit-content;
	max-inline-size: calc(100vw - 4rem);

	& .block-chooser {
		display: flex;
		align-items: center;
		gap: .5rem;

		& > label {
			display: block;
			inline-size: 3rem;
			block-size: 3rem;
			border-radius: .3rem;
			box-sizing: border-box;
			outline-offset: -1px;
			background-size: 100% 100%;
			background-repeat: no-repeat;
			image-rendering: pixelated;

			&.air {
				background-color: lightSkyBlue;
			}

			@each $block in $blocks {
				&.#{$block} {
					background-image: var(--#{$block}-image);
				}
			}

			&:hover {
				outline: 3px solid silver;
			}

			&:has(> input[type=radio]:checked) {
				outline: 3px solid white;
			}

			& > input[type=radio] {
				position: absolute;
				opacity: 0;
				pointer-events: none;
			}
		}
	}

	& .move-buttons {
		display: flex;
		align-items: center;
		gap: .5rem;

		& > button {
			display: block;
			inline-size: 3rem;
			block-size: 3rem;
			border: none;
			border-radius: .3rem;
			background-color: #0003;
			font-size: 1.2rem;

			&:hover {
				background-color: #0005;
			}

			&:active {
				background-color: #0007;
			}
		}
	}

	@each $block, $_ in $blocks {
		&:has(> .block-chooser > .#{$block} > input[type=radio]:checked) ~ .content .block > label:not(.#{$block}) {
			display: none;
		}
	}

	&:has(> .move-buttons > .up:active) ~ .content .down {
		animation-play-state: running;
	}
	&:has(> .move-buttons > .down:active) ~ .content .up {
		animation-play-state: running;
	}
	&:has(> .move-buttons > .left:active) ~ .content .right {
		animation-play-state: running;
	}
	&:has(> .move-buttons > .right:active) ~ .content .left {
		animation-play-state: running;
	}
}

.content {
	inline-size: fit-content;

	--animation-duration: 10000s;
	--max-translation: 300000rem;

	& .down {
		animation: var(--animation-duration) linear paused move-down;
		@keyframes move-down {
			from {
				translate: 0 0;
			}
			to {
				translate: 0 calc(var(--max-translation));
			}
		}
	}
	& .up {
		animation: var(--animation-duration) linear paused move-up;
		@keyframes move-up {
			from {
				translate: 0 0;
			}
			to {
				translate: 0 calc(-1 * var(--max-translation));
			}
		}
	}
	& .left {
		animation: var(--animation-duration) linear paused move-left;
		@keyframes move-left {
			from {
				translate: 0 0;
			}
			to {
				translate: calc(-1 * var(--max-translation)) 0;
			}
		}
	}
	& .right {
		animation: var(--animation-duration) linear paused move-right;
		@keyframes move-right {
			from {
				translate: 0 0;
			}
			to {
				translate: calc(var(--max-translation)) 0;
			}
		}
	}

	& .blocks {
		display: flex;
		flex-direction: column;
		gap: 1px;
		padding: 1px;
		background-color: gray;

		& .layer {
			display: flex;
			gap: 1px;
		}

		& .block {
			inline-size: var(--block-size);
			block-size: var(--block-size);
			position: relative;
			background-color: lightSkyBlue;
			background-size: 100% 100%;
			background-repeat: no-repeat;
			image-rendering: pixelated;

			& > label {
				display: block;
				block-size: 100%;
				inline-size: 100%;
				box-sizing: border-box;

				&:hover {
					border: 3px solid white;
				}
		
				& > input[type=radio] {
					position: absolute;
					opacity: 0;
					pointer-events: none;
				}
			}

			@each $block in $blocks {
				&:has(> label.#{$block} > input[type=radio]:checked) {
					background-image: var(--#{$block}-image);
				}
			}
		}
	}
}


              
            
!

JS

              
                
              
            
!
999px

Console