Skip to content

Instantly share code, notes, and snippets.

@deadflowers
Last active January 3, 2025 13:56
Show Gist options
  • Select an option

  • Save deadflowers/c11c4c2ce5e08c45bd58ede074208ea6 to your computer and use it in GitHub Desktop.

Select an option

Save deadflowers/c11c4c2ce5e08c45bd58ede074208ea6 to your computer and use it in GitHub Desktop.
Modernizing Your CSS 2025

Modernizing Your CSS 2025

CSS has evolved considerably in recent years offering improved flexibility, maintainability, and design possibilities. While it's exciting to be constantly presented with new solutions, the changes are coming fast. Maybe even too fast! It can be a lot just keeping up with everything. Some would even say overwhelming. Especially if you aren't a full time designer. I think the best way to handle that is 1st, remind yourself nobody is 100% on top of all of it at all times. So you're probably nowhere near as far behind as you think.

In any case step 2 is is going to help and that is, seek out some new CSS to experiment with! Do you know if you're employing obsolete functions in current day projects? I don't but want to know if I am. Are there far simpler ways to do things we've many of us long struggled with? Speaking for myself I'd be shocked if there wasn't! So here's 30 of the bigger changes to be knowledgeable of and experiment with to get your old libraries up to date and keep your customers on the cutting edge.

BTW, the last one is so effing cool I might dedicate a post just to it!


Layout Enhancements

  1. CSS Grid Layout

    • Enables two-dimensional layouts with rows and columns, replacing complex float or table-based layouts.
    • Properties: grid-template-rows, grid-template-columns, grid-gap, etc.
  2. CSS Flexbox Enhancements

    • Offers improved alignment and distribution capabilities for one-dimensional layouts.
    • Properties: align-items, justify-content, gap (now supported in Flexbox).
  3. Subgrid

    • Extends the Grid layout by allowing nested grids to inherit the parent's grid structure.
  4. Aspect Ratio

    • Simplifies creating responsive media containers without relying on padding hacks.
    • Property: aspect-ratio.
  5. Container Queries (Candidate Recommendation)

    • Provides styles based on the size of a parent container instead of the viewport.
    • Syntax: @container (min-width: 600px) { ... }.

Styling Improvements

  1. CSS Variables (Custom Properties)

    • Allows reusable values and dynamic theming.
    • Syntax: --main-color: #ff6347; color: var(--main-color);.
  2. Native Masonry Layout

    • Enables Pinterest-style layouts natively in CSS.
    • Property: grid-template-rows: masonry;.
  3. Box Alignment Module

    • Extends alignment capabilities across Grid, Flexbox, and Block layout contexts.
    • Properties: align-self, place-items, place-content.
  4. CSS Logical Properties and Values

    • Abstracts layout direction for better support of right-to-left (RTL) and vertical writing modes.
    • Properties: margin-inline-start, padding-block-end, etc.
  5. Clamp Function

    • Provides responsive values that adapt to min and max ranges.
    • Syntax: font-size: clamp(1rem, 2.5vw, 2rem);.

Interactivity and Animations

  1. Scroll Snap

    • Smooth scrolling and snapping to defined points.
    • Properties: scroll-snap-type, scroll-snap-align.
  2. CSS Motion Path

    • Animates elements along a custom path.
    • Properties: offset-path, offset-distance.
  3. @keyframes Easing Functions

    • Custom easing curves directly in animations.
    • Syntax: animation-timing-function: cubic-bezier(...);.
  4. Backdrop Filter

    • Applies blur or color effects behind an element.
    • Property: backdrop-filter: blur(10px);.
  5. Scroll-Linked Animations (Scroll Timeline API)

    • Syncs animations with scroll progress.
    • Property: animation-timeline: scroll().
  6. Will-Change Optimization

    • Hints the browser for animations, reducing rendering costs.
    • Property: will-change: transform;.

Media Features

  1. Media Queries Level 4

    • Adds new media query features and ranges.
    • Examples: @media (prefers-color-scheme: dark) or @media (width >= 500px).
  2. Reduced Motion Media Query

    • Adapts animations for users with motion sensitivities.
    • Example: @media (prefers-reduced-motion: reduce).
  3. Resolution Independence

    • Query DPI or device-pixel ratio directly.
    • Syntax: @media (min-resolution: 2dppx).

Typography

  1. Variable Fonts

    • Combine multiple font styles in a single file for performance and flexibility.
    • Properties: font-variation-settings.
  2. Line Clamp

    • Limits text to a specified number of lines with ellipsis.
    • Property: line-clamp.
  3. Hyphenation

    • Enables better word wrapping and justification.
    • Property: hyphens: auto;.
  4. Font Loading API (via JavaScript)

    • Allows control over font loading behavior.

Borders and Effects

  1. Rounded Borders and Shadows

    • Use logical shorthand for rounded corners.
    • Properties: border-radius: 50% 0 0 50%;.
  2. Inset Box Shadow

    • Creates more dynamic shadow effects.
    • Property: box-shadow: inset 0 0 10px #000;.

Utility Improvements

  1. :is() and :where() Pseudo-classes

    • Streamline selectors with fallback styles.
    • Syntax: :is(h1, h2, h3) { margin: 0; }.
  2. :not() Enhancements

    • Accepts complex selectors.
    • Syntax: :not(.class1, .class2).
  3. has() Pseudo-class

    • Selects parent elements based on children.
    • Syntax: div:has(> img).
  4. accent-color

    • Standardizes the styling of form elements.
    • Property: accent-color: rebeccapurple;.
  5. @layer Rule (CSS Cascade Layers)

    • Prioritizes CSS rules based on origin for better modularity.
    • Syntax: @layer utilities { ... }.
  6. color-contrast()

    • Function to determine which color contrats more with given color find the more contrasting color (e.g. words vs background)
    • Syntax:
    background: var(--bg);color: color-contrast(var(--bg) vs white, black);

How to Leverage These Updates

To modernize a CSS library:

  • Embrace Grid and Flexbox: Replace float/grid systems with native Grid/Flexbox layouts.
  • Use Variables: Define themes and reusable tokens with CSS variables.
  • Enhance Interactivity: Add scroll-based and animation effects.
  • Modularize Styles: Leverage @layer for prioritizing utility styles.
  • Improve Accessibility: Adapt layouts with media queries like prefers-reduced-motion.

These updates ensure better performance, maintainability, and support for responsive and modern design paradigms.

Part 2

We can drill into this a bit more though for more actionable insights filtering first by changes just in the past few years, and looking at some specific code examples. This part 2 of Modernizing Your CSS 2025 is intended to be just that though I can't promise the dates recorded here are accurate. In any case much of this like the previous list was new to me and I definitely have plenty to learn and play with!


1. Container Queries (2022)

Allow styling based on a parent container's size instead of the viewport.

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

/* Apply styles based on container width */
@container (min-width: 600px) {
  .child {
    font-size: 1.5rem;
    grid-template-columns: repeat(3, 1fr);
  }
}

2. CSS Grid Subgrid (2022)

Allows nested grids to inherit the parent grid structure for complex layouts.

/* Parent grid */
.parent {
  display: grid;
  grid-template-columns: 1fr 2fr;
  gap: 1rem;
}

/* Child grid inherits parent's structure */
.child {
  display: subgrid;
  grid-template-rows: subgrid;
}

3. Accent-Color (2021)

Standardizes form control styling.

/* Customize form control colors */
input[type="checkbox"], input[type="radio"] {
  accent-color: rebeccapurple;
}

4. @layer Rule for Cascade Layers (2022)

Introduces layers for modular and predictable CSS.

/* Define layers */
@layer reset, base, utilities;

/* Add styles in specific layers */
@layer reset {
  * {
    margin: 0;
    padding: 0;
  }
}

@layer base {
  body {
    font-family: sans-serif;
    line-height: 1.6;
  }
}

@layer utilities {
  .text-center {
    text-align: center;
  }
}

5. Scroll-Linked Animations (2023)

Synchronize animations with the user's scroll position.

/* Define a scroll animation timeline */
@scroll-timeline my-timeline {
  source: auto;
  axis: block;
}

/* Attach animation to an element */
.animated {
  animation: slide 2s linear both;
  scroll-timeline: my-timeline;
}

@keyframes slide {
  from {
    transform: translateX(-100%);
  }
  to {
    transform: translateX(0);
  }
}

6. CSS Masonry Layout (2022)

Native masonry layout support for grid-like designs.

/* Create a masonry layout */
.masonry {
  display: grid;
  grid-template-rows: masonry;
  gap: 1rem;
}

7. Logical Properties for RTL Support

Improves internationalization by abstracting directional properties.

/* Logical spacing for left-to-right or right-to-left layouts */
.card {
  padding-inline: 1rem;
  margin-block: 1rem;
}

8. Line Clamp (2021)

Limits text to a specific number of lines with ellipses.

/* Truncate text to three lines */
.truncated {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

9. Aspect-Ratio (2021)

Simplifies responsive design for images, videos, and containers.

/* Maintain a 16:9 aspect ratio */
.video-container {
  aspect-ratio: 16 / 9;
}

10. :has() Pseudo-Class (2023)

Select parent elements based on the presence of child elements.

/* Highlight sections containing an image */
section:has(img) {
  border: 2px solid rebeccapurple;
}

11. Gap Support in Flexbox (2021)

Adds gap property compatibility with Flexbox layouts.

.flex-container {
  display: flex;
  gap: 1rem;
}

12. Clamp() Function for Responsive Sizing

Define responsive values with a minimum, preferred, and maximum range.

/* Responsive font size */
h1 {
  font-size: clamp(1.5rem, 2vw, 3rem);
}

13. Backdrop-Filter Enhancements

Apply visual effects to backgrounds.

/* Blur the background */
.modal {
  backdrop-filter: blur(10px);
}

14. Custom Scrollbars

Customize scrollbar appearance.

/* Custom scrollbar styling */
.scroll-container::-webkit-scrollbar {
  width: 10px;
}

.scroll-container::-webkit-scrollbar-thumb {
  background: linear-gradient(to bottom, #6b0f1a, #b91372);
  border-radius: 5px;
}

15. Native Media Query Ranges

Simplify writing media queries.

/* Match widths between 500px and 1200px */
@media (width >= 500px) and (width <= 1200px) {
  .responsive {
    font-size: 1.5rem;
  }
}

16. Reduce Motion for Accessibility

Adapt animations for users who prefer reduced motion.

/* Disable animations for motion-sensitive users */
@media (prefers-reduced-motion: reduce) {
  * {
    animation: none;
  }
}

17. Font-Optical-Size (2022)

Improves font rendering for different sizes.

/* Enable optical sizing for fonts */
body {
  font-optical-sizing: auto;
}

18. Overscroll Behavior

Control scroll chaining behavior.

/* Prevent scrolling beyond container boundaries */
.container {
  overscroll-behavior: contain;
}

19. Native Color Functions

Use color-mix() for dynamic color blending.

/* Blend colors dynamically */
.button {
  background-color: color-mix(in srgb, red 50%, blue 50%);
}

20. :is() and :where() Simplified Selectors

Simplify complex selectors.

/* Apply styles to multiple heading levels */
:is(h1, h2, h3) {
  margin-bottom: 1rem;
}

/* Use :where() for low-specificity rules */
:where(.utility-class) {
  margin: 0;
}

Summary of Modern Features

These updates represent major advances in responsiveness, accessibility, and styling flexibility for the past few years and demonstrate how to incorporate them or update past projects and modernizing your toolkit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment