Is there a way to synchronize breakpoint values across different media queries?

Currently utilizing Next.js version 9.53.

Utilizing the built-in CSS module support provided by Next.js without the use of CSS pre-processors (less, sass, etc.) or CSS-in-JS (styled-components, etc.).

Presently, my approach involves writing CSS in a mobile-first manner with redundant media queries such as:

/* "desktop" */
@media screen and (min-width: 1024px) {
  ...
}

These duplicate copies of media queries exist in various modules. While I appreciate having alternative styles nearby default styling definitions, the reliance on specific pixel values in the media queries is not ideal for me.

In addition to defining high-level elements in a "global" CSS file and using CSS variables extensively for consistency throughout module CSS, I face limitations when it comes to sharing breakpoints across different media queries, as explained in this SO answer.

I am seeking a solution akin to CSS variables that allows for sharing size breakpoints among multiple media queries:

  • My preference is to maintain alternative CSS queries close to their defaults within respective modules rather than consolidating them into one centralized location.
  • An uncomplicated resolution would be preferable, avoiding significant changes to the build process like incorporating a CSS pre-processor solely for this purpose.

Answer №1

To achieve this functionality, you can utilize the matchMedia method.

I provided a demonstration in this response.

One approach is to save the common breakpoint in a JavaScript variable and apply it across various media queries.

Answer №2

According to the information provided in the spec,

The usage of the var() function is restricted to replacing portions of values within properties on an element. It cannot be applied to property names, selectors, or any other context outside of property values. Attempting to use it in such contexts may result in invalid syntax or a value unrelated to the intended variable.

In conclusion, utilizing the var() function within a media query is not permitted.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When attempting to print a Rectangle on my webpage using JavaScript and taking user input, I encountered an error stating that 'str' is not defined

I'm trying to implement a feature where clicking on the "try it" button will prompt the user for the number of lines and then print that many lines in the form of a rectangle. However, when I run my code, nothing appears on the DOM and there is an err ...

Can we make one tab disappear when another tab is clicked in Ionic 2 app menu icon?

I am working on a project using Ionic 2 and I have implemented multiple tabs in the application. However, I need to find a way to hide the top tab when the user clicks on the bottom tabs menu icon. Here is my Plunker for your reference. My goal is to ma ...

"Ensuring a DIV element has a height of 100% within

Is there a way to set and enforce a div's height to be 100% of the browser window, another div, or a table cell without resorting to complicated hacks found on Stack Overflow or other websites? ...

The redirection feature in NextResponse seems to be malfunctioning

Currently facing an issue with the NextResponse redirect on my Next 13 project. While I am able to login successfully, the redirect is not functioning properly, resulting in the cookie not being set as expected. Below is the code snippet causing the prob ...

NextJS is throwing an error when trying to access the value returned

I've been exploring next.js and am trying to implement useContext for managing global state. However, I'm encountering difficulty in passing the value from provider to child element as the returned value of language in header.js is coming up as u ...

Choosing a single row from a Bootstrap React table

i have successfully implemented the Async Function to display data, as shown in the image. the table starts empty but gets populated after the await. However, I am facing an issue in finding a property to retrieve the selected row from the table. Is there ...

Flexbox's bootstrap columns display issue on smaller screens with no wrapping

I am currently working on a section of my website where I have applied some CSS to 2 divs and an anchor tag in order to center the content vertically. However, I am facing an issue with the flex style properties when the window size is less than 768px. In ...

Update the image source within a CSS element

I'm facing an issue with changing the source image of an Element. The situation is that I have a customized CSS script and I need to modify a logo image. Below, you can find the source code: https://i.stack.imgur.com/8HLCE.png The problem lies in at ...

Is there a way to use JavaScript or jQuery to identify if Firefox is blocking mixed content

Is there a way to detect when Firefox (or any browser) is blocking mixed content using JavaScript or jQuery? To learn more about Firefox's mixed content blocking feature, visit: The backstory: My company has an external vendor that handles the onli ...

What is the best way to incorporate animation using Tailwind CSS within my Next.js project?

I have a challenge with placing three images in a circular path one after the other using Tailwind CSS for styling. I've identified the circular path and keyframe style for the images. How do I configure this style in my Tailwind config file and imple ...

Toggle Form Section Visibility

I am working on a table with five rows that have upload buttons. Each time I submit a row, the page refreshes, but I want to hide all but one row at a time. When the first submit button is clicked, it should show the next row and hide the previous ones. Th ...

The functionality of Tailwind CSS Utility Classes is not functioning as expected

Currently, I am working on developing a replica of Airbnb using nextjs and tailwindcss within the VS code environment. Despite my efforts, I have encountered an issue where the utility classes are not functioning properly. The logo that was integrated in ...

What is the best way to create a seamless background transition for buttons on a mobile device?

Currently, I am working on my random quote generator project and I am trying to make it so that when the button is touched on a mobile device, the background color becomes semi-transparent. Below is the code I am using: #loadQuote { position: fixed; w ...

Animation will cease once the page is refreshed via Ajax

I'm facing a challenge with a problem that I can't seem to resolve. The issue lies in my use of this plugin for displaying random images. The only step left is to refresh the content within the div. However, every time I attempt to do so, the an ...

How can I display just the time portion of a date in AngularJS?

Hey everyone, I need assistance with displaying only the time value in AngularJS. Check out my Plunker I have fetched the time value using ng-repeat, but I want to display only the time value on my portal. I have tried to display the time value lik ...

Welcome to the NGINX Default Landing Page

In my NGINX server, the index page is located in subfolders rather than at the root of the site (e.g. /root/pages/htmls/index.html). How can I configure index.html to be the default webpage for the site? ...

Is there a way to retrieve the previous value in an input field's onChange event?

I am working with inputs in a React project and have assigned a function to their onChange event. While I have been able to access the current value, I am now looking for a way to retrieve the previous value as well. The reason I need the old value is bec ...

Steps to resolve Next.js Vercel deployment issue with missing modules

My next.js application was running smoothly on my local machine and Vercel, but now it encounters an error during the build process on Vercel. The specific error message is as follows: Despite attempting to resolve the issue by deleting node_modules and p ...

Viewing HTML in Gmail shows the raw code with tags, making it easy to read with no fancy CSS or styling

This is a simple HTML document with test content that was sent in the message body by a mailing server to a Gmail account: <html> <body> Some text goes here <br><br> User who applied: Username <br> User's email: < ...

In situations where there may be a duplicate, what alternative can I utilize in place of the id attribute?

I understand that almost any element in the DOM can have an "id" attribute, and I've used it to track each client in a table of clients. Although ids should not be repeated, my rows are assigned unique identifiers based on each person's "clientId ...