CSS Snippets Library
Essential CSS code snippets for common UI patterns. Copy-paste ready with explanations.
Text Selection Styling
Typography
Style the background and text color when users select text on your page
Select this text to see the custom selection colors in action!
Custom Scrollbar (Webkit)
UI Elements
Style scrollbars for Chrome, Safari, and Edge browsers
Modern Focus States
Accessibility
Accessible focus styles that only show for keyboard navigation
Tab to this button to see the focus ring. Click won't show it (focus-visible)
Smooth Scrolling
Behavior
Enable smooth scroll behavior for anchor links and programmatic scrolling
Enables smooth scrolling for anchor links and programmatic scroll. Respects user's motion preferences.
Text Truncation (Single Line)
Typography
Truncate text with ellipsis when it overflows (single line)
This is a very long line of text that will be truncated with an ellipsis when it exceeds the container width. You won't see the end of this sentence...
Multi-line Text Clamp
Typography
Limit text to a specific number of lines with ellipsis
This is a multi-line paragraph that demonstrates the line-clamp property. It will show exactly 3 lines of text and then cut off with an ellipsis. This is particularly useful for card layouts where you want to preview content but maintain a consistent height. The text continues but gets cut off after three lines...
Hide Scrollbar (Keep Functionality)
UI Elements
Hide scrollbar visually but keep scroll functionality intact
Hide Scrollbars on Specific Element
UI Elements
Hide both horizontal and vertical scrollbars on a centered scrollable element
Backdrop Blur (Glass Effect)
Effects
Create frosted glass effect with backdrop blur
Glass effect with backdrop blur
Aspect Ratio Container
Layout
Maintain aspect ratio for responsive media containers
Center with Position Absolute
Layout
Center an element using absolute positioning
Gradient Text
Typography
Apply gradient color to text
Gradient Text Effect
Placeholder Styling
Forms
Style placeholder text in form inputs
Dark Mode (Auto)
Theming
Enable automatic dark mode based on user preference
Fluid Type with clamp()
Typography
Responsive font sizes that scale between min and max
Theme Tokens (CSS Variables)
Theming
Define light/dark tokens for colors and reuse across UI
Container Queries (Card Adaptation)
Layout
Make components adapt based on their own width with container queries
Parent Styles with :has()
Forms
Style a parent when a child input matches a state
Scroll Snap (Horizontal Carousel)
Interaction
Create a smooth snapping carousel with pure CSS
Anchor Offset for Sticky Headers
Behavior
Prevent content from hiding behind a fixed/sticky header when linking in-page
Stable Layout with Scrollbars
Layout
Reserve space for scrollbars to avoid layout shift
Safe-Area Insets (iOS)
Layout
Respect device notches and home indicators with env() insets
Sticky Footer Grid
Layout
Keep footer at the bottom on short pages without JS
Full-Bleed Section in Centered Layout
Layout
Let a section extend edge-to-edge inside a centered wrapper
Elegant Underlines
Typography
Readable, offset underlines using text-decoration properties
Balanced Headlines & Hyphenation
Typography
Improve rag and wrapping for headings and paragraphs
Tabular Numbers
Typography
Align digits cleanly for prices, timers, and data tables
Accent Color for Inputs
Forms
Set a brand color for native form controls
Caret & Selection Tint
Forms
Pair caret color with brand selection styles
Autofill Styling (WebKit)
Forms
Neutralize bright yellow autofill backgrounds
Custom File Input Button
Forms
Style the file selector button without extra markup
:focus-within Group Highlight
Accessibility
Highlight a field group when any child is focused
Reduced Motion Fallbacks
Accessibility
Respect user motion preferences by disabling animations
Overscroll Control (Modals)
Behavior
Prevent background scrolling and contain overscroll in dialogs
Per-Section Scroll Margin
Behavior
Give anchor targets breathing room under sticky headers
Scroll Shadows (Edge Hints)
Effects
Subtle shadows to indicate scrollable areas
Skeleton Loading (Shimmer)
Effects
Lightweight shimmer placeholder for loading states
CSS Nesting (Modern Syntax)
Syntax
Organize component styles with native CSS nesting
Masonry-ish Columns
Layout
Simple Pinterest-style columns without JS
Print Styles for Links
Append URLs after links and hide unwanted elements in print
Pointer & Hover Media Queries
Interaction
Tweak UI for touch devices that lack hover
Keyboard-Safe Viewport Units
Layout
Use dynamic viewport height to avoid mobile browser UI jumps