What is the best way to change the maxWidthLg of Material UI's .MuiContainer?

I need to remove the max-width that is set in the theme. When I look at it in Chrome and uncheck the option, it functions as desired:

@media (min-width: 1280px)
.MuiContainer-maxWidthLg {
    max-width: 1280px;
}

How can I achieve this? I have attempted something like this:

const useStyles = makeStyles(theme => ({
    root: {       
        '& .MuiContainer-maxWidthLg' : {
             maxWidth: //No matter what value I enter here, it does not work
        }, 

Unfortunately, it doesn't seem to be having any impact... How can I override this?

Thank you,

Alex

Answer №1

The maxWidth property in the Container component is set to 'lg' by default, but you have the option to disable Material-UI's control over the maximum width by specifying maxWidth={false}.

For example:

import React from "react";
import Container from "@material-ui/core/Container";
import Paper from "@material-ui/core/Paper";

export default function App() {
  return (
    <Container maxWidth={false}>
      <Paper>
        <h1>Hello CodeSandbox</h1>
      </Paper>
    </Container>
  );
}

https://codesandbox.io/s/container-maxwidth-ilv34?fontsize=14&hidenavigation=1&theme=dark

Additional reference: https://material-ui.com/api/container/#props

Code source: https://github.com/mui-org/material-ui/blob/v4.9.13/packages/material-ui/src/Container/Container.js#L88

Answer №2

On November 22, 2021, this is how the task was performed

<Container sx={{ maxWidth:'100%' }} maxWidth={false} >

Answer №3

If the default settings provided by Material UI are not suitable, you can customize the values below to better fit your needs.

    const theme = createMuiTheme({
      breakpoints: {
        values: {
          xs: ,
          sm: ,
          md: ,
          lg: ,
          xl: ,
          xxl:
        }
      }
    });
    
    export default theme;


//To implement in app.js

import { ThemeProvider } from "@material-ui/core/styles";
import theme from "style/theme";

 <ThemeProvider theme={theme}>
    //yourCode
 </ThemeProvider>

Answer №4

For those looking to tweak the lg breakpoint, this code snippet could come in handy. I'm referencing the global theme overrides guide found at: https://mui.com/customization/theme-components/

This solution is tailored for version MUI 5.4.3

import { createTheme } from "@mui/material/styles";

let theme = createTheme({});
theme = createTheme(theme, {
    components: {
        MuiContainer: {
            styleOverrides: {
                maxWidthLg: {
                    [theme.breakpoints.up('lg')]: {
                        maxWidth: // Add your custom value here
                    },

                    // Feel free to customize other properties such as padding, color, etc.
                }
            }
        }
    })

Answer №5

It is possible to accomplish the same result using makeStyles:

const customStyles = makeStyles((theme) => ({
  container: {
    [theme.breakpoints.up('xl')]: {
      maxWidth: <YourMaxWidth>px,
    },
  },
}))

Answer №6

To adjust the width of an element, you have two options: disable the maxWidth prop and modify the width using sx, or utilize the ThemeProvider to customize various width settings globally.

import Container from '@mui/material/Container';
import Link from '@mui/material/Link/Link';
import Stack from '@mui/material/Stack/Stack';
import { createTheme, ThemeProvider } from '@mui/material/styles';
import * as React from 'react';
import './style.css';

const theme = createTheme({
  components: {
    MuiContainer: {
      styleOverrides: {
        maxWidthMd: {
          maxWidth: 320,
        },
        maxWidthLg: {
          maxWidth: 500,
        },
      },
    },
  },
});

export default function App() {
  return (
    <ThemeProvider theme={theme}>
      {/* Customize component's width directly */}
      <Container
        maxWidth={false}
        sx={{ maxWidth: '200px'}}
      >
        <Stack spacing={2} direction="row">
          <Link>Home</Link>
          <Link>About</Link>
          <Link>Contact</Link>
        </Stack>
      </Container>

      {/* Use built-in maxWidth options with theme customization */}
      <Container maxWidth="md">
        <Stack spacing={2} direction="row">
          <Link>Home</Link>
          <Link>About</Link>
          <Link>Contact</Link>
        </Stack>
      </Container>
      <Container maxWidth="lg">
        <Stack spacing={2} direction="row">
          <Link>Home</Link>
          <Link>About</Link>
          <Link>Contact</Link>
        </Stack>
      </Container>
    </ThemeProvider>
   );
}

For more information on overriding MUI container widths, you can refer to this article:

Answer №7

After some trial and error, I was able to successfully apply the desired styles by placing the following code within a parent div:

'& .MuiContainer-maxWidthLg' : {
      maxWidth: '100%'
}, 

Answer №8

When working with the global approach, I found that I had to use "!important" in order to make it work:

const customTheme = createTheme({
    components: {
        MuiContainer: {
            styleOverrides: {
                root: {
                    maxWidth: '1440px'
                },
                maxWidthMd: {
                    maxWidth: 320,
                },
                maxWidthLg: {
                    maxWidth: '1440px!important',
                },
            },
        },
    },

I also encountered a situation where mui-emotion had a max-width: 1200 that needed to be overridden.

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 a paragraph from text inputs using React

I'm currently working on code that retrieves input from two textboxes - one for a string value and one for a number value. I want this data to be displayed in real-time within a paragraph without the need for a submit button. I've been struggling ...

How can I retrieve the position of a div or an image within a div (specifically the top and left coordinates) and incorporate these values into CSS?

I'm currently working on a website that is dynamic and utilizes bootstrap. I am looking to incorporate an animation where the dynamic thumbnails in the col-md-4 div zoom to 100% when clicked, appearing at the center of the container. I am struggling ...

Effortlessly transforming a massive JSON into an Array using ReactJS

I have a large JSON dataset containing information on over 2000 cities. I want to use this data in my React app, but first I need to convert it into an array. A similar question has been asked before, but I couldn't find any answers that fit my specif ...

Hiding elements in dark mode using `hidden` or `block` in Tailwind will not override its own hidden selector

To switch between dark and light mode for these two images, the dark:block selector does not override the hidden class. <div className="ml-5 content-center"> <img className="hidden dark:block h-6 w-auto my-1" src="/stati ...

How do you send a variable in a GET request with React?

My challenge is to retrieve data from a table where the teacherId matches the teacherId of the user who logs in, but I am facing difficulties in passing this teacherId from the front-end to the back-end. Below is the backend code: app.get("/api/get&q ...

Problem with Internet Explorer version 9 and above and the Flip Card feature

I have a cool feature for one of my clients where clicking on one div causes another div to flip in its place, like a card flipping over. It's kind of like those portfolio image flippers, but instead of images, it flips divs with content inside. How ...

Flexbox allows you to easily manage the layout of your website

I am currently working on a CSS project and have encountered an issue. Whenever I apply the "display: flex" property to the .student element, the border around it mysteriously doubles. The reason for wanting to use the flex property is to align the text ve ...

The "variable" used in the React app is not defined, resulting in a no

Following the tutorial of Mosh Hamedani, I attempted to create a react-app. You can watch the tutorial here. I followed his instructions exactly as mentioned. I encountered an issue when trying to pass an argument named product to a function called on on ...

Tips for updating the styleurls link dynamically by clicking a button and maintaining the changes

In the midst of developing an angular 2 project, I am currently working on integrating theme settings. Within my project, I have crafted three distinct component.scss files: red.component.scss, yellow.component.scss, and blue.component.scss. My goal is t ...

To ensure the specificity selector for material UI in React, it is essential to include an empty CSS definition

The styling for the unselected toggle button is working smoothly. However, when you don't define an empty class selector, the style of the selected toggle button does not show: ./App.js import * as React from "react"; { render ...

What is the best way to utilize useEffect solely when the length of an array has

Is there a way to trigger a state update only when the length of the prop "columns" changes? useEffect(() => { if (columns.length !== prevColumns.length) { // update state } }, [columns]); Any suggestions on how to achieve this? ...

Guide to creating a Stencil-powered React library using NX, ready for publishing on npm

I am working with a NX monorepo that has been set up with Stencil. Within this setup, I have created Stencil web components and generated a React library using Stencil. Using the React library in a React app within the same repository works just fine. Now ...

Managing dependencies in React: how to efficiently use useEffect without redundancy?

Is there a way to remove the dependency on the auth state in a React hook, specifically when using useEffect? I'm looking for a solution that avoids redundancy and the need to create another similar hook. export const useHookAuth = () => { // u ...

Shifting the hover effect to a click event with ReactJS

Encountering an issue with hover and active states on mobile. Here is the link to my code: https://codesandbox.io/s/inspiring-wozniak-33l61?file=/src/Portfolio.scss I am attempting to change the background color of containers when tapped on mobile devices ...

Inline-block divs styled with CSS3 are causing a white gap to appear at the top of

I have been searching for solutions to my problem, but I can't seem to find one that works. My goal is to create two columns that each take up 50% of the screen width. However, I am facing an issue where there is a gap at the top that I cannot seem t ...

Removing the Shadow from Material UI's Dialog Box

I am currently struggling with the Material UI dialog component that displays information about a location marker. My issue is with disabling the unattractive box shadow that surrounds the component. Despite setting box-shadow: none and trying different so ...

Encountering a NextJS error when attempting to access a dynamic route

I am currently working on a Next.js application that involves dynamic routing. I have encountered an error message stating: Error: The provided path X0UQbRIAAA_NdlgNdoes not match the page:/discounts/[itemId]`.' I suspect that the issue may be relat ...

Arrange pictures and div elements in a horizontal layout

I am facing an issue with my code that is displaying 5 'cards' in a messed up layout instead of a straight line. I have tried to align them vertically, but some are still higher and not in the right positions. For reference, I want the layout to ...

Component does not display dynamically created DOM elements

I have a function that creates dynamic DOM elements like this: const arrMarkup = []; const getMarkup = () => { if (true) { arrMarkup.push( <Accordion expanded={expanded === cust.name} onChange={handleChange(cust.name)}> ...

React: Switching PopUp causes the entire component to be re-rendered

Currently, I am in the process of familiarizing myself with React, so I appreciate your patience. I am developing a component using MaterialUI which consists of a grid and a PopOver. A basic mockup of this component is as follows: export const Overview ...