Avoid repeating media queries in MUI JSS by combining them efficiently

Is there a more efficient way to apply the same set of CSS styles to multiple media query rules without repetition? Currently, the only workaround seems to be:

[theme.breakpoints.up('xl')]: {
  color: 'red',
  backgroundColor: 'green',
  border: '1px solid blue',
  fontSize: 17,
  marginTop: 3,
  padding: 7,
  display: 'flex',
  flexDirecton: 'column',
},
[theme.breakpoints.only('md')]: {
  color: 'red',
  backgroundColor: 'green',
  border: '1px solid blue',
  fontSize: 17,
  marginTop: 3,
  padding: 7,
  display: 'flex',
  flexDirecton: 'column',
},
[theme.breakpoints.down('xs')]: {
  color: 'red',
  backgroundColor: 'green',
  border: '1px solid blue',
  fontSize: 17,
  marginTop: 3,
  padding: 7,
  display: 'flex',
  flexDirecton: 'column',
}

Answer №1

It's important to understand that the theme.breakpoints methods are not performing any magic -- they are simply convenient ways to generate media query strings.

For example, theme.breakpoints.down('xs') (using default breakpoints) translates to @media (max-width:599.95px), and theme.breakpoints.only('md') translates to

@media (min-width:960px) and (max-width:1279.95px)
. You can refer to the Material-UI code in the createBreakpoints function.

Understanding the syntax for achieving your CSS (and JSS) goals is the next step. Using commas, you can create an "or" condition with multiple media queries. The final string for your example would look like this:

@media (max-width:599.95px), (min-width:960px) and (max-width:1279.95px), (min-width:1920px)

To avoid hardcoding breakpoint values, you can construct the above string by concatenating the desired function calls with commas while eliminating extra instances of @media as it should only appear once at the beginning.

Here's a sample implementation:

// Sample code

Related answer: How can I use CSS @media for responsive with makeStyles on Reactjs Material UI?

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

Turn off the background when the automatic popup box appears

I have implemented a popup box that automatically appears after 5 seconds when the site is loaded. However, if I click outside of the popup box, it closes. I want to disable the background when the popup box is displayed. If I remove the two lines of code ...

Header 1 is not receiving any special styling, while Headers 2 through 6 are being styled accordingly

I am currently utilizing Skeleton V2.0.4 to design a landing page, but I seem to be having trouble with my styles not displaying correctly. Below are the specific lines of code related to it: /* Typography –––––––––––––– ...

animation for closing the hamburger menu

Having trouble creating a hamburger menu animation. I've set up two divs - one for the basic horizontal lines and one for the X icon. Using jQuery, I can hide and show them (clicking on the lines hides them and shows the X). But now I want to animate ...

Arranging items in the final row of a flexbox

, I am seeking a solution that goes beyond the repetitive ones provided before. I need to find a more efficient way to achieve my desired outcome based on the code snippet below. In examining the code, it's clear that the first row accommodates 6 ele ...

Tailwind refrains from using @apply classes during the build process and enforces a mobile-like viewport

Encountered a strange issue, let me try to explain it thoroughly. So, while working on my React project with Tailwind (bundled with Vite), there are instances when Tailwind suddenly stops functioning. It only compiles the classes used by my component libr ...

The state remains constant upon clicking

Below is the code snippet of my component: import React from "react"; import { useState } from "react"; import { Text, SvgContainer, Svg, Flex } from "../lib/styles"; import Modal from "./Modal"; const Credits = () ...

Unable to locate a selenium-recognized element within this field

I've attempted to use FindElementByName but I keep getting a no such element error when it should recognize this name. What am I overlooking? This issue is occurring on the website Intacct.com. Despite my efforts, I have been unable to find a unique C ...

HTML steps for smooth scrolling to the top and bottom of a list

My HTML contains a vertical list with top and bottom arrows positioned nearby. I am looking to implement a function where clicking on the top arrow will move the list up gradually, and clicking on the bottom arrow will move the list down step by step. Belo ...

Guidelines for setting the @param annotation to represent a React element when passing it as a function parameter

How can I resolve the error in IntelliJ WebStorm IDE for the @param block below related to the issue of Argument Type ComponentClass<undefined> not being assignable to parameter type ??? ... `? /** * Displays the specified component inside a div ...

Inline checkbox label with Bootstrap 5 styling

How can I align the checkbox with its label? Example <div class="row g-1 border-bottom p-3"> <div class="col border-end justify-content-center d-flex align-items-center"> <div class="h6 text-sm-center h-25&quo ...

React KeyCloak token renewal: expired refreshToken

I am struggling to update the Token in my React Application with Keycloak while trying to implement authorization in my client-side application. App.js import keycloak from "../../keycloak"; const App = () => { const handleOnEvent = asy ...

Is there a way to automatically override the CSS cursor style?

Issue with SCSS styling on image link I attempted to modify the cursor style from pointer to default, but after saving and reloading my React app, the change did not take effect. I tried writing some code, but it seems that Stack Overflow is indicating an ...

Interactive data table feature in React, transferring selected row data to a modal pop-up

I am currently developing an offline Progressive Web App (PWA) using ReactJS and have integrated the react-data-table-component, which has been very helpful so far. Within the table, I have implemented an onRowClicked function that triggers whenever a row ...

Distributing my React application packaged within an Electron app through GitHub releases

After successfully bundling my create-react-app with electron and packaging it, I encountered an issue when trying to publish my app on Github. The documentation states that files over 10 MB cannot be uploaded using Github releases. I have been unable to ...

Warning: Potential issue encountered when integrating NextUI theme with NextJS

Currently, I am working with the latest version of NextJS. As part of my project, I have implemented the next-theme to incorporate a dark mode switcher functionality successfully. However, when I utilized a Button component from NextUI, it triggered the ...

The MUI DateField activates leap year validation too soon when utilizing AdapterDateFns

One issue I've noticed is that the validation logic in a MUI DateField (version 6.4.0 of @mui/x-date-pickers) behaves differently depending on the date adapter being used (AdapterDateFns vs AdapterDayjs). When entering a date like February 29th, it re ...

The location of the footer is not aligned correctly

Recently, I encountered an issue that has left me puzzled. The problem is that the footer on my webpage is appearing between the calendar button and the calendar itself. Here is a snippet from my calendar.html file: {% extends 'base.html' %} {% ...

Error deploying to Heroku: PG::DuplicateColumn - Duplicate column found

I have a Rails API with Devise that I am looking to deploy on Heroku in order to integrate json files into a React app. However, during the migration process to Heroku, I encountered the following error: "PG::DuplicateColumn: ERROR: column "user_id" of r ...

Next.js is complaining that React has identified a rearrangement of Hooks despite being called from the top level

When using SWR to fetch data from my database, I encountered the following error message: Warning: React has detected a change in the order of Hooks called by OrganizationSettings. This will lead to bugs and errors if not fixed. For more information, r ...

Is there a way to arrange the cards in a row so they fill up the grid before moving on to the next row?

After retrieving data from a table in my database, I am displaying it on the screen as cards. However, the issue is that all the cards are displaying on the left side of the screen in a single column, rather than appearing in rows of 3 as desired. Below i ...