Code library
Free snippets & functions
Copy, adapt and use them in your own projects.
h1 { font-size: clamp(1.8rem, 4vw + 1rem, 3.5rem); }
p { font-size: clamp(1rem, 0.9rem + 0.4vw, 1.2rem); }
.section { padding-block: clamp(2rem, 6vw, 5rem); }clamp(min, preferred, max) lets a size grow with the screen but never get too small or too large.
.field:has(input:focus) label { color: #2563eb; }
.field:has(input:invalid:not(:placeholder-shown)) { --ring: #ef4444; }
.card:has(img) { padding: 0; }
.card:has(img) .body { padding: 1rem; }
form:has(:invalid) button[type="submit"] { opacity: 0.5; pointer-events: none; }Change the parent depending on what is inside.
:is(h1, h2, h3, h4) a { text-decoration: none; }
.article :is(ul, ol) { padding-left: 1.25rem; }
:where(.card, .panel) > :where(h2, h3) { margin-top: 0; }Group selectors, and keep specificity at zero with :where().
h1, h2, h3 { text-wrap: balance; }
p { text-wrap: pretty; }Balanced headings and fewer lonely last words.
@media print {
nav, aside, footer, .no-print, button { display: none !important; }
body { background: #fff; color: #000; font-size: 12pt; }
a[href^="http"]::after { content: " (" attr(href) ")"; font-size: 0.85em; }
h1, h2, h3 { break-after: avoid; }
img, table, pre { break-inside: avoid; }
@page { margin: 2cm; }
}Clean pages on paper: hide the menus, show link addresses.
.timeline { list-style: none; margin: 0; padding: 0 0 0 1.5rem; border-left: 2px solid #cbd5e1; }
.timeline li { position: relative; padding-bottom: 1.5rem; }
.timeline li::before {
content: "";
position: absolute;
left: calc(-1.5rem - 7px);
top: 0.35rem;
width: 12px;
height: 12px;
border-radius: 50%;
background: #2563eb;
box-shadow: 0 0 0 3px #fff;
}
.timeline time { font-size: 0.8rem; color: #6b7280; }A line with dots, built with borders and ::before.
.plans { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 1.25rem; align-items: stretch; }
.plan { padding: 1.5rem; border: 1px solid #e5e7eb; border-radius: 16px; background: #fff; }
.plan.featured {
border-color: #2563eb;
box-shadow: 0 12px 30px rgb(37 99 235 / 0.18);
transform: translateY(-8px);
}
.plan .price { font-size: 2.25rem; font-weight: 800; }The recommended plan stands out.
.progress {
height: 10px;
border-radius: 999px;
background: #e5e7eb;
overflow: hidden;
}
.progress > span {
display: block;
width: calc(var(--value, 0) * 1%);
height: 100%;
background: linear-gradient(90deg, #22c55e, #16a34a);
transition: width 0.4s ease;
}
/* <div class="progress" style="--value:65"><span></span></div> */Width comes from a custom property you set in HTML or JavaScript.
.skeleton {
border-radius: 6px;
background: linear-gradient(90deg, #e5e7eb 25%, #f3f4f6 50%, #e5e7eb 75%);
background-size: 200% 100%;
animation: shimmer 1.4s infinite linear;
}
.skeleton.line { height: 1em; margin: 0.5em 0; }
.skeleton.avatar { width: 48px; height: 48px; border-radius: 50%; }
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }Gray blocks with a moving shine while data loads.
.figure { position: relative; overflow: hidden; border-radius: 12px; }
.figure img { width: 100%; display: block; transition: transform 0.4s; }
.figure figcaption {
position: absolute;
inset: auto 0 0 0;
padding: 1rem;
color: #fff;
background: linear-gradient(transparent, rgb(0 0 0 / 0.75));
transform: translateY(100%);
transition: transform 0.3s;
}
.figure:hover figcaption, .figure:focus-within figcaption { transform: none; }
.figure:hover img { transform: scale(1.06); }A figure whose caption slides up.
.nav {
position: sticky;
top: 0;
z-index: 50;
padding: 0.75rem 1rem;
background: rgb(255 255 255 / 0.75);
-webkit-backdrop-filter: saturate(180%) blur(12px);
backdrop-filter: saturate(180%) blur(12px);
border-bottom: 1px solid rgb(0 0 0 / 0.08);
}A translucent bar that blurs the content below.
.modal {
position: fixed;
inset: 0;
display: none;
place-items: center;
background: rgb(0 0 0 / 0.55);
z-index: 100;
}
.modal:target { display: grid; }
.modal .box { width: min(92vw, 480px); padding: 1.5rem; background: #fff; border-radius: 14px; }Open with a link to #id, close with a link to #.
.dropdown { position: relative; display: inline-block; }
.dropdown-menu {
position: absolute;
top: 100%;
left: 0;
min-width: 180px;
padding: 0.4rem;
background: #fff;
border: 1px solid #e5e7eb;
border-radius: 10px;
box-shadow: 0 10px 24px rgb(0 0 0 / 0.12);
opacity: 0;
visibility: hidden;
transform: translateY(6px);
transition: opacity 0.15s, transform 0.15s, visibility 0.15s;
}
.dropdown:hover .dropdown-menu,
.dropdown:focus-within .dropdown-menu { opacity: 1; visibility: visible; transform: none; }
.dropdown-menu a { display: block; padding: 0.5rem 0.75rem; border-radius: 6px; }
.dropdown-menu a:hover { background: #f3f4f6; }Opens on hover and also when a link inside has keyboard focus.
[data-tip] { position: relative; }
[data-tip]::after {
content: attr(data-tip);
position: absolute;
left: 50%;
bottom: calc(100% + 8px);
transform: translateX(-50%);
padding: 0.35rem 0.6rem;
border-radius: 6px;
background: #111827;
color: #fff;
font-size: 0.8rem;
white-space: nowrap;
opacity: 0;
pointer-events: none;
transition: opacity 0.15s;
}
[data-tip]:hover::after, [data-tip]:focus-visible::after { opacity: 1; }Text from a data attribute, shown on hover and focus.
details { border: 1px solid #e5e7eb; border-radius: 8px; padding: 0 1rem; }
details + details { margin-top: 0.5rem; }
summary { padding: 0.9rem 0; cursor: pointer; font-weight: 600; list-style: none; display: flex; justify-content: space-between; }
summary::-webkit-details-marker { display: none; }
summary::after { content: "+"; font-size: 1.25rem; }
details[open] summary::after { content: "\2212"; }
details[open] { background: #f9fafb; }
details > :not(summary) { padding-bottom: 1rem; }Native open and close, styled with CSS.
.switch { position: relative; display: inline-block; width: 46px; height: 26px; }
.switch input { position: absolute; opacity: 0; inset: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch .track { position: absolute; inset: 0; background: #cbd5e1; border-radius: 999px; transition: background 0.2s; pointer-events: none; }
.switch .track::after {
content: "";
position: absolute;
top: 3px; left: 3px;
width: 20px; height: 20px;
border-radius: 50%;
background: #fff;
transition: transform 0.2s;
}
.switch input:checked + .track { background: #2563eb; }
.switch input:checked + .track::after { transform: translateX(20px); }
.switch input:focus-visible + .track { outline: 3px solid #93c5fd; outline-offset: 2px; }Pure CSS, still keyboard accessible.
.steps { counter-reset: step; list-style: none; padding: 0; }
.steps li { counter-increment: step; position: relative; padding-left: 3rem; margin-bottom: 1.25rem; }
.steps li::before {
content: counter(step);
position: absolute;
left: 0;
top: 0;
width: 2rem;
height: 2rem;
border-radius: 50%;
background: #2563eb;
color: #fff;
display: grid;
place-items: center;
font-weight: 700;
}Automatic numbers in nice circles.
blockquote { position: relative; padding: 1rem 1rem 1rem 3rem; font-style: italic; }
blockquote::before {
content: "\201C";
position: absolute;
left: 0.5rem;
top: -0.25rem;
font-size: 3.5rem;
color: #93c5fd;
line-height: 1;
}
.external::after { content: " \2197"; font-size: 0.8em; }Add visuals without extra HTML.
html { scroll-behavior: smooth; }
:target, h2[id], h3[id] { scroll-margin-top: 5rem; }
@media (prefers-reduced-motion: reduce) {
html { scroll-behavior: auto; }
}Anchors stop below a sticky header.
.snap {
display: flex;
gap: 1rem;
overflow-x: auto;
scroll-snap-type: x mandatory;
scroll-padding: 1rem;
}
.snap > * {
flex: 0 0 85%;
scroll-snap-align: start;
border-radius: 12px;
}
@media (min-width: 700px) { .snap > * { flex-basis: 40%; } }A swipe carousel without JavaScript.
.scroll {
overflow: auto;
scrollbar-width: thin;
scrollbar-color: #94a3b8 transparent;
}
.scroll::-webkit-scrollbar { width: 8px; height: 8px; }
.scroll::-webkit-scrollbar-thumb { background: #94a3b8; border-radius: 8px; }
.scroll::-webkit-scrollbar-track { background: transparent; }Thin scrollbar that works in modern browsers.
.sr-only {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
overflow: hidden;
clip: rect(0 0 0 0);
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}Hidden on screen, still read aloud.
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
scroll-behavior: auto !important;
}
}Switch off animations for people who asked for less movement.
:focus { outline: none; }
:focus-visible {
outline: 3px solid #2563eb;
outline-offset: 2px;
border-radius: 4px;
}
.skip-link {
position: absolute;
left: 1rem;
top: -4rem;
padding: 0.5rem 1rem;
background: #111827;
color: #fff;
}
.skip-link:focus { top: 1rem; }Show the ring for keyboard users, not for mouse clicks.
:root, [data-theme="light"] {
--bg: #ffffff; --text: #111827; --accent: #2563eb;
}
[data-theme="dark"] {
--bg: #0b1220; --text: #e5e7eb; --accent: #60a5fa;
}
body { background: var(--bg); color: var(--text); transition: background 0.2s, color 0.2s; }
a { color: var(--accent); }JavaScript sets data-theme on html, CSS does the rest.
:root {
color-scheme: light dark;
--bg: #ffffff;
--text: #111827;
--card: #f3f4f6;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #0f172a;
--text: #e5e7eb;
--card: #1e293b;
}
}
body { background: var(--bg); color: var(--text); }
.card { background: var(--card); }Swap the variables, and tell the browser about it with color-scheme.
@media (max-width: 640px) {
table, thead, tbody, tr, th, td { display: block; }
thead { position: absolute; left: -9999px; }
tr { margin-bottom: 1rem; border: 1px solid #e5e7eb; border-radius: 8px; padding: 0.5rem; }
td { display: flex; justify-content: space-between; gap: 1rem; border: 0; padding: 0.35rem 0.5rem; }
td::before { content: attr(data-label); font-weight: 600; color: #6b7280; }
}Each cell shows its label from a data attribute.
.table-wrap { max-height: 360px; overflow: auto; border: 1px solid #e5e7eb; border-radius: 8px; }
.table-wrap table { border-collapse: separate; border-spacing: 0; width: 100%; }
.table-wrap thead th {
position: sticky;
top: 0;
z-index: 1;
background: #f3f4f6;
box-shadow: inset 0 -1px 0 #e5e7eb;
}The header row stays visible in a scrolling box.
.layout { display: grid; grid-template-columns: 1fr 300px; gap: 2rem; align-items: start; }
.sidebar { position: sticky; top: 1rem; max-height: calc(100vh - 2rem); overflow: auto; }Stays in view while the content scrolls.
.video {
aspect-ratio: 16 / 9;
width: 100%;
border-radius: 12px;
overflow: hidden;
background: #000;
}
.video iframe, .video video { width: 100%; height: 100%; border: 0; }Keeps the shape at any width. No padding hack needed.