<script>
document.documentElement.dataset.embed = window.location.search.includes('type=embed') ? "yep" : "nope";
</script>

<main>
	<h1>Styling <code>&lt;details&gt;</code>: UIKit Accordion</h1>

	<p class="warning">Your browser does not support <code>::details-content</code>, so this demo won’t work as intended. Please use a browser with support – such as Chrome 131 or newer – to check out this page.</p>

	<h2>Demo</h2>
	
	<div id="demo">
		<details name="accordion" open>
			<summary>Item 1</summary>
			<div class="details-content-wrapper">
				<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Dolor, aliquam. Veritatis consequatur, soluta molestiae voluptates accusamus qui odio, error, repellat rem harum id similique omnis quod dignissimos saepe quas mollitia.</p>
				<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Dolor, aliquam. Veritatis consequatur, soluta molestiae voluptates accusamus qui odio, error, repellat rem harum id similique omnis quod dignissimos saepe quas mollitia.</p>
			</div>
		</details>
		<details name="accordion">
			<summary>Item 2</summary>
			<div class="details-content-wrapper">
				<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Dolor, aliquam. Veritatis consequatur, soluta molestiae voluptates accusamus qui odio, error, repellat rem harum id similique omnis quod dignissimos saepe quas mollitia.</p>
			</div>
		</details>
		<details name="accordion">
			<summary>Item 3</summary>
			<div class="details-content-wrapper">
				<p>Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
				<p>Dolor, aliquam. Veritatis consequatur, soluta molestiae voluptates accusamus qui odio, error, repellat rem harum id similique omnis quod dignissimos saepe quas mollitia.</p>
				<p>Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
			</div>
		</details>
	</div>

	<p>Demo for <a href="https://developer.chrome.com/blog/styling-details" target="_top">https://developer.chrome.com/blog/styling-details</a>, built after <a href="https://getuikit.com/docs/accordion" target="_top">https://getuikit.com/docs/accordion</a></p>

	<h2>The Code</h2>
	
	<h3>UI</h3>
	
	<pre><code>/* Nothing out of the ordinary to report */</code></pre>
	
	<h3>Animation</h3>
	<pre><code>::details-content {
	display: block; /* Override UA */

	transition: all 0.5s ease, content-visibility 0.5s <strong>step-end</strong> allow-discrete;
	height: 0;
	overflow: clip; /* Clip off contents while animating */
}

@supports (interpolate-size: allow-keywords) {
	[open]::details-content {
		transition: all 0.5s ease, content-visibility 0.5s step-start allow-discrete;
		height: auto;
		padding-bottom: 0.5em;
	}
}
@supports not (interpolate-size: allow-keywords) {
	[open]::details-content {
		transition: all 0.5s ease, content-visibility 0.5s step-start allow-discrete;
		height: 150px; /* Arbitrarily chosen height */
		overflow-y: scroll; /* In case the contents should be taller than 150px */
	}
}

.details-content-wrapper {
	padding: 1rem 0.5rem;
  margin-trim: block;
}</code></pre>
</main>
@supports selector(::details-content) {
	.warning {
		display: none;
	}
}

details {
	background: white;
}

summary {
	font-size: 1.5rem;
	color: #333;
	cursor: pointer;
	height: 3rem;
	padding: 0 0.5rem;
	
	/* Roll our own marker */
	display: flex;
	justify-content: space-between;

	&::marker {
		content: "";
	}
	&::after {
		content: "+";
	}
	[open] &::after {
		content: "-";
	}

	&:hover {
		color: #666;
	}
}

.details-content-wrapper {
	padding: 1rem 0.5rem;

	/* We need margin-trim … */
	:first-child { margin-top: 0; }
	:last-child {	margin-bottom: 0;	}
}

::details-content {
	transition: all 0.5s ease, content-visibility 0.5s ease allow-discrete; /* Rely on the special animation powers of content-visibility */
	height: 0;
	overflow: clip; /* Clip off contents while animating */
}

@supports (interpolate-size: allow-keywords) {
	:root {
		interpolate-size: allow-keywords;
	}

	[open]::details-content {
		height: auto;
		padding-bottom: 0.5em;
	}
}
@supports not (interpolate-size: allow-keywords) {
	[open]::details-content {
		height: 150px; /* Arbitrarily chosen height */
		overflow-y: scroll; /* In case the contents should be taller than 150px */
	}
}

@layer reset {
	* {
		margin: 0;
		padding: 0;
	}

	html,
	body {
		height: 100%;
	}
	
	html {
		font-size: 14px;
	}
}

@layer baselayout {
	html {
		color: #444;
		font-family: system-ui;
		line-height: 1.42;
		
		overscroll-behavior-x: none;
		overflow: auto;
	}

	main {
		max-width: 90ch;
		margin: 0 auto;
		padding-bottom: 10rem;
	}

	p {
		margin-bottom: 1em;
	}
	
	h1, h2 {
		margin: 4em 0 1em;
	}
	h3 {
		margin: 1em 0 0.5em;
	}
	
	#demo {
		padding: 1em;
		border: 1px solid #ccc;
		background: #f4f6f9;
	}
}

@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);
		padding: 0.1rem 0.3rem;
		margin: 0.1rem 0;
		border-radius: 0.2rem;
		/* 			display: inline-block; */
		-webkit-box-decoration-break: clone;
		white-space: pre-wrap;
	}
}

@layer warning {
	.warning {
		box-sizing: border-box;
		padding: 1em;
		margin: 1em 0;
		border: 1px solid #ccc;
		background: rgba(255 255 205 / 0.8);
	}

	.warning > :first-child {
		margin-top: 0;
	}

	.warning > :last-child {
		margin-bottom: 0;
	}

	.warning a {
		color: blue;
	}
	.warning--info {
		border: 1px solid #123456;
		background: rgb(205 230 255 / 0.8);
	}
	.warning--alarm {
		border: 1px solid red;
		background: #ff000010;
	}
}

/* Hide a bunch of stuff when embedded */
:root[data-embed="yep"] {
	main {
		padding: 2em 1em;
		
		:not(p.warning, p.warning *, #demo, #demo *, #demo ~ p, #demo ~ p *) {
			display: none;
		}
	}
	#demo ~ p {
		margin-top: 1em;
		text-align: center;
	}
}

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.