Code library

Free snippets & functions

Copy, adapt and use them in your own projects.

CSS · Gradient textBeginner
.gradient-text {
  background: linear-gradient(90deg, #2563eb, #db2777);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  font-weight: 800;
}

Fill the letters with a gradient.

.hero {
  min-height: 60vh;
  display: grid;
  place-items: center;
  padding: 2rem;
  text-align: center;
  color: #fff;
  background:
    linear-gradient(rgb(0 0 0 / 0.55), rgb(0 0 0 / 0.55)),
    url("hero.jpg") center / cover no-repeat;
}

A dark gradient on top keeps the text readable.

CSS · Round avatarBeginner
.avatar {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px #e5e7eb;
}

A square photo cropped into a circle.

CSS · Responsive imagesBeginner
img {
  max-width: 100%;
  height: auto;
}

.thumb {
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
  border-radius: 10px;
}

Never wider than the box, never stretched.

CSS · Your first media queryBeginner
.title { font-size: 2.5rem; }
.menu  { display: flex; gap: 1rem; }

@media (max-width: 640px) {
  .title { font-size: 1.75rem; }
  .menu  { flex-direction: column; gap: 0.5rem; }
}

Change the design on small screens.

CSS · Sidebar and content layoutBeginner
.layout {
  display: grid;
  grid-template-columns: 260px 1fr;
  gap: 2rem;
}

@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; }
}

A fixed sidebar and flexible content that stacks on phones.

CSS · A three-column gridBeginner
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1.5rem;
}

@media (max-width: 800px) { .grid { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 520px) { .grid { grid-template-columns: 1fr; } }

fr units share the free space equally.

CSS · Wrapping flex itemsBeginner
.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}
.tags li {
  list-style: none;
  padding: 0.25rem 0.7rem;
  border-radius: 999px;
  background: #eef2ff;
}

Items go to the next line when there is not enough space.

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1rem;
}
.header nav { display: flex; gap: 1rem; }

A one-line header layout.

CSS · Flexbox row with gapBeginner
.row {
  display: flex;
  align-items: center;
  gap: 1rem;
}
.row .grow { flex: 1; }

Items next to each other, spaced evenly.

CSS · A clean tableBeginner
table {
  width: 100%;
  border-collapse: collapse;
}
th, td {
  padding: 0.65rem 0.8rem;
  text-align: left;
  border-bottom: 1px solid #e5e7eb;
}
thead th { background: #f9fafb; font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.04em; }
tbody tr:nth-child(even) { background: #fafafa; }
tbody tr:hover { background: #eff6ff; }

Collapsed borders, zebra rows and a hover highlight.

CSS · Color a checkbox in one lineBeginner
input[type="checkbox"], input[type="radio"] {
  accent-color: #2563eb;
  width: 1.1rem;
  height: 1.1rem;
}

label.check {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
}

accent-color changes the native control color.

CSS · A simple form layoutBeginner
.form { display: grid; gap: 1rem; max-width: 420px; }
.field { display: grid; gap: 0.35rem; }
.field label { font-weight: 600; font-size: 0.9rem; }
.field .hint { font-size: 0.8rem; color: #6b7280; }
.field input:invalid:not(:placeholder-shown) { border-color: #ef4444; }

Label above field, even spacing.

CSS · Styled form fieldsBeginner
input[type="text"], input[type="email"], input[type="password"], select, textarea {
  width: 100%;
  padding: 0.65rem 0.8rem;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  background: #fff;
  color: #111827;
  font: inherit;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: #2563eb;
  box-shadow: 0 0 0 3px rgb(37 99 235 / 0.2);
}
input::placeholder, textarea::placeholder { color: #9ca3af; }

Large enough to tap, with a clear focus ring.

CSS · Alert boxesBeginner
.alert {
  padding: 0.85rem 1rem;
  border-left: 4px solid #3b82f6;
  border-radius: 6px;
  background: #eff6ff;
  color: #1e3a8a;
}
.alert.success { border-color: #22c55e; background: #f0fdf4; color: #14532d; }
.alert.error   { border-color: #ef4444; background: #fef2f2; color: #7f1d1d; }

Info, success and error messages with a colored edge.

CSS · Badges and tagsBeginner
.badge {
  display: inline-block;
  padding: 0.15em 0.65em;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  line-height: 1.6;
  background: #e5e7eb;
  color: #374151;
}
.badge.ok     { background: #dcfce7; color: #166534; }
.badge.warn   { background: #fef9c3; color: #854d0e; }
.badge.danger { background: #fee2e2; color: #991b1b; }

Small rounded labels for status.

CSS · Card that lifts on hoverBeginner
.card {
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.card:hover {
  transform: translateY(-4px);
  box-shadow: 0 12px 24px rgb(0 0 0 / 0.12);
}

@media (prefers-reduced-motion: reduce) {
  .card { transition: none; }
  .card:hover { transform: none; }
}

Move up a little and grow the shadow.

CSS · A simple cardBeginner
.card {
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  padding: 1.25rem;
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.05), 0 4px 12px rgb(0 0 0 / 0.06);
}
.card h3 { margin: 0 0 0.5rem; }
.card p  { margin: 0; color: #4b5563; }

The most common component on the web.

CSS · Nice link stylesBeginner
a {
  color: #2563eb;
  text-decoration-line: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
  text-decoration-color: rgb(37 99 235 / 0.4);
}
a:hover { text-decoration-color: currentColor; }
a:focus-visible { outline: 2px solid currentColor; outline-offset: 3px; border-radius: 2px; }

Keep links obvious, but make the underline lighter.

CSS · Outline and ghost buttonsBeginner
.btn-outline {
  padding: 0.55rem 1rem;
  border: 2px solid #2563eb;
  border-radius: 8px;
  background: transparent;
  color: #2563eb;
  font-weight: 600;
  cursor: pointer;
}
.btn-outline:hover { background: #2563eb; color: #fff; }

.btn-ghost {
  padding: 0.55rem 1rem;
  border: 0;
  background: transparent;
  color: #374151;
  cursor: pointer;
}
.btn-ghost:hover { background: #f3f4f6; }

Secondary actions should look quieter than the main button.

CSS · A good-looking buttonBeginner
.btn {
  display: inline-block;
  padding: 0.6rem 1.1rem;
  border: 0;
  border-radius: 8px;
  background: #2563eb;
  color: #fff;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  transition: background 0.15s, transform 0.05s;
}
.btn:hover { background: #1d4ed8; }
.btn:active { transform: translateY(1px); }
.btn:focus-visible { outline: 3px solid #93c5fd; outline-offset: 2px; }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; }

Hover, focus and a pressed state are what make a button feel real.

CSS · A spacing scaleBeginner
:root {
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 1rem;
  --space-4: 1.5rem;
  --space-5: 2rem;
  --space-6: 3rem;
}

.card { padding: var(--space-4); }
.stack > * + * { margin-top: var(--space-3); }

Pick from a few sizes instead of random numbers. The page looks consistent.

CSS · Center anything with gridBeginner
.center-box {
  display: grid;
  place-items: center;
  min-height: 100vh;
}

Two lines to center both ways.

.container {
  width: min(100% - 2rem, 1100px);
  margin-inline: auto;
}

Give the element a width, then let the side margins share the rest equally.

.article {
  max-width: 68ch;
  margin-inline: auto;
  padding: 1rem;
  line-height: 1.7;
  font-size: 1.0625rem;
}

.article p + p { margin-top: 1em; }
.article h2 { margin-top: 2em; line-height: 1.25; }

Lines of 60-75 characters are the easiest to read.

CSS · A system font stackBeginner
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 1rem;
  line-height: 1.6;
}

code, pre {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

Fast, no download, and looks native on every device.

CSS · Colors with CSS variablesBeginner
:root {
  --brand: #2563eb;
  --brand-dark: #1e40af;
  --text: #1f2937;
  --muted: #6b7280;
  --bg: #ffffff;
}

body { color: var(--text); background: var(--bg); }
a { color: var(--brand); }
a:hover { color: var(--brand-dark); }
.muted { color: var(--muted); }

Define your colors once and use them everywhere.

CSS · A small modern CSS resetBeginner
*, *::before, *::after { box-sizing: border-box; }
* { margin: 0; }

html { -webkit-text-size-adjust: 100%; }

body {
  min-height: 100vh;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

img, picture, video, canvas, svg { display: block; max-width: 100%; }
input, button, textarea, select { font: inherit; }
p, h1, h2, h3, h4 { overflow-wrap: break-word; }

Removes surprises from browser defaults and makes images behave.

CSS · Box-sizing resetBeginner
*, *::before, *::after {
  box-sizing: border-box;
}

body {
  margin: 0;
}

Makes width include padding and border. Put it at the top of every stylesheet.

AJAX · Why AJAX is asynchronousBeginner
console.log('1. before');

fetch('/api/items').then(function () {
  console.log('3. reply arrived');
});

console.log('2. after (runs before the reply!)');

Code after fetch runs before the reply comes back. Use then or await to wait.