10 CSS Properties That Quietly Deleted My JavaScript
Every few months I go back through an old project and find a chunk of JavaScript solving a problem that CSS now solves on its own — a resize listener recalculating an aspect ratio, a scroll handler smoothing out anchor links, a canvas hack blurring a background. None of it was bad code when it was written. It's just that the platform quietly caught up, and the JavaScript never got removed. Here…
CSS properties have quietly replaced a significant amount of JavaScript in web projects. These properties have streamlined functionality and improved performance. Here are ten CSS properties that have replaced JavaScript:
1. Aspect-ratio: Maintaining an image, video, or placeholder box at a fixed proportion can now be achieved with a single line of code: `.image { aspect-ratio: 16 / 9; object-fit: cover; }`. This eliminates the need for resize listeners and layout thrashing.
2. Object-fit: This property, often used in conjunction with aspect-ratio, fits an image into a fixed box without distortion. It replaces the need to swap <img> tags for background-image and manage background-size/position, preserving alt text, right-click-to-save, and lazy-loading attributes.
3. Gap: Spacing between flex or grid children was previously achieved by adding margins to every child except the last one. The `gap` property simplifies this by consistently applying spacing on both axes and handling layout wrap changes.
4. Backdrop-filter: Creating frosted-glass modal or header effects used to involve capturing a screenshot of the background and blurring it manually with canvas. The `backdrop-filter` property now allows live updates of blurs as the content scrolls or animates.
5. Clip-path: Non-rectangular shapes, such as diagonal-cut hero images, hexagonal avatars, or chevron dividers, were previously created using SVG masks or manual canvas drawing. The `clip-path` property simplifies the creation of these shapes and allows for smooth transitions and animations.
6. Scroll-behavior: Smooth scrolling to anchors or back to the top of a page used to require custom window.scrollTo() implementation. The `scroll-behavior` property now applies smooth scrolling to all same-page navigation and hash links by default.
7. Overscroll-behavior: Preventing scrollable sidebars or modals from chaining and scrolling the page behind them was previously achieved through touchstart/touchmove event handling. The `overscroll-behavior` property now allows stopping scroll chaining while maintaining scroll functionality within the element.
8. Pointer-events: Toggling click interactivity for elements, such as overlays, was typically accomplished with JavaScript branches that modify class or style properties. The `pointer-events` property simplifies this by allowing the interactivity toggle to live entirely in CSS.
9. Text-overflow: Truncating long text with a trailing ellipsis used to be achieved by measuring rendered text width in JavaScript and manually slicing the string. The `text-overflow` property now allows for automatic and correct truncation on every resize.
10. Color-scheme: Before this property, native browser UI like form controls, scrollbars, and page backgrounds remained unaffected by prefers-color-scheme media queries. The `color-scheme` property now tells the browser to adjust these elements to match the user's preferred color scheme, eliminating visible mismatches.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.