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!

selectionhighlighttypography

Custom Scrollbar (Webkit)

UI Elements

Style scrollbars for Chrome, Safari, and Edge browsers

Scroll this area to see the custom scrollbar. The scrollbar is styled with a thin width, light track, and rounded thumb that darkens on hover.

Keep scrolling to see more...

The custom styling makes scrollbars feel more polished and match your brand.

scrollbarwebkitui

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)

focusaccessibilitya11ykeyboard

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.

scrollsmoothanimationaccessibility

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...

textellipsisoverflowtruncate

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...

textclampellipsismultiline

Hide Scrollbar (Keep Functionality)

UI Elements

Hide scrollbar visually but keep scroll functionality intact

This area is scrollable but the scrollbar is hidden. Try scrolling with your mouse wheel or trackpad.

The scroll functionality works perfectly, but the visual scrollbar is hidden for a cleaner look.

Great for custom UI designs.

scrollbarhideoverflow

Hide Scrollbars on Specific Element

UI Elements

Hide both horizontal and vertical scrollbars on a centered scrollable element

scrollbarhideoverflowcenter

Backdrop Blur (Glass Effect)

Effects

Create frosted glass effect with backdrop blur

Glass effect with backdrop blur

blurglassbackdropeffect

Aspect Ratio Container

Layout

Maintain aspect ratio for responsive media containers

16:9 Aspect Ratio
aspect-ratioresponsivelayout

Center with Position Absolute

Layout

Center an element using absolute positioning

Perfectly Centered
centerabsolutepositionlayout

Gradient Text

Typography

Apply gradient color to text

Gradient Text Effect

gradienttextcolortypography

Placeholder Styling

Forms

Style placeholder text in form inputs

placeholderinputforms

Dark Mode (Auto)

Theming

Enable automatic dark mode based on user preference

dark-modethemingprefers-color-scheme

Fluid Type with clamp()

Typography

Responsive font sizes that scale between min and max

typographyresponsiveclamp

Theme Tokens (CSS Variables)

Theming

Define light/dark tokens for colors and reuse across UI

css-variablestokenstheming

Container Queries (Card Adaptation)

Layout

Make components adapt based on their own width with container queries

container-querieslayoutresponsive

Parent Styles with :has()

Forms

Style a parent when a child input matches a state

hasformsparent-selector

Scroll Snap (Horizontal Carousel)

Interaction

Create a smooth snapping carousel with pure CSS

scroll-snapcarouselinteraction

Anchor Offset for Sticky Headers

Behavior

Prevent content from hiding behind a fixed/sticky header when linking in-page

anchorscrollsticky-header

Stable Layout with Scrollbars

Layout

Reserve space for scrollbars to avoid layout shift

scrollbarlayout-shiftstability

Safe-Area Insets (iOS)

Layout

Respect device notches and home indicators with env() insets

safe-areaioslayout

Sticky Footer Grid

Layout

Keep footer at the bottom on short pages without JS

footergridlayout

Full-Bleed Section in Centered Layout

Layout

Let a section extend edge-to-edge inside a centered wrapper

full-bleedlayoutvw

Elegant Underlines

Typography

Readable, offset underlines using text-decoration properties

underlinelinkstypography

Balanced Headlines & Hyphenation

Typography

Improve rag and wrapping for headings and paragraphs

text-wraphyphenstypography

Tabular Numbers

Typography

Align digits cleanly for prices, timers, and data tables

numberstypographydata

Accent Color for Inputs

Forms

Set a brand color for native form controls

formsaccent-colorinputs

Caret & Selection Tint

Forms

Pair caret color with brand selection styles

caretformsbrand

Autofill Styling (WebKit)

Forms

Neutralize bright yellow autofill backgrounds

autofillwebkitforms

Custom File Input Button

Forms

Style the file selector button without extra markup

file-inputformsui

:focus-within Group Highlight

Accessibility

Highlight a field group when any child is focused

focus-withina11yforms

Reduced Motion Fallbacks

Accessibility

Respect user motion preferences by disabling animations

reduced-motionaccessibilitya11y

Overscroll Control (Modals)

Behavior

Prevent background scrolling and contain overscroll in dialogs

overscrollmodalbehavior

Per-Section Scroll Margin

Behavior

Give anchor targets breathing room under sticky headers

scrollanchorssticky

Scroll Shadows (Edge Hints)

Effects

Subtle shadows to indicate scrollable areas

scrollshadowsux

Skeleton Loading (Shimmer)

Effects

Lightweight shimmer placeholder for loading states

skeletonloadingshimmer

CSS Nesting (Modern Syntax)

Syntax

Organize component styles with native CSS nesting

nestingsyntaxcomponents

Masonry-ish Columns

Layout

Simple Pinterest-style columns without JS

masonrycolumnsgallery

Print Styles for Links

Print

Append URLs after links and hide unwanted elements in print

printlinksmedia-query

Pointer & Hover Media Queries

Interaction

Tweak UI for touch devices that lack hover

hoverpointerresponsive

Keyboard-Safe Viewport Units

Layout

Use dynamic viewport height to avoid mobile browser UI jumps

viewportdvhmobile
Webrenew

Ready to build something great?