Code library

Free snippets & functions

Copy, adapt and use them in your own projects.

Beginner 38 snippets

CSS · Sticky headerBeginner
header {
  position: sticky;
  top: 0;
  z-index: 50;
  background: #fff;
  box-shadow: 0 1px 0 rgba(0, 0, 0, .08);
}

/* Anchors land below the header */
[id] { scroll-margin-top: 80px; }
html { scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }

The header stays at the top while scrolling. Anchor links need scroll-margin so headings do not hide under it.

CSS · Centre anything with FlexboxBeginner
.center {
  display: flex;
  justify-content: center;   /* horizontal */
  align-items: center;       /* vertical */
  min-height: 100vh;
}

/* Even shorter, with Grid */
.center-grid { display: grid; place-items: center; min-height: 100vh; }

The shortest way to centre content both ways.

CSS · A loading spinnerBeginner
.spinner {
  width: 32px;
  height: 32px;
  border: 4px solid #e5e7eb;
  border-top-color: #2563eb;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

A circle with one colored edge, rotating.

.box {
  background: #e0e7ff;
  transition: background-color 0.25s ease, transform 0.25s ease;
}
.box:hover {
  background: #c7d2fe;
  transform: scale(1.03);
}

Add transition to the normal state, not only to :hover.

CSS · Position a badge on a cornerBeginner
.icon-wrap { position: relative; display: inline-block; }

.icon-wrap .count {
  position: absolute;
  top: -6px;
  right: -8px;
  min-width: 18px;
  padding: 0 5px;
  border-radius: 999px;
  background: #ef4444;
  color: #fff;
  font-size: 0.7rem;
  line-height: 18px;
  text-align: center;
}

The parent is relative, the child is absolute.

CSS · Cut long text with ...Beginner
.one-line {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.three-lines {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
}

One line or a fixed number of lines.

li:first-child { border-top-left-radius: 8px; border-top-right-radius: 8px; }
li:last-child  { border-bottom: 0; }
li:nth-child(odd)  { background: #f9fafb; }
li:nth-child(3n)   { font-weight: 700; }
li:not(:last-child) { border-bottom: 1px solid #e5e7eb; }

Select items by position without extra classes.

CSS · Animated underline on hoverBeginner
.nav-link {
  position: relative;
  text-decoration: none;
  color: inherit;
}
.nav-link::after {
  content: "";
  position: absolute;
  left: 0;
  bottom: -3px;
  width: 100%;
  height: 2px;
  background: currentColor;
  transform: scaleX(0);
  transform-origin: left;
  transition: transform 0.25s ease;
}
.nav-link:hover::after, .nav-link:focus-visible::after { transform: scaleX(1); }

Grows from left to right using a pseudo-element.

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
main   { flex: 1; }
footer { padding: 1rem; background: #111827; color: #d1d5db; }

Short pages still push the footer to the bottom of the window.

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.