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

I am looking to apply the flex-direction style to the Material UI Grid component

Looking to set the direction property for a <Grid container> component in the Material UI framework. After reviewing the documentation, I attempted setting the direction property as column with the following code snippet: <Grid container directio ...

Concealing overflow for text content through CSS styling

I am currently working with an element that contains both an image and text. View the Fiddle here Note: To see the full grid, resize the preview accordingly. The CSS I have written is as follows: .gridster .gs-w .item{ position: relative; wi ...

Liquid 960: Bizarre Alpha/Omega Phenomenon

Currently, I am working with Fluid 960 and encountering some unexpected behavior with alpha/omega. Typically, I utilize alpha/omega to adjust left and right margins within nested grids. Oddly enough, when I use alpha/omega on a set of nested grids, the gr ...

A specialized identifier for nested objects in a React component

I am currently working with a JSON data structure that looks like this: [ [ { city: x patients: x id: 1 }, { city: y patients: y id: 2 } ], [ { city: x patients: x id: 1 }, { city: y patients: y id: 2 } ...

Firefox not rendering responsive YouTube embed properly

This method of embedding seems to be functioning well on all browsers except for Firefox; I took advantage of a free trial at crossbrowsertesting.com to verify. I’m not using a direct iFrame embed, and all the solutions I’ve come across are related to ...

The color of the top bar remains unchanged when hovered over

I am struggling to design my personal website, and I can't seem to get the top bar to change color on hover. If you believe you have a solution, please provide your answer. index.html appears to be functioning properly, but perhaps additional adjustm ...

Replacing CSS conditional statements with jQuery

My CSS code is set to hide the #global-widget-base tag and display a tab #aside-expander when the browser width is less than 1200px. @media screen and (max-width: 1200px) { aside#global-widget-base { display: none !important; } #aside- ...

Can the react admin pagination for the List Component be customized to include an input that allows users to select their desired page number?

Is it possible to override the standard Pagination in React Admin's List Component and replace the "..." with an input field where users can enter a page number directly instead of clicking on buttons? After some research, it seems like this may not ...

What is the best way to prevent updating the state before the selection of the end date in a date range using react-datepicker?

Managing user input values in my application to render a chart has been a bit tricky. Users select a start date, an end date, and another parameter to generate the chart. The issue arises when users need to edit the dates using react-datepicker. When the s ...

Utilizing References in React Components

One of the challenges I am facing involves a Container that needs references to some of its child components: const Container = () => { const blocks: HTMLDivElement[] = []; return ( <div> <Navigation currentBlock={currentBlock} ...

When hovering over the sidebar, it smoothly slides out. (ideally using only CSS)

I've been spending my free time working on a website, but I'm struggling to figure out how to create a navigation similar to the one featured on FontShop's site: . I want it so that when you hover over their logo (or in my case, a blank rect ...

When using the react-table library, the useState hook within a table always defaults to its initial value even if

I have set up a React application using react-table to organize and display fetched data in a table format. The table includes various buttons for interacting with the data, such as deleting entries or updating information (e.g., toggling approval status). ...

Changing the appearance of a shadow root element using CSS styling

My CustomField has a FormLayout inside, and I want to change the #layout element within the shadow-root. While it can be done with JavaScript: document.querySelector("#myid > vaadin-form-layout") .shadowRoot .querySelector("#layout" ...

What strategies can be used to effectively structure CSS and JavaScript in a project for optimal organization?

In my NetBeans project, I currently have a large web project with CSS files included in the header. Some CSS codes are needed on all pages, while others are only necessary for specific pages. I am looking to optimize high-traffic pages by removing any ...

Installing dependencies for Material-UI using npm

I just started my React and NPM journey. After setting up a new directory, running npm init, and then npm install to install React, here is how my configuration in package.json looks like: { "name": "reactapp", "version": "1.0.0", "description": "", ...

CSS background property does not function properly if the URL contains brackets

Here is the URL for an image I am working with: URL: Currently, I am having an issue in my CSS: background: url(http://www.andrearosseti.cl/image/cache/data/New%20Coleccion/Planas/SWEET-5-TAUPE-(1)-340x340.jpg) The problem arises due to the presence of ...

Having trouble with running npm install after updating node js?

Images displaying error messages First Image Second Image Third Image Version of Node.js ...

"Learn the process of toggling the visibility of multiple objects by passing props from child components to parent components and

My approach to design is quite straightforward. I have a sidebar in child components (Aside.js) and a Main object that displays content (Main.js), all of which are used in ImportPage. In Aside, I have three buttons that I want to use to show and hide conte ...

The function getServerSideProps is not defined in the NextJS environment

I am facing an issue where I need to pass an array called initialPosts into my Home function using getServerSideProps, but the props are showing up as undefined. Despite trying various solutions, none of them seem to work. In getServerSideProps, I have us ...

You can slide one cell at a time using Swipeable in react-native, and when you swipe to open another cell, the previous one will close

Recently delving into react native and exploring the Swipeable feature from "react-native-gesture-handler/Swipeable". Encountering an issue where opening the second item leaves the first item still showing open. The desired functionality is to only have on ...