Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize a material-ui button

I have this code here :

I am trying to have 3 small square buttons with + and - sign and one in the middle with a digit. I am using material. The buttons now are rectangular and too big for my app. My problem is I just can't have them square and resize them. I have tried a lot of things but it doesn't work. Thanks

     <Grid item>
        <Button onClick={this.up} variant="outlined">
          <Add color="secondary" />
        </Button>
        <Button style={{ padding: "11px 0px" }} variant="outlined">
          {this.state.quantity < 1 ? 0 : this.state.quantity}
        </Button>
        <Button onClick={this.down} variant="outlined">
          <Remove color="secondary" />
        </Button>
      </Grid>
like image 454
Charlote22 Avatar asked Jun 26 '18 09:06

Charlote22


People also ask

How do I change the button size in material UI?

To resize a React Material UI button, we set can the style prop of the Button to an object that sets the dimensions of the button. We add a button by adding the Button component. And we set the style prop of the button to an object that has the maxWidth , maxHeight , minWidth and minHeight properties.

How do I change the size of a button in react?

Button size To change the size of the default Button to small Button, set the cssClass property to e-small .

Why is material UI so hard?

The interface of the theme itself is different, but that seems to be about it there. Material UI implements Material UI by default and this is the main reason why it's hard to customize. Too much to change. I guess this is one of the reasons why there are not that many custom themes available for this library.


Video Answer


3 Answers

You could add max/min width/height style options.

For instance:

<Button style={{maxWidth: '30px', maxHeight: '30px', minWidth: '30px', minHeight: '30px'}}/>

In this case button always will look like a square and have a fix size (30px).

like image 60
Mikhail Katrin Avatar answered Oct 19 '22 02:10

Mikhail Katrin


I assume you have a <Grid container> around the elements you posted. MUI Grids are designed to fill up the space and manage column widths. Seems like you probably need to restrict the outer <Grid container> to the total width you want the columns to span.

Also, note if you want to override all <Buttons>, do so in the theme...

createMuiTheme({
  overrides: {
    MuiButton: {
      outlined: {
        borderRadius: '0'
      }
    }
  }
})
like image 23
doublejosh Avatar answered Oct 19 '22 02:10

doublejosh


Material UI 5.x.x

createTheme({
  components: {
    MuiButton: { 
      styleOverrides: { 
        root: { minWidth: 0 } 
      } 
    }
  }
})
like image 4
Sultan Aslam Avatar answered Oct 19 '22 02:10

Sultan Aslam