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>Creating new Selectors with <code>:has()</code>:<br /> The <code>:nth-in-island-of-class</code>, and <code>:nth-last-in-island-of-class</code> selector generator</h1>

<h2>What?</h2>
<p>This page is a demo for <a href="https://brm.us/css-islands" target="_top">https://brm.us/css-islands</a>. Please read the post to get the full details and explanation.</p>

<h2>Selector Generator</h2>

<p>I want to target the <select name="n" id="n"><option value="1">1st</option><option value="2">2nd</option><option value="3" selected>3rd</option><option value="4">4th</option><option value="5">5th</option><option value="6">6th</option><option value="7">7th</option><option value="8">8th</option><option value="9">9th</option><option value="10">10th</option></select> <select name="type" id="type"><option value="nth">:nth-in-island-of-class</option><option value="nth-last">:nth-last-in-island-of-class</option></select> with selector <input type="text" name="selector" id="selector" value=".special"/></p>

<pre><code id="output">[loading …]</code></pre>

<h3>Demo</h3>
<div class="no-support" data-support="css-has-basic"><p>🚨 Your browser does not support CSS <code>:has()</code>, so this demo will not work correctly.</p></div>
<div class="no-support" data-support="css-has-relative"><p>🚨 Your browser does not support relative selectors in CSS <code>:has()</code>, so this demo will not work correctly.</p></div>

<p><em>TIP: You can click each paragraph to toggle the <code>.special</code> class</em></p>

<div class="demo">
	<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Eaque in, enim rem recusandae mollitia error, natus ab atque dignissimos culpa veniam exercitationem sequi voluptates, commodi corrupti ullam sit. Debitis, a!</p>
	<p class="special">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Eaque in, enim rem recusandae mollitia error, natus ab atque dignissimos culpa veniam exercitationem sequi voluptates, commodi corrupti ullam sit. Debitis, a!</p>
	<p class="special">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Eaque in, enim rem recusandae mollitia error, natus ab atque dignissimos culpa veniam exercitationem sequi voluptates, commodi corrupti ullam sit. Debitis, a!</p>
	<p class="special">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Eaque in, enim rem recusandae mollitia error, natus ab atque dignissimos culpa veniam exercitationem sequi voluptates, commodi corrupti ullam sit. Debitis, a!</p>
	<p class="special">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Eaque in, enim rem recusandae mollitia error, natus ab atque dignissimos culpa veniam exercitationem sequi voluptates, commodi corrupti ullam sit. Debitis, a!</p>
	<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Eaque in, enim rem recusandae mollitia error, natus ab atque dignissimos culpa veniam exercitationem sequi voluptates, commodi corrupti ullam sit. Debitis, a!</p>
	<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Eaque in, enim rem recusandae mollitia error, natus ab atque dignissimos culpa veniam exercitationem sequi voluptates, commodi corrupti ullam sit. Debitis, a!</p>
	<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Eaque in, enim rem recusandae mollitia error, natus ab atque dignissimos culpa veniam exercitationem sequi voluptates, commodi corrupti ullam sit. Debitis, a!</p>
	<p class="special">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Eaque in, enim rem recusandae mollitia error, natus ab atque dignissimos culpa veniam exercitationem sequi voluptates, commodi corrupti ullam sit. Debitis, a!</p>
	<p class="special">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Eaque in, enim rem recusandae mollitia error, natus ab atque dignissimos culpa veniam exercitationem sequi voluptates, commodi corrupti ullam sit. Debitis, a!</p>
	<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Eaque in, enim rem recusandae mollitia error, natus ab atque dignissimos culpa veniam exercitationem sequi voluptates, commodi corrupti ullam sit. Debitis, a!</p>
	<p class="special">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Eaque in, enim rem recusandae mollitia error, natus ab atque dignissimos culpa veniam exercitationem sequi voluptates, commodi corrupti ullam sit. Debitis, a!</p>
	<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Eaque in, enim rem recusandae mollitia error, natus ab atque dignissimos culpa veniam exercitationem sequi voluptates, commodi corrupti ullam sit. Debitis, a!</p>
	<p class="special">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Eaque in, enim rem recusandae mollitia error, natus ab atque dignissimos culpa veniam exercitationem sequi voluptates, commodi corrupti ullam sit. Debitis, a!</p>
	<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Eaque in, enim rem recusandae mollitia error, natus ab atque dignissimos culpa veniam exercitationem sequi voluptates, commodi corrupti ullam sit. Debitis, a!</p>
	<p class="special">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Eaque in, enim rem recusandae mollitia error, natus ab atque dignissimos culpa veniam exercitationem sequi voluptates, commodi corrupti ullam sit. Debitis, a!</p>
	<p class="special">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Eaque in, enim rem recusandae mollitia error, natus ab atque dignissimos culpa veniam exercitationem sequi voluptates, commodi corrupti ullam sit. Debitis, a!</p>
	<p class="special">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Eaque in, enim rem recusandae mollitia error, natus ab atque dignissimos culpa veniam exercitationem sequi voluptates, commodi corrupti ullam sit. Debitis, a!</p>
	<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Eaque in, enim rem recusandae mollitia error, natus ab atque dignissimos culpa veniam exercitationem sequi voluptates, commodi corrupti ullam sit. Debitis, a!</p>
	<p class="special">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Eaque in, enim rem recusandae mollitia error, natus ab atque dignissimos culpa veniam exercitationem sequi voluptates, commodi corrupti ullam sit. Debitis, a!</p>
	<p class="special">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Eaque in, enim rem recusandae mollitia error, natus ab atque dignissimos culpa veniam exercitationem sequi voluptates, commodi corrupti ullam sit. Debitis, a!</p>
</div>
              
            
!

CSS

              
                :where(.demo > *) {
	margin: 0;
	padding: 0.5em 1em;
	border: 1px solid transparent; /* We set a transparent border on all sides to prevent a layout jump when actually painting the border */
	cursor: pointer;
	transition: all 0.5s ease-in-out;
}

/* Basic .special styling */
.special {
	background: rgb(0 0 0 / 0.1);
	border-left: 1px solid currentcolor;
}

/* Non-demo styles below */
@layer base {
	@layer layout {
		html {
			max-width: 84ch;
			padding: 3rem 2rem;
			margin: auto;
			line-height: 1.5;
			font-size: 1.25rem;
		}
		body {
			margin: 0;
			padding: 0;
		}
		a,
		a:visited {
			color: blue;
		}
		
		h2 {
			margin-top: 2em;
		}
		
		h1 {
			display: none;
		}
	}
	
	@layer code {
		pre {
			border: 1px solid #dedede;
			padding: 1em;
			background: #f7f7f7;
			font-family: "Courier 10 Pitch", Courier, monospace;
			overflow-x: auto;
			border-left: 0.4em solid cornflowerblue;
			tab-size: 4;
		}
		
		code:not(pre code) {
			background: #f7f7f7;
			border: 1px solid rgb(0 0 0 / 0.2);
		}
	}

	@layer support {
		.no-support,
		.has-support {
			margin: 1em 0;
			padding: 1em;
			border: 1px solid #ccc;
		}

		.no-support {
			background-color: #ff00002b;
			display: block;
		}
		.has-support {
			background-color: #00ff002b;
			display: none;
		}
		:is(.has-support, .no-support) > :first-child {
			margin-top: 0;
		}
		:is(.has-support, .no-support) > :last-child {
			margin-bottom: 0;
		}

		@supports(selector(:has(*))) {
			.no-support[data-support="css-has-basic"] {
				display: none;
			}
			.has-support[data-support="css-has-basic"] {
				display: block;
			}
		}

		@supports(selector(:has(+ *))) {
			.no-support[data-support="css-has-relative"] {
				display: none;
			}
			.has-support[data-support="css-has-relative"] {
				display: block;
			}
		}

		@supports(selector(:nth-child(1 of .foo))) {
			.no-support[data-support="css-nth-child-of-s"] {
				display: none;
			}
			.has-support[data-support="css-nth-child-of-s"] {
				display: block;
			}
		}
	}
}

@layer reset {
	
}
              
            
!

JS

              
                document.querySelectorAll('.demo p').forEach($p => {
	$p.addEventListener('click', (e) => {
		e.target.classList.toggle('special');
	});
});

const createDynamicStyleElement = (id) => {
    var style = document.createElement("style");
    style.type = "text/css";
    style.id = id;
    return style;
}

const generateSelector = (n, type, selector) => {
	
	n = parseInt(n);
	if (n === 0) {
		throw new Error('Invalid value for n');
	}
	
	let suffix = '';
	
	switch (type) {
		case `nth`:
			return `${selector}:not(${selector} + ${selector}) ${`+ ${selector} `.repeat(n-1).trim()}`;
			break;
			
		case 'nth-last':
			
			if (n > 1) {
				suffix = `:has(${`+ ${selector} `.repeat(n-1).trim()})`;
			}		
			
			return `.special:not(:has(${`+ ${selector} `.repeat(n).trim()}))${suffix}`
			
			break;
			
		default:
			throw new Error('Invalid type');
	}
}

const updateStyles = (selector) => {
	let $style = document.querySelector('#dynamic-styles');
	if (!$style) {
		$style = createDynamicStyleElement('dynamic-styles');
		document.head.appendChild($style);
	}
	
	const styleSheet = Array.from(document.styleSheets).filter(s => s.ownerNode == $style)[0];
	
	if (styleSheet.cssRules.length) {
		styleSheet.deleteRule(0);
	}
	
	styleSheet.insertRule(`${selector} { color: red; }`, 0);
}

const updateOutput = (selector) => {
	document.querySelector('#output').innerText = `${selector} {
    …
}`;
}

const update = () => {
	const selector = generateSelector(
		document.getElementById('n').value,
		document.getElementById('type').value,
		document.getElementById('selector').value,
	);
	updateStyles(selector);
	updateOutput(selector);
}

document.querySelectorAll('select, input').forEach($el => {
	$el.addEventListener('change', (e) => {
		update();
	});
});

update();
              
            
!
999px

Console