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

              
                <h1><code>@bramus/style-observer</code> demo</h1>

<h2>Instructions</h2>

<p>Click anywhere on this document to change the <code>background-color</code>. In response, a toast with the new computed value pops-up.</p>

<h2>More info</h2>
<p>This page is a demo for <a href="https://github.com/bramus/style-observer/" target="_top"><code>@bramus/style-observer</code></a>, a <code>MutationObserver</code> for CSS Computed Styles.<br>Go read <a href="https://brm.us/style-observer" target="_top">https://brm.us/style-observer</a> for an introduction and usage instructions.</p>
              
            
!

CSS

              
                @import url(https://esm.sh/notyf/notyf.min.css);

body {
	background: lightpink;
	user-select: none;
}

:where(body > *:not(script, .notyf, .notyf-announcer)) {
	background: white;
	display: block;
	padding: 0.2em;
	width: fit-content;
}
pre {
	border: 2px solid lightblue;
	border-left-width: 10px;
	padding: 0.5em;
	font-weight: 700;
	overflow-x: auto;
	max-width: 95vw;
	tab-size: 2;
	background: white;
}

p {
	line-height: 1.34;
}

a {
	color: blue;
}
              
            
!

JS

              
                import CSSStyleObserver from "https://esm.sh/@bramus/style-observer@1";
import { Notyf } from "https://esm.sh/notyf";

// Create an instance of Notyf
const notyf = new Notyf({
	ripple: false,
});

// Utility method to show a notification using notyf
const showNotification = (message) => {
	notyf.success(message);
};

// Utility method that generates a random (hex) color
const randomColor = () => '#' + (Math.random() * 0xFFFFFF << 0).toString(16).padStart(6, '0');

// Array with the names of the properties to observe.
// This can be one or more, both regular properties and custom properties
const properties = ['background-color'];

// Create a CSSStyleObserver that tracks the properties.
// Every time one of those properties their computed value changes, the passed in callback gets executed
// Here, the callback shows the new computed value in a notification
const cssStyleObserver = new CSSStyleObserver(properties, (values) => {
  showNotification(`Background color changed to ${values['background-color']}`);
});

// Have the CSSStyleObserver instance observe the `<body>` element
cssStyleObserver.attach(document.body);

// Change the background-color every time you click the document
document.documentElement.addEventListener('click', (e) => {
	document.body.style.setProperty('background-color', randomColor());
});
              
            
!
999px

Console