30 Seconds of CSS
30 Seconds of CSS

A curated collection of useful CSS snippets you can understand in 30 seconds or less.

"30 Seconds of CSS" is a curated collection of useful CSS snippets intended to be understood in 30 seconds or less.

Here are the principal characteristics and categories of the snippets included in this collection:

Core Purpose and Utility

  • Curated Collection: It provides CSS solutions for common design and layout challenges.

  • Accessibility and Speed: Snippets are designed to be easily understood. Each snippet includes the necessary HTML and CSS, a demo, and a detailed explanation of how the code works.

  • Browser Support: Each entry lists the current browser support percentage, often noting if prefixes are required for full coverage.

Main Categories of Snippets

The collection is divided into several major categories covering key areas of web development:

1. Layout (Positioning and Structure)

This category addresses how elements are positioned on the page.

  • Centering Techniques: Includes modern methods like Flexbox centering and Grid centering (horizontally and vertically), as well as older alternatives like Display table centering and Transform centering.

  • Box Model Management: Features a Box-sizing reset which ensures that an element's defined width and height are not affected by its border or padding.

  • Responsive Ratios: Provides a snippet for maintaining a Constant width to height ratio on elements with variable width.

  • Space Distribution: Includes Evenly distributed children using justify-content: space-between.

  • Accessibility Hiding (Offscreen): Offers a "bulletproof way" to hide an element visually and positionally while keeping it accessible to screen readers, useful for ADA development.

2. Visual (Aesthetics and Styling)

This category focuses on decorative and stylistic effects.

  • Text Effects: Includes Gradient text (using proprietary -webkit- properties), Etched text, and techniques for Truncate text (single line with ellipsis) or Truncate text multiline (ending with a gradient fade).

  • Customization: Features snippets for creating Custom scrollbars (on WebKit platforms), applying Custom text selection styles, and using a System font stack to mimic a native app feel.

  • Shapes and Borders: Shows how to create simple CSS shapes like a Circle or a Triangle, and how to achieve a Hairline border using box-shadow and media queries based on device pixel ratio.

  • Dynamic Graphics: Features a Dynamic shadow that uses inheritance and blur filters to create a shadow based on the element's own gradient color.

3. Animation and Interactivity

This section provides dynamic visual effects and user experience improvements.

  • Loaders: Examples include a Bouncing loader (using translate3d() for performance) and a Donut spinner.

  • Transitions: Snippets for Height transition (from 0 to auto, requiring JavaScript) and using Easing variables (cubic-bezier values) for powerful timing functions.

  • Hover Effects: Examples include Hover underline animation, Hover Shadow Box Animation, and Sibling fade (fading out unhovered siblings).

  • Form Interaction: Includes a CSS-only Toggle switch and Focus Within to style a parent element when one of its children is focused.

4. Other/Utility

This covers powerful CSS features and general utilities.

  • Mathematical Functions: The Calc() function allows defining values using mathematical expressions, permitting the use of different units (e.g., pixel and percent) together.

  • CSS Variables: Demonstrates defining and reusing Custom variables globally within the :root pseudo-class.

  • Counters: Explains how to use Counters (variables maintained by CSS) to create automatically numbered, outlined lists.

  • Style Reset: Provides the Reset all styles snippet using all: initial to reset all properties to their default values.