<main>
<section>
<h1>CSS calc-size() function</h1>
<p>
Animate accordion height with new calc-size function.
</p>
</section>
<div class="disclaimer">
<div class="disclaimer-header">
<span class="alert-icon">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126ZM12 15.75h.007v.008H12v-.008Z" />
</svg>
</span>
<h3>Disclaimer</h3>
<button type="button" class="close-icon">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6M6 6l12 12" />
</svg>
</button>
</div>
<p>
Currently, <span>calc-size()</span> is only supported in Chrome browsers. To see this demo in action, update Chrome to the latest stable release (129). See <a href="https://caniuse.com/?search=calc-size">caniuse</a> for an overview of browser support.
</p>
</div>
<ul class="accordion-group">
<!-- Accordion Item -->
<li class="accordion-item">
<!-- Button Trigger -->
<button type="button" class="accordion-trigger">
<span>Label 1</span>
<span class="accordion-icon">
<svg xmlns="http://www.w3.org/2000/svg" class="plus" viewBox="0 0 160 160" fill="currentColor">
<rect class="vertical-cross" x="70" rx="8" width="15" height="100%" />
<rect y="70" rx="8" width="100%" height="15" />
</svg>
</span>
</button>
<!-- Content Container -->
<div class="accordion-content">
<p class="contents">
Cray man braid literally vegan, snackwave polaroid post-ironic. Letterpress fit godard skateboard, street art. Tote bag neutra plaid. Edison bulb tattooed fashion axe.
</p>
</div>
</li>
<!-- Accordion Item -->
<li class="accordion-item">
<!-- Button Trigger -->
<button type="button" class="accordion-trigger">
<span>Label 2</span>
<span class="accordion-icon">
<svg xmlns="http://www.w3.org/2000/svg" class="plus" viewBox="0 0 160 160" fill="currentColor">
<rect class="vertical-cross" x="70" rx="8" width="15" height="100%" />
<rect y="70" rx="8" width="100%" height="15" />
</svg>
</span>
</button>
<!-- Content Container -->
<div class="accordion-content">
<p class="contents">
Cray man braid literally vegan, snackwave polaroid post-ironic. Letterpress fit godard skateboard, street art. Tote bag neutra plaid. Edison bulb tattooed fashion axe.
</p>
</div>
</li>
<!-- Accordion Item -->
<li class="accordion-item">
<!-- Button Trigger -->
<button type="button" class="accordion-trigger">
<span>Label 3</span>
<span class="accordion-icon">
<svg xmlns="http://www.w3.org/2000/svg" class="plus" viewBox="0 0 160 160" fill="currentColor">
<rect class="vertical-cross" x="70" rx="8" width="15" height="100%" />
<rect y="70" rx="8" width="100%" height="15" />
</svg>
</span>
</button>
<!-- Content Container -->
<div class="accordion-content">
<p class="contents">
Cray man braid literally vegan, snackwave polaroid post-ironic. Letterpress fit godard skateboard, street art. Tote bag neutra plaid. Edison bulb tattooed fashion axe.
</p>
</div>
</li>
<!-- Accordion Item -->
<li class="accordion-item">
<!-- Button Trigger -->
<button type="button" class="accordion-trigger">
<span>Label 4</span>
<span class="accordion-icon">
<svg xmlns="http://www.w3.org/2000/svg" class="plus" viewBox="0 0 160 160" fill="currentColor">
<rect class="vertical-cross" x="70" rx="8" width="15" height="100%" />
<rect y="70" rx="8" width="100%" height="15" />
</svg>
</span>
</button>
<!-- Content Container -->
<div class="accordion-content">
<p class="contents">
Cray man braid literally vegan, snackwave polaroid post-ironic. Letterpress fit godard skateboard, street art. Tote bag neutra plaid. Edison bulb tattooed fashion axe.
</p>
</div>
</li>
<!-- Accordion Item -->
<li class="accordion-item">
<!-- Button Trigger -->
<button type="button" class="accordion-trigger">
<span>Label 5</span>
<span class="accordion-icon">
<svg xmlns="http://www.w3.org/2000/svg" class="plus" viewBox="0 0 160 160" fill="currentColor">
<rect class="vertical-cross" x="70" rx="8" width="15" height="100%" />
<rect y="70" rx="8" width="100%" height="15" />
</svg>
</span>
</button>
<!-- Content Container -->
<div class="accordion-content">
<p class="contents">
Cray man braid literally vegan, snackwave polaroid post-ironic. Letterpress fit godard skateboard, street art. Tote bag neutra plaid. Edison bulb tattooed fashion axe.
</p>
</div>
</li>
</ul>
</main>
@import url("https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&display=swap");
:root {
--text-black: #09080a;
--text-gray: #3b4b68;
--highlight: #f1f2f4;
--disclaimer: #dc2626;
// --background-discalimer:
}
@layer reset {
* {
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: "Inter", sans-serif;
}
body {
color: var(--text-black);
}
ul,
li {
list-style: none;
}
button {
border: none;
background: none;
appearance: none;
cursor: pointer;
white-space: nowrap;
}
a {
text-decoration: none;
}
}
main {
width: 100%;
padding-inline: 1rem;
padding-bottom: 2rem;
}
section {
max-width: 64ch;
gap: 0.5rem 0;
margin-block: 3rem;
p {
color: var(--text-gray);
}
}
main,
section {
display: flex;
flex-direction: column;
align-items: center;
}
h1 {
font-size: 2rem;
letter-spacing: 1px;
}
p {
line-height: 1.5rem;
}
.disclaimer,
.accordion-group {
max-width: 420px;
width: 100%;
}
.disclaimer {
color: var(--text-gray);
border: 2px solid var(--disclaimer);
border-radius: 0.5rem;
margin-bottom: 1rem;
padding: 0.75rem;
display: none;
&-header {
display: flex;
align-items: center;
gap: 0 0.5rem;
margin-bottom: 0.5rem;
}
h3,
.alert-icon {
color: var(--disclaimer);
}
.close-icon {
margin-left: auto;
}
.alert-icon,
.close-icon {
display: block;
width: 24px;
height: 24px;
}
p > span {
font-weight: 600;
}
@supports not (height: calc-size(auto, size)) {
display: block;
}
}
.accordion-group {
color: var(--text-gray);
}
.accordion-item + .accordion-item {
border-top: 2px solid var(--highlight);
}
.accordion-trigger {
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
padding-inline: 0.75rem;
padding-block: 1rem;
border-radius: 0.5rem;
font-size: 1.2rem;
font-weight: 600;
color: inherit;
&:hover {
background: var(--highlight);
}
span {
display: block;
flex-shrink: 0;
}
.accordion-icon {
width: 18px;
height: 18px;
}
svg {
width: 100%;
height: auto;
}
.vertical-cross {
transition: transform 750ms;
transform: scaleY(1);
transform-origin: 50% 50%;
[data-open] & {
transform: scaleY(0);
}
}
}
.accordion-content {
height: 0;
transition-property: height;
transition-duration: 750ms;
overflow: hidden;
.contents {
padding-inline: 0.75rem;
padding-top: 0.25rem;
padding-bottom: 1rem;
}
[data-open] & {
height: auto; // fallback if no support for calc-size
height: calc-size(auto, size);
}
}
View Compiled
const accordionTrigger = document.querySelectorAll(".accordion-trigger");
const accordionItem = document.querySelectorAll(".accordion-item");
const closeDisclaimer = document.querySelector(".close-icon");
function toggleAccordion(event) {
currentTarget = event.target;
const currentParent = currentTarget.closest(".accordion-item");
const isOpen = currentParent.hasAttribute("data-open");
for (let item of accordionItem) {
item.removeAttribute("data-open");
}
if (!isOpen) {
currentParent.setAttribute("data-open", "");
}
}
function handleClose() {
const disclaimer = document.querySelector(".disclaimer");
disclaimer.style.display = "none";
}
for (let trigger of accordionTrigger) {
trigger.addEventListener("click", toggleAccordion);
}
closeDisclaimer.addEventListener("click", handleClose);
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.