I got tired of CSS media queries

2026-11-24来源 dev.toEnglish
I got tired of CSS media queries

W3Schools responsive web design definition:

Responsive web design is about creating web pages that look good on all devices! A responsive web design will automatically adjust for different screen sizes and viewports.

One standard technique to build such interfaces is applying CSS Media Queries, according to W3Schools:

CSS media queries allow you to apply styles based on the characteristics of a device or the environment displaying the web page. CSS media queries are essential for creating responsive web pages.

My question: are they really essential?

Intro 📚

Hey guys, I hope you are doing well!

My name is Arthur, and for the past few years I have been dedicating time and effort to web responsiveness.

Back in 2018, I was working for a startup, and we had this big rebranding launch scheduled. The new website should be published before the big launch, and we were working on a tight schedule.

That was an intense delivery. Looking back, what took up most of our time in launching that website was making sure it looked good on all screen resolutions.

If you are still not familiar with creating style rules with breakpoints using CSS media queries, check the simple example below:

/* Default h1 font-size set as 24px */ h1 { font-size: 24px; } 

We can create a "breakpoint" at 1536px screen width:

/* h1 font-size is changed to 32px in screens with 1536px width or more */ @media screen and (min-width: 1536px) { h1 { font-size: 32px; } } </style> 

The problem 🧐

Manually create and adjust styles with breakpoints for ALL screen resolutions is very time-consuming. Most of the time, it just doesn't worth the effort.

Considering just Desktop screen resolutions, we can have screens from 1280 × 720 pixels to 3840 × 2160 pixels (4K Ultra HD). Add iPads and mobiles to the scene...that's just too much, right?

My solution 🤓

What if we could build a scalable interface that would keep its sizes and proportions from the reference design across all screen resolutions?

That has been my goal with ui-scaler, open-source package, published on NPM:

ui-scaler demo

Ever since, I have been building my web pages in the resolution reference, whether a Figma design or my own monitor, and I am guaranteed they will be displayed the same for every different screen:

  • I build them once for Desktop; The design is consistent for all Desktop resolutions
  • I adjust it for Mobiles; The design is consistent for all Mobile resolutions

Want to hear the best part? It's super easy to implement!

How it works ⚙️

ui-scaler takes advantage of the native CSS relative length unit rem:

The rem measurement unit is a relative unit that you can use for length values in CSS [...] rem stands for root em, which is a measurement unit that refers to the font-size of the root element of a document

To achieve the demonstrated above effect, it's essential that all sizing and positioning styles in the webpage are defined with the rem unit. This is the only requirement.

Two good news:

  1. Modern styles frameworks, such as Tailwind CSS and Bootstrap apply styles with rem by default
  2. ui-scaler optionally can convert pixels to the respective rem value (see more below)

Considering that, you just need to install the package and execute the scaleUI method in your project entry point (index.js, main.ts...).

This method will dynamically change the html element font-size based on the user's screen resolution. As a result, all styles defined with rem adjust accordingly!

Web accessibility 🫂

What makes this approach work in the real world are 2 extra considerations the script does:

1) User browser font size preference:

ui-scaler will consider the difference between the browser font size preference and the default 16px. All font-size styles on the page will take that difference into consideration.

2) User zoom:

When users zoom in or out (CTRL+ or CTRL-), they are effectively changing the viewport. This affects window.innerWidth and window.innerHeight used by ui-scaler to set the ideal font-size for the given page.

The package then uses the devicePixelRatio property from the window interface to apply compensation and make sure users can zoom as they wish.

Final considerations 🏁

Breakpoints Media Queries

Even with ui-scaler, CSS breakpoints will still be needed to build responsive web interfaces, but only for reorienting elements between landscape vs portrait, for example. The package drastically reduces the need for media queries.

Practical and simple

The goal for this solution has always been making the life of the developer easier! For most cases, to apply we just need to execute the method and that is it.

On the official website, you can find the installation guide for all major frameworks (React, Angular, Vue, Svelte...), and instructions for working with it on the server side (SSR).

Options

The method scaleUI() receives an optional param object. You can check all available options on the website as well.

The most useful one for applying the package in existing projects is the baseFontSize. This works as a global sizing adjustment. The default value is 16. If you install the package and all your elements are looking too large, for example, you can just lower this number to 15 or 14.

Feedback 🤝

The website uses the package! In case you want a playground, access it and play around with multiple screen resolutions. Observe how elements scale, and how the html element's font-size updates.

I would love to hear any feedback from you.

Wish to help?

  • ⭐ the repository
  • Share with others
  • Leave a comment below

原文:https://www.a8q9.cn/en/article/i-got-tired-of-css-media-queries-cc36b6.html

相关文章