DEV Community

Alex Chen
Alex Chen

Posted on

CSS Tricks That Save Me Hours Every Week

CSS Tricks That Save Me Hours Every Week

These aren't the flashy animations you see on CodePen. These are practical tricks I use in real projects.

1. Truncate Text with Ellipsis

/* Single line */
.truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Multi-line (works everywhere now!) */
.line-clamp-3 {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* Use case: Product descriptions, news previews, card titles */
.product-title {
  font-size: 1.25rem;
  font-weight: 600;
  line-height: 1.4;
  max-height: 3.5em; /* ~2 lines */
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
}
Enter fullscreen mode Exit fullscreen mode

2. Modern Centering

/* ❌ Old way */
.center-old {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

/* ✅ Flexbox (most common) */
.center-flex {
  display: flex;
  justify-content: center; /* horizontal */
  align-items: center;     /* vertical */
  min-height: 100vh;
}

/* ✅ Grid (cleanest syntax) */
.center-grid {
  display: grid;
  place-items: center;
  min-height: 100vh;
}

/* ✅ Flexbox margin trick (for single item) */
.parent {
  display: flex;
}
.child {
  margin: auto; /* Centers in both directions! */
}
Enter fullscreen mode Exit fullscreen mode

3. Sticky Header/Footer

/* No JavaScript needed! */
.sticky-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: white; /* Prevents content showing through */
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.sticky-footer {
  position: sticky;
  bottom: 0;
  background: white;
  padding: 1rem;
  border-top: 1px solid #eee;
}

/* Sidebar that stays visible while scrolling */
.sidebar {
  position: sticky;
  top: 80px; /* Below sticky header */
  height: calc(100vh - 80px);
  overflow-y: auto;
}
Enter fullscreen mode Exit fullscreen mode

4. Responsive Typography

/* Instead of media queries for every breakpoint: */
html {
  font-size: clamp(14px, 2vw + 10px, 20px);
}

/* Scales between 14px (small screens) and 20px (large screens) */
/* Automatically responsive — no breakpoints needed! */

/* For headings with even more control: */
h1 {
  font-size: clamp(1.5rem, 5vw + 1rem, 4rem);
  line-height: 1.1;
}

p {
  font-size: clamp(1rem, 1.5vw + 0.5rem, 1.125rem);
  max-width: 70ch; /* Optimal reading length (~70 characters) */
}
Enter fullscreen mode Exit fullscreen mode

5. Custom Scrollbar

/* Make scrollbars match your design */
.custom-scrollbar::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

.custom-scrollbar::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 4px;
}

.custom-scrollbar::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 4px;
}

.custom-scrollbar::-webkit-scrollbar-thumb:hover {
  background: #a1a1a1;
}

/* Firefox */
.custom-scrollbar {
  scrollbar-width: thin;
  scrollbar-color: #c1c1c1 #f1f1f1;
}
Enter fullscreen mode Exit fullscreen mode

6. Smooth Scrolling & Scroll Snapping

/* Smooth scrolling for anchor links */
html {
  scroll-behavior: smooth;
}

/* Snap to sections (like a presentation) */
.snap-container {
  scroll-snap-type: y mandatory;
  overflow-y: scroll;
  height: 100vh;
}

.snap-section {
  scroll-snap-align: start;
  height: 100vh;
}

/* Card carousel snapping */
.card-carousel {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  gap: 1rem;
  padding: 1rem 0;
}

.card-carousel > * {
  scroll-snap-align: center;
  flex-shrink: 0;
  width: 300px;
}
Enter fullscreen mode Exit fullscreen mode

7. Selection & Focus Styles

/* Brand your text selection */
::selection {
  background: #3b82f6; /* Your brand color */
  color: white;
}

/* Better focus styles for accessibility */
:focus-visible {
  outline: 2px solid #3b82f6;
  outline-offset: 2px;
}

/* Remove default focus for mouse users, keep for keyboard */
:focus:not(:focus-visible) {
  outline: none;
}
Enter fullscreen mode Exit fullscreen mode

8. Container Queries (Game Changer!)

/* Media queries respond to VIEWPORT size */
/* Container queries respond to PARENT size! */

/* Define container */
.card-container {
  container-type: inline-size;
  container-name: card;
}

/* Child responds to parent's width, not viewport! */
@container card (min-width: 400px) {
  .card-inner {
    display: grid;
    grid-template-columns: 200px 1fr;
    gap: 1rem;
  }
}

@container card (max-width: 399px) {
  .card-inner {
    display: flex;
    flex-direction: column;
  }
}

/* This means reusable components that adapt to their container! */
/* No more wrapper classes like .card-large, .card-small */
Enter fullscreen mode Exit fullscreen mode

9. Aspect Ratio Made Easy

/* Before: padding-bottom hack (ugly) */
.aspect-old {
  position: relative;
  padding-bottom: 56.25%; /* 16:9 = 9/16 * 100% */
}
.aspect-old > * {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
}

/* After: one property! */
.aspect-modern {
  aspect-ratio: 16 / 9;
}

/* Common ratios */
.video { aspect-ratio: 16 / 9; }
.square { aspect-ratio: 1 / 1; }
.photo { aspect-ratio: 4 / 3; }
.story { aspect-ratio: 9 / 16; } /* Instagram story */
Enter fullscreen mode Exit fullscreen mode

10. The :has() Selector (CSS "Parent Select")

/* Finally select a parent based on its children! */

/* Card has an image → use grid layout */
.card:has(.card-image) {
  display: grid;
  grid-template-columns: 200px 1fr;
}

/* Card has no image → simple block layout */
.card:not(:has(.card-image)) {
  padding: 1rem;
}

/* Form field has error → red border */
.form-field:has(.error-message) {
  border-color: #ef4444;
}

/* Navigation has dropdown → show arrow */
.nav-item:has(.dropdown)::after {
  content: '▼';
  margin-left: 4px;
  font-size: 0.75rem;
}

/* Empty state styling */
.list-container:has(:empty)::after {
  content: 'No items yet';
  display: block;
  text-align: center;
  color: #999;
  padding: 2rem;
}
Enter fullscreen mode Exit fullscreen mode

Quick Reference Card

Trick Property Browser Support
Text truncate text-overflow All
Centering flex / grid All
Sticky positioning position: sticky All
Responsive fonts clamp() All
Container queries container-type All modern
Aspect ratio aspect-ratio All modern
Parent selector :has() All modern
Scroll snap scroll-snap-type All
View transitions view-transition-name Chrome/Edge

What's your favorite CSS trick? Share it in the comments!

Follow @armorbreak for more frontend content.

Top comments (0)