DEV Community

Alex Chen
Alex Chen

Posted on

10 CSS Tricks I Use Every Day

10 CSS Tricks I Use Every Day

These CSS techniques save me hours. Some are simple, some are clever — all are practical.

1. Center Anything

/* The modern way — works for everything */
.container {
  display: grid;
  place-items: center;
}

/* Flexbox version */
.container {
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Old reliable — for absolute positioning */
.child {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
Enter fullscreen mode Exit fullscreen mode

2. Smooth Scrolling

/* One line for smooth scrolling everywhere */
html {
  scroll-behavior: smooth;
}

/* Now anchor links scroll smoothly */
/* <a href="#section">Go to section</a> */
Enter fullscreen mode Exit fullscreen mode

3. Text Truncation

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

/* Multi-line truncation (2 lines) */
.line-clamp-2 {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* Multi-line truncation (3 lines) */
.line-clamp-3 {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
Enter fullscreen mode Exit fullscreen mode

4. Sticky Header

/* Sticky header — sticks to top when scrolling past it */
header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: white;
  backdrop-filter: blur(10px); /* Frosted glass effect */
}
Enter fullscreen mode Exit fullscreen mode

5. Custom Scrollbar

/* Custom scrollbar (WebKit browsers) */
::-webkit-scrollbar {
  width: 8px;
}

::-webkit-scrollbar-track {
  background: #f1f1f1;
}

::-webkit-scrollbar-thumb {
  background: #888;
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: #555;
}
Enter fullscreen mode Exit fullscreen mode

6. Aspect Ratio

/* Responsive video container — no more padding-bottom hack! */
.video-container {
  aspect-ratio: 16 / 9;
  width: 100%;
  max-width: 800px;
}

/* Square images */
.avatar {
  aspect-ratio: 1;
  width: 48px;
  border-radius: 50%;
  object-fit: cover;
}

/* Card images */
.card-image {
  aspect-ratio: 3 / 2;
  width: 100%;
  object-fit: cover;
}
Enter fullscreen mode Exit fullscreen mode

7. Responsive Typography

/* Fluid typography — scales between min and max */
h1 {
  font-size: clamp(2rem, 5vw, 4rem);
  /* min: 2rem, preferred: 5vw, max: 4rem */
}

p {
  font-size: clamp(1rem, 2.5vw, 1.25rem);
  line-height: 1.6;
}

/* Container queries (modern alternative) */
.card {
  container-type: inline-size;
}

@container (min-width: 400px) {
  .card-title {
    font-size: 1.5rem;
  }
}
Enter fullscreen mode Exit fullscreen mode

8. Glassmorphism

/* Frosted glass card */
.glass-card {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 16px;
  padding: 2rem;
}

/* Works best on colored backgrounds */
Enter fullscreen mode Exit fullscreen mode

9. Animated Underline Links

/* Animated underline that slides in on hover */
.animated-link {
  position: relative;
  text-decoration: none;
  color: #333;
}

.animated-link::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 0;
  height: 2px;
  background: #0066ff;
  transition: width 0.3s ease;
}

.animated-link:hover::after {
  width: 100%;
}
Enter fullscreen mode Exit fullscreen mode

10. Dark Mode Toggle

/* System preference */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a2e;
    --text: #e0e0e0;
    --card: #16213e;
    --border: #2a2a4a;
  }
}

/* Manual toggle with data attribute */
:root {
  --bg: #ffffff;
  --text: #1a1a1a;
  --card: #f5f5f5;
  --border: #e0e0e0;
}

[data-theme="dark"] {
  --bg: #1a1a2e;
  --text: #e0e0e0;
  --card: #16213e;
  --border: #2a2a4a;
}

body {
  background: var(--bg);
  color: var(--text);
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
}
Enter fullscreen mode Exit fullscreen mode
// Toggle function
function toggleDarkMode() {
  const isDark = document.documentElement.getAttribute('data-theme') === 'dark';
  document.documentElement.setAttribute('data-theme', isDark ? 'light' : 'dark');
  localStorage.setItem('theme', isDark ? 'light' : 'dark');
}

// Load saved preference
const saved = localStorage.getItem('theme');
if (saved) document.documentElement.setAttribute('data-theme', saved);
Enter fullscreen mode Exit fullscreen mode

Bonus: Selection Color

/* Customize text selection color */
::selection {
  background: #0066ff;
  color: white;
}
Enter fullscreen mode Exit fullscreen mode

Bonus: Focus Styles

/* Better focus styles for accessibility */
:focus-visible {
  outline: 2px solid #0066ff;
  outline-offset: 2px;
  border-radius: 4px;
}

/* Remove default outline if you have custom focus styles */
button:focus:not(:focus-visible) {
  outline: none;
}
Enter fullscreen mode Exit fullscreen mode

What's your favorite CSS trick? Share it below!

Follow @armorbreak for more frontend content.

Top comments (0)