What is the best way to adjust the background color of Material UI's theme based on different screen sizes?

Having trouble replacing hard-coded CSS breakpoints with Material UI theme settings.

@media screen and (min-width: 0px) and (max-width: 400px) {
    body { background-color: red; }
}
@media screen and (min-width: 401px) and (max-width: 599px) {
    body { background-color: blue; }
}
@media screen and (min-width: 600px) {
    body { background-color: green; }
}

Trying to implement the same breakpoints in a Material UI theme but running into issues. Here's what I've attempted so far:

const defaultTheme = createMuiTheme();
export const theme = createMuiTheme({
    palette: {
      background: {
        [defaultTheme.breakpoints.down('sm')]: {
            default: 'red'
        }
      },
    }
});

Not sure if this approach is supported by Material UI. Any insights would be appreciated.

Answer №1

To understand how this code snippet utilizes theme.palette.background.default, take a look at the link provided. The syntax you attempted will simply create a new property in theme.palette.background that Material-UI won't recognize.

An alternative approach would be to define custom entries in your theme like so:

const theme = createMuiTheme({
  palette: {
    background: {
      xs: "red",
      sm: "blue",
      md: "green"
    }
  }
});

You can then utilize these customizations in your styles:

const styles = theme => ({
  "@global": {
    body: {
      [theme.breakpoints.up("md")]: {
        backgroundColor: theme.palette.background.md
      },
      [theme.breakpoints.down("sm")]: {
        backgroundColor: theme.palette.background.sm
      },
      [theme.breakpoints.down("xs")]: {
        backgroundColor: theme.palette.background.xs
      }
    }
  }
});

For more detailed implementations, check out this CodeSandbox example.

For a related query, refer to this post: Overriding default styles on divs, p's, and body with createMuiTheme

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

Creating an engaging layout with Bootstrap 3: Utilizing grid systems for video integration

I am facing an issue with resizing multiple videos within a Bootstrap grid. The problem is that the column size increases, but the video itself does not resize accordingly. In my CSS file, I have set the width and height of the gif-video class to 300 for ...

Encountering a ReactJS error while trying to POST to the recently created MySQL table

After setting up two tables in my Mysql database, I encountered an issue when working with the second table. Everything was running smoothly with just one table, but as soon as I introduced the new table, problems started to arise. The command "node serve. ...

Having trouble navigating users once they are logged in on ReactNative

I'm working on a login component using Meteor, but I'm facing an issue with redirecting successful users to another screen called 'Home'. The error message I'm receiving is: "undefined is not an object (evaluating '_this2.prop ...

Seeking assistance with installing Node version >14 on Windows 8. Any guidance would be greatly appreciated

Currently running node version 13.14.0, but in order to utilize the latest create-react-app version v5.0.0, I must upgrade to node version 14 or higher. Unfortunately, this is not supported on Windows 8. Any suggestions for a workaround? Encountering an e ...

HTML Techniques: Flipping the Script - Writing Characters from Right to Left

In order to showcase the temperature in degrees Celsius, I have implemented a placement technique using absolute positioning. Consequently, the °C symbol will persist in its designated spot. To achieve the desired presentation, the text should be displaye ...

Manipulate the contents of children divs within a parent div using JavaScript or JQuery

<div id="abc"> <div id="a_b"> abcd </div> <div id="c_d"> xyz </div> </div> I have a challenge where the divs on my page are generated dynamically and their IDs change every time the page loads. When the window i ...

Is it possible to use bootstrap to hide specific components depending on the size of the media screen?

Struggling with styling responsiveness and hoping to find a Bootstrap class that can easily hide components on medium and small screens. I'm looking for a way to hide headings, paragraphs, buttons, and more without any extra hassle. ...

Creating a cutting-edge UI with MUI 5 by utilizing styled components and passing props to

Currently in the process of transitioning from MUI 4 to 5. The focus is on shifting from the use of makeStyles() to styled components. I am looking for an example that showcases the implementation of the styled() method with a component containing child co ...

Exploring Alternative Methods to Navigate Through Elements Using JQuery UI Autocomplete

After testing two different approaches: <div id = "team-search-container"> <label for="team-search" class = "text-center"> <input type = "text" id = "team-search"> </label> </div> With the following code sni ...

What is the best way to set the theme for Material-UI in a React application?

I find myself puzzled when it comes to styling a front-end based on Material-UI. Can someone clarify where the theme originates from in the following example code and what impact the theme has? import React from "react"; import Container from "@material- ...

Element in list displaying a distinct alignment from the rest

I currently have a navigation bar based on a list, which looks something like this: https://i.stack.imgur.com/e1Dmb.png My concern is how to position the "Logout" item on the right side of the screen. I've tried adding clear list items but that seem ...

Height in Material-UI CardMedia

I'm having trouble adjusting the height of the image inside the CardMedia component. I have defined the styles like this: const style = { height: 32, }; and I am applying it to the CardMedia component like so: <CardMedia overlay={<CardTi ...

Effortlessly find data in an HTML table and showcase the results instantly without

I am looking for a way to implement search functionality in my table without refreshing the page. The fields above the table are used for searching and I want the results to be displayed within the same table. Here is an example of the code: <?php $for ...

Could react-router be causing issues with the API endpoint?

My API is functioning correctly, but when used with React, the get request is directed to /api/profile/posts/profile/matt instead of where it should go: /api/posts/profile/matt The proxy is set on localhost:8000/api, which works fine for other APIs when ...

Issues arise when implementing ReactJS transitions for progress bars

As a newcomer to the world of ReactJS, I have been exploring the FB documentations and online tutorials to kickstart my test project. Within this project, I wanted to implement a progress bar that visually represents the user's progression through fi ...

Investigating Jest function calls following a button click while simulating fetch calls

Currently, I am in the process of executing Jest test suites for a React SPA. Specifically, my focus is on writing tests for the sign-in page. Despite having several functions that should execute upon receiving a 404 error after a sign-in attempt, I am str ...

Resize the shape of an image's polygon

Is there a way to independently scale a specific polygon of an image on hover? For example, I have a world map and I would like to enlarge a country when hovering over it, then return it to its original size when no longer hovering. I am familiar with us ...

Can a single project update just one node?

Currently, I am facing a challenge with a large project that uses an outdated version of Node.js which is globally installed on my computer. I intend to kick off a new project using npx create-react-app, but this requires me to upgrade Node.js. I am aware ...

Ending the Overlay

I am using an overlay: <div id="overlayer" class="overlayer"> <div id="board" class="board"></div> </div> Here are the CSS properties I have applied to it: #overlayer { position:fixed; display:none; top:0; left:0; width:100%; hei ...

What could be causing the undefined status of this length function?

I need to determine the length of seconds. If the second is only one character long, I want to add a 0 in front of it. Otherwise, no action is needed. Below is my code: <p>Click the button to show the current number of seconds.</p> <p id= ...