How to center a button in Material-UI - css

I couldn't figure out how to center buttons in Material-UI. This is the code I have:
function BigCard(props) {
const { classes } = props;
return (
<div>
<Card className={classes.card}>
<CardContent>
<Typography variant="display1" className={classes.title}>
Start Funding!
</Typography>
</CardContent>
<CardActions >
<Button size="small" color="primary" className={classes.actions}>
Share
</Button>
<Button size="small" color="primary">
Learn More
</Button>
</CardActions>
</Card>
</div>
);
How can I center my button?

You can use Box element
<Box textAlign='center'>
<Button variant='contained'>
My button
</Button>
</Box>

you could use the override with classes in the material ui doc,
FIRST WAY
You can do something like :
//Add your justify css in your styles const
const styles = {
...
root: {
justifyContent: 'center'
}
};
And use the classes props to add this to your CardActions component :
<CardActions classes={{root: classes.root}}>
SECOND WAY (easier)
OR You can use the style directly on your component, but i advise you to train how to use classes if you're working alot with material-ui
Just do something like :
<CardActions style={{justifyContent: 'center'}}>

For use cases that avoid defining css
<Grid container justify="center">
{props.children}
</Grid>

Here is how I did it with Material - UI
import {Typography, Button} from '#material-ui/core';
<Typography align='center'>
<Button
color='primary'
size='large'
type='submit'
variant='contained'
>
Sign Up
</Button>
</Typography>

Quick and dirty:
<Button style={{margin: '0 auto', display: "flex"}}>
NEXT
</Button>

From MUI v5, you can apply the custom styles using the sx props. Because CardActions layout is already flex, you only need to set its justify-content to center.
Using sx instead of inline styles like the other answer help reduce the CSS specificity, and make it easier to override the CSS in the future
<CardActions sx={{ justifyContent: "center" }}>
<Button size="small">Learn More</Button>
</CardActions>
Live Demo

What I tried is below and is also shown in official videos of material ui on youtube.
<Grid item xs={12} sm={12} md={4} lg={4}
style={{
textAlign:'center' // this does the magic
}}
>
<Button>
NEXT
</Button>
</Grid>
Thanks and best wishes

You have to use two properties: display and justify-content
<CardActions display='flex' justifyContent='center'>

This will centre your button horizontally
<Button
size="medium"
variant="contained"
color="primary"
style={{
display: "flex",
flexDirection: "row",
justifyContent:"center",
backgroundColor: purple[500],
'&:hover': {
backgroundColor: purple[700],
},
}}
>
Get Hired
</Button>

The easiest way is to use a Box wrapping around the button
<Box
sx={{
display: 'flex',
flexDirection: { xs: 'column', md: 'row' },
alignItems: 'center',
justifyContent: 'center',
}}
>
<Button>Click me</Button>
</Box>

This may sound counter-intuitive but what worked for my case (slightly different than yours) was to use the fullWidth attribute in the Button element.
Here is my scenario (MUI v5):
<Grid container alignItems={'center'} >
<Grid item>
<Button fullWidth variant='contained' >Click Me!</Button>
</Grid>
</Grid>

wrap element in this center
.center{
width: 100%;
height: 100%;
display: flex;
justify-content: center;
}

Related

Aligning in CSS and Material UI in React

How do I align one button to the left while the other two on the center?
Pls check codesandbox here
CLICK HERE
<DialogActions sx={{ justifyContent: "center" }}>
<Button>Left</Button>
<Button onClick={handleClose}>Cancel center</Button>
<Button onClick={handleClose}>Subscribe center</Button>
</DialogActions>
You can achieve that using Grid. Check out below codes.
<DialogActions
sx={{
display: "grid",
gridTemplateColumns: "1fr repeat(2, auto) 1fr",
justifyContent: "center"
}}
>
<Button sx={{ marginRight: "auto" }}>Left</Button>
<Button onClick={handleClose}>Cancel center</Button>
<Button onClick={handleClose}>Subscribe center</Button>
</DialogActions>

How to use MUI to stack components on top of eachother?

I want a div that looks like this on Desktop:
------------------------------------------------------------------
| (icon) | (content) |(button here)|
------------------------------------------------------------------
to look like this on mobile:
--------------------------
|(icon)| (content) |
--------------------------
| (button here) |
--------------------------
I have tried using Card and CardHeader, but they don't give me the results I want on mobile:
<Card sx={{ width: "90%", marginBottom: 2 }}>
<CardHeader
avatar={<img src={icon} style={{ maxWidth: "100%" }} />}
action={
<Button
onClick={onClick}
variant="contained"
sx={{
width: "173px",
height: "48px",
letterSpacing: 1.4,
}}
>
{buttonText}
</Button>
}
title={children}
subheader={subContent}
/>
</Card>
I would prefer an alternative to Card. Grid doesn't seem to have stackable capabilities, so not sure what to do there... Thank you.
From your illustration above, it sounds like Grid is exactly what you're looking for, as Grid is specifically for responsive layouts. You can use a combination of the xs, sm, md, lg, and xl props to define how much space, in the 12 column layout, that you want each item to occupy at a given breakpoint. For example, in your case:
<Grid container>
<Grid item xs={4} sm={2}>
(icon)
</Grid>
<Grid item xs={8} sm={8}>
(content)
</Grid>
<Grid item xs={12} sm={2}>
(button here)
</Grid>
</Grid>
sm+:
xs:
Working example for your use case: https://codesandbox.io/s/basicgrid-material-demo-forked-q795q?file=/demo.js:0-725
MUI Stack component is usually great for stacking
[1]: https://mui.com/material-ui/react-stack/
I have played with it below a little bit but check out the spec maybe its may save you a lot
import { Stack } from '#mui/material'
{ isMobile ? <Stack justifyItems="center" sx={{ width: `100%` }}>
<Stack direction="row" spacing={12} alignItems="center" sx={{ margin: `auto` }}>
<p>Icon</p>
<p>Content</p>
</Stack>
<Stack sx={{ margin: `auto auto` }}>
<button>Button here</button>
</Stack>
</Stack> : <Stack direction="row" spacing={12} alignItems="center">
<p>Icon</p>
<p>Content</p>
<button>Button here</button>
</Stack> }

Having trouble creating onHover on Grid item component MaterialUI

I am creating a Grid layout that has nested Grid components in it. However, for some reason I do not understand, using an inline style in the Grid item component does not work. Please help, I am quite new in using MaterialUI in ReactJS. The Grid is encapsulated inside a Hide component that only shows in mobile devices.
What I am trying to achieve is that the Grid item change color on
hover
Here is what I did:
<Hidden mdUp='hide'>
<Grid
container
xs={12}
style={{
fontFamily: 'Old Newspaper',
background: '#f49275',
'&:hover': { background: 'black' },
marginLeft: '4px',
marginRight: '4px',
}}
>
<Grid
item
xs={4}
style={{
justifyContent: 'center',
display: 'flex',
}}
>
<a href='#'>
♦ Github ♦
</a>
</Grid>
<Grid
item
xs={4}
style={{ justifyContent: 'center', display: 'flex' }}
>
<a
href='#'
target='_blank'
>
♦ LinkedIn ♦
</a>
</Grid>
<Grid
item
xs={4}
style={{ justifyContent: 'center', display: 'flex' }}
>
<a
href='#'
target='_blank'
>
♦ Codewars ♦
</a>
</Grid>
</Grid>
</Hidden>
To get hover style on grid items you've to add hover style on grid items. And you can't add hover styles on an element in inline CSS way. Give a className to grid items and then add hover styles on those.
import React from "react";
import { makeStyles } from "#material-ui/core/styles";
import Grid from "#material-ui/core/Grid";
import { Hidden } from "#material-ui/core";
const useStyles = makeStyles((theme) => ({
item:{
color:'#fff',
justifyContent: "center",
display: "flex",
'&:hover':{
'&>a':{
color:'green'
}
}
}
}));
export default function SpacingGrid() {
const classes = useStyles();
return (
<Hidden mdUp="hide">
<Grid
container
className={classes.container}
xs={12}
style={{
fontFamily: "Old Newspaper",
background: "#f49275",
marginLeft: "4px",
marginRight: "4px"
}}
>
<Grid
item
xs={4}
className={classes.item}
>
♦ Github ♦
</Grid>
<Grid item xs={4} className={classes.item}>
<a href="#" target="_blank">
♦ LinkedIn ♦
</a>
</Grid>
<Grid item xs={4} className={classes.item}>
<a href="#" target="_blank">
♦ Codewars ♦
</a>
</Grid>
</Grid>
</Hidden>
);
}
codesandbox link:- https://codesandbox.io/s/o8b52
And can you please elaborate more your doubt about using inline style in grid item.

Align Items Flexbox , React Material in React

I'm using flexbox, React Material in my React App and i wanted to align the searchbar and the clear button on the same line and with space on each other.
Pls see this link
CLICK HERE
<div className={classes.searchBarSection}>
<Paper component="form" className={classes.searchBar}>
<InputBase
className={classes.input}
placeholder="Search..."
inputProps={{ "aria-label": "search..." }}
/>
<IconButton
type="button"
className={classes.iconButton}
aria-label="search"
>
<SearchIcon />
</IconButton>
</Paper>
<Button variant="contained" size="small" color="primary">
Clear
</Button>
</div>
try this:
searchBarSection: {
display: "flex",
justifyContent: "space-between",
width: "300px"
},
adjust the width for how much space you want between them
this will look like:
add these styles, the width will give the searchBatSection breathing room to space the children. Experiment around with the justifyContent options to find your suit.
searchBarSection: {
width: "50%",
display: "flex",
justifyContent: "space-between"
},
Use grid
searchBarSection: {
display: 'grid',
gridTemplateColumns: '0.8fr 0.2fr',
gap: '12px',
},
https://codesandbox.io/s/material-demo-6w93i

In material ui, why isn't the "spacing" styling attribute doing anything when I try and space elements in my Box?

I'm using materialUI in my React 16.10 application. I have two buttons in a Box.
<Box
style={{
marginTop: "3rem",
marginLeft: "1rem",
display: "flex",
justifyContent: "center",
spacing: 1,
}}
>
<Button onClick={handleSave} variant="contained" color="primary">
SAVE
</Button>
<Button
onClick={toDetailsView}
startIcon={<CancelIcon />}
variant="contained"
color="primary"
>
CANCEL
</Button>
</Box>
Even though I'm using "spacing: 1", the buttons appear right next to each other so it is impossible to distinguish where one ends and the next begins ...
How do I add style so that there is a little bit of space in between the buttons?
Looking for easy fix ?
margin: 1rem;

Resources