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

              
                <div class="support">
	<p>This demo uses anchor positioning which is not widely supported outside of Chromium based browsers. <a href="https://caniuse.com/?search=anchor-">Check out browser compatability here</a></p>
</div>

<div class="mouse-cursor"></div>
<div class="mouse-click-point"></div>
<div class="rectangle"></div>
              
            
!

CSS

              
                body {
	/* 	This bit is the mouse cursor. From another demo of mine, but using it here too to give a couple of circles where the mouse cursor is. Getting the position of the mouse from mousemove event in JS and two custom properties */
	background-image: radial-gradient(
			16px at calc(var(--mouse-x) * 100vw) calc(var(--mouse-y) * 100vh),
			rgba(0, 0, 0, 0) 17px,
			rgba(0, 0, 0, 1) 18px,
			rgba(0, 0, 0, 0) 19px
		),
		radial-gradient(
			16px at calc(var(--mouse-x) * 100vw) calc(var(--mouse-y) * 100vh),
			rgba(0, 0, 0, 0) 11px,
			rgba(0, 0, 0, 1) 12px,
			rgba(0, 0, 0, 0) 13px
		);
	/* end of mouse cursor */

	height: 100vh;
	width: 100vw;
	container-type: size;

	position: relative;
}

/* This is where the current mouse position is. Needed to be able to draw the rectangle from the current mouse position to where the user clicked last */
.mouse-cursor {
	--background: rgb(from purple r g b / 0.75);
	--is: --start;
	--dimension: 15px;

	border-radius: 50%;

	translate: 0% 0%;

	left: max(
		calc(var(--_dimension) / -2),
		calc(calc(var(--mouse-x, 0) * 100cqi) - calc(var(--_dimension) / 2))
	);
	top: max(
		calc(var(--_dimension) / -2),
		calc(calc(var(--mouse-y, 0) * 100cqb) - calc(var(--_dimension) / 2))
	);
}

.mouse-click-point {
	--background: blue;
	--is: --end;

	top: calc(var(--mouse-click-y, 0.5) * 100cqb);
	left: calc(calc(var(--mouse-click-x, 0.5) * 100cqi));
}

[class^="mouse"] {
	position: absolute;
	--_dimension: var(--dimension, 30px);
	aspect-ratio: 1;
	height: var(--_dimension);
	anchor-name: var(--is);
	background-color: var(--background);
}

.rectangle {
	/* 	Adding onto the idea by Kizu here: https://kizu.dev/anchor-positioning-experiments/ */
	--_from: var(--from, --start);
	--_to: var(--to, --end);

	top: min(anchor(var(--_from) center), anchor(var(--_to) center));
	left: min(anchor(var(--_from) center), anchor(var(--_to) center));
	right: min(anchor(var(--_from) center), anchor(var(--_to) center));
	bottom: min(anchor(var(--_from) center), anchor(var(--_to) center));

	background: oklab(from hotpink l a calc(b * 3) / 1.75);

	position: absolute;

	/* Technique found here: https://css-irl.info/css-halftone-patterns/	 */
	--dotSize: 0.15rem;
	--bgSize: 0.5rem;
	--bgPosition: calc(var(--bgSize) / 2);

	background: radial-gradient(red, hotpink);

	mask-image: radial-gradient(
			circle at center,
			black var(--dotSize),
			transparent 0
		),
		radial-gradient(circle at center, black var(--dotSize), transparent 0);
	mask-size: var(--bgSize) var(--bgSize);
	mask-position: 0 0, var(--bgPosition) var(--bgPosition);
}

.support {
	max-inline-size: 90%;
	margin-inline: auto;
	margin-top: 2.5cqi;

	border: 3px double red;
	padding: 3rem;

	@supports (anchor-name: --foo) {
		display: none;
	}
}

              
            
!

JS

              
                const root = document.documentElement;

const setVariables = (evt, name) => {
	let x = evt.clientX / innerWidth;
	let y = evt.clientY / innerHeight;

	root.style.setProperty(`--${name}-x`, x);
	root.style.setProperty(`--${name}-y`, y);
};

document.addEventListener("mousemove", (evt) => {
	setVariables(evt, "mouse");
});

document.addEventListener("click", (evt) => {
	setVariables(evt, "mouse-click");
});

              
            
!
999px

Console