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

Ensuring a div remains perfectly centered

Currently, I am new to HTML and still in the process of learning. I am facing a challenge in centering a button within a div, and I have been using margins to position it. While this method works well when the window is fullscreen, the button's positi ...

What is causing the section to cover the navbar?

My content is overlapping the navbar on certain screen sizes, and I'm not sure why. Any ideas on how to fix this issue? Would wrapping everything in a container help? Currently using bootstrap v5.3.0-alpha1. Additionally, when I have a collapsed nav ...

I am trying to fetch images from Google Drive in React, however, I keep encountering a Cross-Origin Read Blocking (CORB) error

I am working on a web project using React. I am trying to display images stored in Google Drive from my React application. Unfortunately, I'm encountering a Cross-Origin Read Blocking (CORB) error. What steps should I take to resolve this issue? ...

Creating a centered and beautifully styled picture with a specific maximum size using React

I recently completed the development of a new website, which can be viewed at Upon inspection of the website, it is evident that the photo is not centered and appears too large on mobile phones. Despite my efforts to align it using various methods outline ...

Tips for customizing autocomplete/autofill in Material-UI Autocomplete with ReactJS

I have implemented material-ui autocomplete ( https://mui.com/components/autocomplete/#free-solo ). Everything seems to be working well, you can see the demo here : https://codesandbox.io/s/0v9v9?file=/demo.tsx By default, the Text input is displaying au ...

Sliding panels with Jquery

I've created some basic panels that slide in and out horizontally. My goal is to hide all content except the first panel when the page loads. When a link to other panels is clicked, the current content will slide left to be hidden and new content will ...

What is the method for changing the color of the bottom border from blue to green in a select field with React JS?

I am currently utilizing the select field component from material UI react Upon selecting an item from the field, I encounter two specific issues: The border bottom line turns blue and the background color changes to gray. My objective is to modify the ...

Error while fetching state in Material UI/Enzyme Test?

I am currently facing an issue with setting the state in my component's constructor. It seems that despite following the documentation, the state is not being properly set on the ReactWrapper. The mounting process should render the component and subc ...

Issue encountered: Trying to utilize the <Link> component outside of the <Router> component in React three fiber and react-router-dom, resulting in an error message

Someone told me that react-router-dom cannot be used with react-three-fiber because of technical issues involving the provider not being accessible inside the canvas. I attempted to place a Link tag outside of the canvas, and while it does redirect to anot ...

Encountering an issue while trying to verify user registration in MySQL using Node.js Express. During the user registration process, an error arises indicating that 'res' is not defined

import React from 'react' import { Link } from 'react-router-dom' import {useNavigate} from 'react-router-dom'; import { useState } from 'react' axios from "axios" const Register = () => { const [i ...

Create a Conditional interface that is capable of receiving multiple sets of API data based on the path name

Is there a way we can dynamically assign different types in ReactJs using TypeScript based on the current pathname? For instance, if the pathname is A then use interface Orders[], if it's B then use setState with Locations interface to manage a single ...

"Ascend beyond the constraints of fixed measurements with dynamic

My JQuery image fader has a width of 100% and height set to auto. I'm thrilled that it works perfectly on the iPhone as well. Now, I am facing a challenge while trying to make content "float" after my JQuery slider. The problem arises because I use ...

What is the purpose of encasing the routes within the <Switch> element in react-router, when they seem to function perfectly fine without it

Currently utilizing react-router-dom version 5.2.0. As a practice, I typically enclose all my Route components within a Switch component. For instance: const MyRoutes = () => ( <Switch> <Route path='/route1' exact ...

Issue with routing in Next.js 13.4 - tailAdmin interface

I recently utilized a template from https://github.com/TailAdmin/free-nextjs-admin-dashboard. However, I'm encountering a 404 error when trying to access my new blog page. The path causing the issue is "/app/blog/[id].tsx" import React from "r ...

Using dynamic jquery to target specific elements. How can we apply jquery to selected elements only?

Hello everyone! I have been working on a simple hover color change effect using jQuery, but I noticed that I am repeating the code for different buttons and service icons. Is there a way to achieve the same result so that when a button is hovered, the co ...

Having trouble with data types error in TypeScript while using Next.js?

I am encountering an issue with identifying the data type of the URL that I will be fetching from a REST API. To address this, I have developed a custom hook for usability in my project where I am using Next.js along with TypeScript. Below is the code sni ...

What could be the reason behind my effect executing multiple times, gradually decreasing the application's performance with a few interactions?

I have implemented this code, but I am encountering a problem with receiving multiple console logs. Can someone help me identify what I might be doing incorrectly here? I attempted to utilize useCallback and useMemo, but it's possible that my implemen ...

What are the consequences of incorporating getInitialProps within _document.js that leads to a crash in the Next.js application?

What are the implications of using getInitialProps in _document.js for static rendering? How can it potentially impact the overall static rendering process? Consider the following scenarios: A small e-commerce website with a mix of static and dynamic pa ...

Unable to position divs next to each other in Firefox, yet successfully achieved in Chrome

I have successfully placed two divs side by side using the following markup, as shown in image 1, but this layout only works on Chrome. However, Firefox renders it differently as seen in Image 2. Is this a known issue that I overlooked? How can I resolve t ...

Fade one element on top of another using Framer Motion

Looking to smoothly transition between fading out one div and fading in another using Framer Motion, but facing issues with immediate rendering causing objects to jump around. Example code snippet: const [short, setShort] = useState(false); return ( ...