How can I stop MUI Button from automatically becoming fullWidth?

One of the challenges I encountered was styling a Button component from MUI:

export const ButtonStyle = styled((props) => <Button {...props} />)(({ theme }) => ({
    marginTop: 8,
    marginBottom: 8,
    width: 'auto',
    borderRadius: 3
}));

When I tried to create a Button using:

                    <DivStyle>
                        <Typography variant="h5"> Change Your Password </Typography>
                        <ButtonStyle variant="outlined">
                            Update Password
                        </ButtonStyle> 
                    </DivStyle>

where DivStyle is defined as:

export const DivStyle = styled('div')(({ theme }) => ({
    display: 'flex',
    width: '90%',
    minWidth: 300,
    [theme.breakpoints.up('lg')]: {
        width: '70%'
    },
    flexDirection: 'column',
    paddingBottom: 0
}));

The issue arose when the Button appeared full-width in the parent div, contradicting the default setting of fullWidth=false mentioned in the MUI documentation.

I am curious as to why this inconsistency occurs even though the button should not be taking up the entire width.

Answer №1

Since the Button is nested within a flex column container, it automatically adjusts its width to fill the space of its parent container. This behavior can be seen in the following example:

One solution would be to change the DivStyle element to a standard block element.

Another approach could involve wrapping the Button in a simple div. In this case, the flex-basis property would apply to the div instead of the button itself.

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

Utilize Context to update state instead of using setState in NextJS

For my first attempt at using NextJS and a headless CMS (specifically DatoCMS), everything seemed to be working well initially. However, I noticed that I was relying heavily on prop drilling to pass information from the CMS down to deeply nested components ...

Disabled text selection in Internet Explorer

I am in need of making one HTML element (Label) unselectable for IE. I have tried using the following methods: Unselectable=on onselectreturn=false; Unfortunately, none of these solutions are working for me. For Firefox and Chrome, I've successful ...

Issues arise with tabbed content as default content fails to display and classes are not added upon clicking

I'm currently working on a tabbed module that consists of three tabs. Here's how it operates: When the user clicks on a carousel_element, it reveals carousel_hidden-text within that div and displays it in another div called carousel_quote. I&ap ...

Error message in Typescript: "Property cannot be assigned to because it is immutable, despite not being designated as read-only"

Here is the code snippet I am working with: type SetupProps = { defaults: string; } export class Setup extends React.Component<SetupProps, SetupState> { constructor(props: any) { super(props); this.props.defaults = "Whatever ...

Troubleshooting a vertical overflow problem with Flexbox

Struggling to design a portfolio page for FreeCodeCamp using flexbox layout due to vertical overflow issues. Here is the HTML: <div class="flex-container flex-column top"> <header class="flex-container"> <h1 class="logo"><i clas ...

Error 404: Unable to locally install node.js npm due to ENOENT chmod

I ran into issues while attempting to execute npm install on a node/react application that I have taken over for development purposes. I am not looking to globally install any packages. My setup involves using an M1 Macbook Pro with Big Sur. I installed n ...

Unable to integrate React-Bootstrap with Expo due to an error resolving the module react/jsx-runtime

Working on a react-Native app with Expo, I decided to utilize components from the bootstrap library. I kicked off a new project: expo init test-app Next, I integrated the library into the project folder: expo install react-bootstrap@next <a href="/cdn- ...

Upcoming construction: Issue encountered - The Babel loader in Next.js is unable to process .mjs or .cjs configuration files

Within my package.json file, I have set "type": "module" and "next": "^12.2.5". In my tsconfig.json: { "compilerOptions": { "target": "ES2022", "module": "esnext ...

Error 504: GATEWAY_TIMEOUT - Vercel hosting running NextJS

I've encountered a frustrating issue with Vercel and NextJS that's causing me some headaches. My NextJS application has simple api routes, but when I try to access my hello world endpoint (or any other endpoint), I'm faced with the following ...

Issues with the responsive prop values have been detected in MUI v5 and are not functioning as expected

How can I customize my button to be small-sized on mobile view and large when the screen hits the MD breakpoint? Current button styles can be found here. I attempted using size={{ xs: "small", md: "large" }} but it did not produce the ...

I am having trouble connecting my CSS file to my EJS file

I'm having trouble including my external main.css file in my header.ejs file. I've designated my CSS file as public in my backend Node.js with Express.js server (index.js) using this static code: app.use(express.static("public")); The ...

Is it possible to create an index.html page with all the necessary head tags to prevent duplicate code across multiple html pages?

Imagine I am using app.js or a Bootstrap CDN link for all of my HTML pages, but I don't want to include it in every single page individually. Is there a way to create an index.html file that includes this so that all other HTML pages load the head fro ...

Difficulty in vertical alignment of inline-block elements

Greetings, I am currently working on creating a Resume in HTML. However, I seem to be facing an issue with the inline-block property as the two div elements that should be placed next to each other are not displaying as expected - one of them appears sligh ...

Utilizing the Authorization Header in WebSocket within a React Electron Application

Struggling to establish a connection with a secure websocket that requires Bearer Auth via Header. Despite popular advice, it seems setting headers for WebSockets is not straightforward. How can I achieve this in a React Electron App? Currently using the & ...

What is the correct way to establish and oversee environment variables for my React project?

In order to manage different environments for my Next.js project, I have created three environment files: .env.local, .env.dev, and .env.prod. Each file contains similar environment variables tailored for different stages (local, development, and productio ...

Using TypeScript, what is the best way to call a public method within a wrapped Component?

Currently, I'm engaged in a React project that utilizes TypeScript. Within the project, there is an integration of the react-select component into another customized component. The custom wrapped component code is as follows: import * as React from " ...

Utilizing Global Variables in a ReactJS Application with SASS

Currently delving into the world of ReactJS with SASS integration in my project. Seeking a way to house all variables and mixins in a single file that can be accessed globally by other SCSS files without the need for manual importing. Any solutions to achi ...

Modify the color of text in Bootstrap navigation bar

I am struggling to adjust the text color within my bootstrap links. I attempted the method mentioned in this post, but unfortunately it did not work for me. Can someone please guide me on how to successfully change the text color? Thank you. You can view t ...

The "webpack --watch" command stops running after completing a single build

Previously, I relied on the webpack --watch command to keep my webpack running in watch mode for my ReactJS app. Unfortunately, it seems to have stopped working abruptly. Now, it only compiles the code once and then stops. I attempted the solutions mentio ...

The PurgeCSS CLI does not generate CSS files beyond the command line interface

I'm struggling to successfully extract my CSS using purgecss from the command line and save it to a separate file. The instructions on PurgeCSS.com are vague: By default, the CLI only outputs the result in the console. To save the purified CSS files ...