Utilizing pixel values for Material-UI breakpoints rather than using the default sm, md, lg, xl options

Below is the code snippet that I am using:

[theme.breakpoints.only('lg')]: {}

The above code works perfectly and shifts at the desired breakpoint. However, when I implement the following:

[theme.breakpoints.between('1200', '1021')]: {}

It seems to not function correctly on those specified points. I am curious about how to utilize actual numerical values in the breakpoint section so that mui can recognize it without the need to create a new breakpoint specifically for this purpose.

Answer №1

The theme.breakpoints.between function now allows you to pass numbers instead of breakpoint keys, but make sure to follow the correct order. The lower pixel value should come first.

Your current code is producing:

@media (min-width:1200px) and (max-width:1020.95px)

This won't work because the minimum width can never be higher than the maximum width.

If you switch the values and use

theme.breakpoints.between(1021, 1200)
, you will get something that works better:

@media (min-width:1021px) and (max-width:1199.95px)

It's important to note that theme.breakpoints.x functions are simply shortcuts for creating media query strings. You can also directly specify the media query in JSS styles as shown below:

import React from "react";
import { makeStyles, useTheme } from "@material-ui/core/styles";

const useStyles = makeStyles({
  myCustomClass: {
    "@media (min-width:600px) and (max-width:1199.95px)": {
      backgroundColor: "green"
    },
    "@media (max-width:599.95px)": {
      backgroundColor: "blue"
    },
    "@media (max-width:900px)": {
      color: "yellow"
    }
  }
});
export default function App() {
  const theme = useTheme();
  const classes = useStyles();
  return (
    <div className={classes.myCustomClass}>
      theme.breakpoints.between("1021", "1200"):{" "}
      {theme.breakpoints.between("1021", "1200")}
      <br />
      theme.breakpoints.between("sm", "md"):{" "}
      {theme.breakpoints.between("sm", "md")}
      <br />
      theme.breakpoints.only("md"): {theme.breakpoints.only("md")}
    </div>
  );
}

https://codesandbox.io/s/breakpoints-bzgjd?fontsize=14&hidenavigation=1&theme=dark

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

jQuery can be used to mask phone numbers with three input fields

I am looking to implement phone field masking using jQuery or JavaScript. I have tried a few jQuery mask plugins, but I am still searching for a better solution. Specifically, I need three input fields for the USA (it is a requirement). The inputs needed ...

Master the art of filtering data arrays with multiple checkboxes in React

I am looking to filter data by selecting different checkboxes using the codes provided below: const movieData = [ { title: "movie 0", genre: "" }, { title: "movie 1", genre: ["action", "thr ...

Click event not functioning in programmatically loaded HTML

I am facing an issue with a JSON file that contains the page content I am trying to load. The link within it appears as follows: <a data-ng-click='foo()'>Bar</a> When I load this page content into the HTML page: <p class="body" ...

Unexpected labels continue to pop up in the footer of the HTML file

I have noticed that the following HTML always appears at the very bottom of the body tag in all my projects, regardless of whether I am using React.js or not. Interestingly, when I switch to an incognito browser, these tags disappear. Curiously, these sa ...

modify the color of text in a row within a jquery ajax table

Is it possible to change the font color of values in a row based on a condition inside a function? Specifically, if the TotalStudent count exceeds the room capacity, can we add student information to the table with red font color? Below is my attempt using ...

The functionality of Blur is malfunctioning on Android with @react-native-community/blur

When using BlurView on Android, elements do not position correctly and end up overlapping each other. This issue does not occur on iOS devices. Please refer to the screenshot below for a visual representation. https://i.stack.imgur.com/NODDk.jpg Sometime ...

Stylis-plugin-rtl for Material-UI V5 ensures that your application is fully compatible

I am using Material UI 5 with Next.js and have followed all the steps outlined in the documentation here, along with the Emotion and Stylis-plugin-rtl v2: https://next.material-ui.com/guides/right-to-left/#heading-jss However, after refreshing the page, ...

Recognizing JavaScript in Intellij IDEA within a script tag that does not specify the type as "text/javascript"

Using the MathJax javascript library has presented a challenge for me. Whenever I make changes to the MathJax configuration, I encounter issues because it is written in javascript but its type is labeled as "text/x-mathjax-config". This causes Intellij Ide ...

Is there a way to filter out only the objects from the JSON data and exclude the strings?

I am facing an issue while looping through a JSON object. The presence of strings in the JSON is causing the loop to fail. How can I iterate only through the objects in the JSON without affecting the loop? My main goal is to iterate through the objects co ...

Is there a way to dispatch an event from one Angular ui-router view to another view?

In order to change the login button to display "logged in as xxx" after authentication, I have structured my page into three views: header, content, footer. The login button is located in the header view. Upon clicking login, it transitions to the "app.log ...

How to Maintain SASS Code Efficiency with Media Queries

How can I avoid repeating myself in my CSS code when using two different media queries for phones and tablets that require the same exact styling? $mobile-portrait: "only screen and (max-width: 680px)"; $tablet-portrait: "only screen and (min-device-wid ...

JavaScript object conversion to Java Model is proving to be a challenge

When looking at my Spring controller and client-side Javascript code, I noticed that the Javascript object is having trouble reaching the Spring controller in Object form. Here is a snippet of my Controller code: @RequestMapping(value = "/addRating", meth ...

Pausing briefly after selecting an element with Webdriver

I have been attempting to highlight an element on a webpage for a specific duration of time, approximately 5-6 seconds. However, I am facing difficulty in making the highlight persist for the desired duration as it currently flashes briefly. Despite using ...

How to customize the button color in a Next.js application

Help Needed: Issue with Changing Default Button Color in Next.JS Web Application. The button text color appears as grey on Desktop Google Chrome, but shows up as blue on Mobile Chrome browser. I want to unify the color to be grey on both platforms. Custo ...

How can I reduce the additional space inside a MUI Button component?

I'm struggling to create a small button without any white space inside it. I've attempted various solutions such as setting a maximum width, and adjusting the margins and padding to zero, but unfortunately, none of them have resolved the issue. & ...

Updated: "Adjust the preserveAspectRatio attribute in the SVG element within the Lottie player"

I have successfully incorporated a lottie json file using the lottie player. Unfortunately, I do not have access to the SVG file itself, only the json file. My goal is to modify the preserveaspectratio attribute of the SVG to xMinYMax meet instead of xMi ...

How can I incorporate arithmetic operators within a function in EJS?

Currently, I am developing an express app that includes a booking form using ejs with added functionality for payment processing. In the code, I have a select tag where the selected text is stored in a variable. Although console logging shows the value co ...

The issue of onClick failing to function when paired with the addEventListener for the

Looking into a react component for a profile button that opens a menu with three options: My Profile, Settings, and Logout. The issue seems to be with the onClick event on the a tags not working as expected (the console.log is not being printed). Interes ...

Is there a more efficient approach to streamline Javascript code similar to the chaining method used in JQuery?

When working with jQuery, it's possible to streamline the code by running multiple methods in a single statement: $("#p1").css("color", "red").html("Hello world!").attr("class","democlass"); But how can this be accomplished in Javascript? document. ...

Issue with directive not activating when attribute is changed

I am facing an issue with my website where users can make selections from two dropdowns, and based on those values, attributes are sent to directives for a corresponding function to be called. The problem I'm encountering is that the directives are n ...