Code library

Free snippets & functions

Copy, adapt and use them in your own projects.

Advanced 36 snippets

@view-transition { navigation: auto; }

::view-transition-old(root) { animation: fade-out 0.2s ease both; }
::view-transition-new(root) { animation: fade-in 0.3s ease both; }

@keyframes fade-out { to { opacity: 0; } }
@keyframes fade-in  { from { opacity: 0; } }

.hero-img { view-transition-name: hero; }

Smooth cross-fade between two pages of the same site.

CSS · Typing effectAdvanced
.typing {
  width: 22ch;
  overflow: hidden;
  white-space: nowrap;
  border-right: 3px solid currentColor;
  font-family: ui-monospace, Menlo, Consolas, monospace;
  animation: typing 3s steps(22) 1s both, caret 0.8s step-end infinite;
}
@keyframes typing { from { width: 0; } }
@keyframes caret  { 50% { border-color: transparent; } }

One line typed out with steps().

CSS · Infinite marqueeAdvanced
.marquee { overflow: hidden; display: flex; }
.marquee-track {
  display: flex;
  gap: 3rem;
  flex: none;
  min-width: 100%;
  animation: marquee 25s linear infinite;
}
.marquee:hover .marquee-track { animation-play-state: paused; }

@keyframes marquee { to { transform: translateX(-100%); } }

A looping logo strip. The content is duplicated once in HTML.

CSS · Staggered entrance animationAdvanced
@keyframes rise {
  from { opacity: 0; transform: translateY(16px); }
  to   { opacity: 1; transform: none; }
}

.list > li {
  opacity: 0;
  animation: rise 0.5s ease forwards;
  animation-delay: calc(var(--i, 0) * 80ms);
}

@media (prefers-reduced-motion: reduce) {
  .list > li { animation: none; opacity: 1; }
}

Each item appears slightly after the previous one.

CSS · 3D flip cardAdvanced
.flip { perspective: 900px; width: 260px; height: 160px; }
.flip-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transition: transform 0.6s;
  transform-style: preserve-3d;
}
.flip:hover .flip-inner, .flip:focus-within .flip-inner { transform: rotateY(180deg); }
.flip .front, .flip .back {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border-radius: 14px;
  backface-visibility: hidden;
}
.flip .front { background: #2563eb; color: #fff; }
.flip .back  { background: #111827; color: #fff; transform: rotateY(180deg); }

Flips on hover or keyboard focus.

.duotone {
  position: relative;
  background: #1e3a8a;
  isolation: isolate;
}
.duotone img {
  display: block;
  width: 100%;
  filter: grayscale(1) contrast(1.1);
  mix-blend-mode: screen;
}
.duotone::after {
  content: "";
  position: absolute;
  inset: 0;
  background: #f472b6;
  mix-blend-mode: multiply;
  pointer-events: none;
}

Turn any photo into a two-color image.

CSS · Soft shadow cardAdvanced
.soft {
  background: #e6e9ef;
  border-radius: 20px;
  box-shadow: 8px 8px 16px #c4c8d0, -8px -8px 16px #ffffff;
}
.soft:active, .soft.pressed {
  box-shadow: inset 6px 6px 12px #c4c8d0, inset -6px -6px 12px #ffffff;
}

Two shadows give the soft, raised look.

CSS · Glass effectAdvanced
.glass {
  background: rgb(255 255 255 / 0.18);
  -webkit-backdrop-filter: blur(14px) saturate(160%);
  backdrop-filter: blur(14px) saturate(160%);
  border: 1px solid rgb(255 255 255 / 0.35);
  border-radius: 18px;
  box-shadow: 0 8px 32px rgb(0 0 0 / 0.2);
  color: #fff;
}

Frosted panel over a colorful background.

CSS · Fade out edges with maskAdvanced
.fade-scroll {
  max-height: 260px;
  overflow: auto;
  -webkit-mask-image: linear-gradient(to bottom, transparent, #000 1.5rem, #000 calc(100% - 1.5rem), transparent);
  mask-image: linear-gradient(to bottom, transparent, #000 1.5rem, #000 calc(100% - 1.5rem), transparent);
}

Scrolling lists that fade at the top and bottom.

CSS · Shapes with clip-pathAdvanced
.hero-diagonal { clip-path: polygon(0 0, 100% 0, 100% 85%, 0 100%); }
.cut-corner { clip-path: polygon(0 0, calc(100% - 20px) 0, 100% 20px, 100% 100%, 0 100%); }
.circle { clip-path: circle(50%); }
.hexagon { clip-path: polygon(25% 5%, 75% 5%, 100% 50%, 75% 95%, 25% 95%, 0 50%); }

Diagonal edges and cut corners.

CSS · Stacking sticky cardsAdvanced
.stack-card {
  position: sticky;
  top: calc(1rem + var(--i, 0) * 1.5rem);
  min-height: 60vh;
  padding: 2rem;
  border-radius: 20px;
  background: var(--bg, #fff);
  box-shadow: 0 -8px 24px rgb(0 0 0 / 0.08);
}
/* <article class="stack-card" style="--i:0">...</article>
   <article class="stack-card" style="--i:1">...</article> */

Each card sticks a little lower, so they pile up as you scroll.

CSS · Masonry-style columnsAdvanced
.masonry { columns: 3 240px; column-gap: 1rem; }
.masonry > * { break-inside: avoid; margin-bottom: 1rem; display: block; width: 100%; border-radius: 10px; }

Pure CSS columns, no JavaScript.

@property --angle {
  syntax: "<angle>";
  inherits: false;
  initial-value: 0deg;
}

.spin-border {
  border: 3px solid transparent;
  border-radius: 14px;
  background:
    linear-gradient(#fff, #fff) padding-box,
    conic-gradient(from var(--angle), #2563eb, #db2777, #f59e0b, #2563eb) border-box;
  animation: rotate 4s linear infinite;
}
@keyframes rotate { to { --angle: 360deg; } }

Registered variables can be animated.

CSS · light-dark() for themesAdvanced
:root { color-scheme: light dark; }

body {
  background: light-dark(#ffffff, #0f172a);
  color: light-dark(#111827, #e5e7eb);
}
.card { background: light-dark(#f3f4f6, #1e293b); border-color: light-dark(#e5e7eb, #334155); }

One value for each theme in one line.

:root {
  --brand: oklch(55% 0.2 260);
  --brand-hover: color-mix(in oklch, var(--brand) 85%, black);
  --brand-soft: color-mix(in oklch, var(--brand) 12%, white);
  --brand-border: color-mix(in oklch, var(--brand) 35%, white);
}
.btn { background: var(--brand); color: white; }
.btn:hover { background: var(--brand-hover); }
.chip { background: var(--brand-soft); border: 1px solid var(--brand-border); }

Hover and soft colors made from one brand color.

.box {
  margin-inline: auto;
  padding-block: 1rem;
  padding-inline: 1.5rem;
  border-inline-start: 4px solid #2563eb;
  inline-size: min(100%, 60ch);
  text-align: start;
}
.icon { margin-inline-end: 0.5rem; }

Works for left-to-right and right-to-left pages.

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 1.5rem;
}
.cards .card {
  display: grid;
  grid-row: span 3;
  grid-template-rows: subgrid;
  gap: 0.5rem;
}

Titles, text and buttons line up across all cards.

CSS · Native CSS nestingAdvanced
.card {
  padding: 1rem;
  border-radius: 12px;
  background: #fff;

  & h3 { margin: 0 0 0.5rem; }
  & p { color: #4b5563; }
  &:hover { box-shadow: 0 8px 20px rgb(0 0 0 / 0.1); }
  &.featured { border: 2px solid #2563eb; }

  @media (max-width: 600px) {
    padding: 0.75rem;
  }
}

Write related rules together without Sass.

CSS · Cascade layersAdvanced
@layer reset, base, components, utilities;

@layer reset {
  * { box-sizing: border-box; margin: 0; }
}
@layer base {
  body { font: 16px/1.6 system-ui, sans-serif; }
  a { color: #2563eb; }
}
@layer components {
  .btn { padding: 0.6rem 1rem; background: #2563eb; color: #fff; border-radius: 8px; }
}
@layer utilities {
  .hidden { display: none; }
  .mt-4 { margin-top: 1rem; }
}

Decide which group of styles wins, whatever the selector strength.

CSS · Container query unitsAdvanced
.widget { container-type: inline-size; }
.widget h3 { font-size: clamp(1rem, 6cqi, 2rem); }
.widget .pad { padding: clamp(0.5rem, 4cqi, 1.5rem); }

Size things relative to the container, with clamp().

.card-wrap { container: card / inline-size; }

.card { display: grid; gap: 0.75rem; }
.card img { width: 100%; aspect-ratio: 16 / 9; object-fit: cover; }

@container card (min-width: 480px) {
  .card { grid-template-columns: 180px 1fr; align-items: start; }
  .card img { aspect-ratio: 1; }
}

The card changes layout based on its own width, not the screen width.