Material UI card will not center. React JS - reactjs

Trying to use a card for the main part of my home page. However, nothing I will do will center the card, and I have tried putting justify, alignItems, alignContent, however, none of them seem to resolve the issue. I honestly have no idea what else to do to align this to the center. I don't see how it's even possible. Here is the code:
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '#material-ui/core/styles';
import Card from '#material-ui/core/Card';
import CardActionArea from '#material-ui/core/CardActionArea';
import CardActions from '#material-ui/core/CardActions';
import CardContent from '#material-ui/core/CardContent';
import CardMedia from '#material-ui/core/CardMedia';
import Button from '#material-ui/core/Button';
import Typography from '#material-ui/core/Typography';
import reactImage from '../images/djangoreact.png'
const styles = {
card: {
width: "100%",
backgroundColor: 'black',
textAlign: 'center',
justifyContent: 'center',
alignContent: 'center',
padding: '30px'
},
media: {
height: 325,
// textAlign: 'center',
// justifyContent: 'center',
// alignContent: 'center',
},
font: {
color: 'white',
// textAlign: 'center',
// justifyContent: 'center',
// alignContent: 'center',
},
header:{
textAlign: 'center',
justify: 'center',
alignContent: 'center',
width: '100%'
}
};
function MediaCard(props) {
const { classes } = props;
return (
<div className={classes.header} style={{
justify: 'center',
alignContent: 'center',
alignItems: 'center'
}}>
<Card className={classes.card} style={{
justify: 'center',
alignContent: 'center',
alignItems: 'center'
}}>
<CardActionArea>
<CardMedia
className={classes.media}
image={reactImage}
title="Contemplative Reptile"
/>
<CardContent>
<Typography gutterBottom variant="h5" component="h2" className={classes.font}>
Welcome to the KB
</Typography>
<Typography component="p" className={classes.font}>
Check out the tutorial for the Djanog/React boilerplate used to make this site!
</Typography>
</CardContent>
</CardActionArea>
<CardActions>
<Button size="small" color="primary">
Learn More
</Button>
</CardActions>
</Card>
</div>
);
}
MediaCard.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(MediaCard);

Use the Grid provided by material-UI
import Grid from '#material-ui/core/Grid';
// import like this
function MediaCard(props) {
const { classes } = props;
<Grid
container
spacing={0}
direction="column"
alignItems="center"
justify="center"
style={{ minHeight: '100vh' }}
>
<Grid item xs={3}>
<Card>
// card content
</Card>
</Grid>
</Grid>
}
hope this will help you.
Use this: https://material-ui.com/layout/grid/ for more info

Alternatively to switching to <Grid>, I put a <div> wrapper around the <CardMedia> element and used
style={{ display:'flex', justifyContent:'center' }}
on the outer <div> tag to achieve the same result.

I do not know what is your problem while you are setting the width of cards 100%.
I cannot see the rendered view, however, I want to recommend one of minimal solution without <Grid>.
If I were you, I would simply try:
card: {
'margin-left': '5%',
width: "90%",
...

use this style
const useStyles = makeStyles((theme) => ({
cardstyle:{
marginTop: theme.spacing(20),
margin:'auto',
flexDirection: 'column',
},
}));
<Card className={clasess.cardstyle}>
// card content
</Card>

Related

Centre the Typography

I tried to centre the typography using both textAlign and align but that did not work. Can someone help me with this ? The resulting page is below the code
import React, {useState} from 'react'
import logo from '../../images/logo.svg'
import { Typography } from '#mui/material'
import AccountCircleIcon from "#mui/icons-material/AccountCircle"
const NavBar = () => {
const StyledToolbar = styled(Toolbar)({
display: "flex",
alignItems: "center",
justifyContent: "space-between"
});
const Logo = styled("img")({
width: "12rem",
height: "auto",
});
const StyledAppBar = styled(AppBar)({
position: "sticky",
backgroundColor: "#EDEDED",
});
return (
<StyledAppBar>
<StyledToolbar>
<Logo src={logo} alt="quizy" />
<Typography variant="h5" color="black" align='center'>
Question
</Typography>
<AccountCircleIcon
style={{ color: "black" }}
/>
</StyledToolbar>
</StyledAppBar>
);
}
Page Result
It actually is centered according to its width. To center with 3 children elements you could assign Logo, Typography and Icon following CSS properties:
flex-grow: 1
flex-basis: 0
This will probably move the AccountCircleIcon a bit to the left but you can assign it margin-left: auto. You will also have to remove your assigned width of Logo. For example:
import React, {useState} from 'react'
import logo from '../../images/logo.svg'
import { Typography } from '#mui/material'
import AccountCircleIcon from "#mui/icons-material/AccountCircle"
const NavBar = () => {
const StyledToolbar = styled(Toolbar)({
display: "flex",
alignItems: "center",
justifyContent: "space-between"
});
const Logo = styled("img")({
width: "12rem",
height: "auto"
});
const StyledAppBar = styled(AppBar)({
position: "sticky",
backgroundColor: "#EDEDED",
});
return (
<StyledAppBar>
<StyledToolbar>
<div style={{ flexGrow: 1, flexBasis: 0 }}>
<Logo src={logo} alt="quizy" />
</div>
<Typography style={{ flexGrow: 1, flexBasis: 0 }} variant="h5" color="black" align='center'>
Question
</Typography>
<div style={{ flexGrow: 1, flexBasis: 0, textAlign: "right" }}>
<AccountCircleIcon
style={{ marginLeft: 'auto', color: "black" }}
/>
</div>
</StyledToolbar>
</StyledAppBar>
);
}

React - Material UI Grid and useStyles prop issues

First time really playing around with materialUI. I've made a grid of cards and sorted out most of the styling within the same component, so I wanted to move all the useStyles styling to a styling file.
After figuring out how to do so, on reload I could see that no backgroundColor styling across any of the components were rendering, but everything else was.
backgroundColor rendered fine when it was in the same file, and weirdly, it renders once (the other backgroundColours not removed render fine, I mean) if I remove one instance of the color and readd it...but on reload, they're all gone again.
Appreciate some help on this please!
GridStyles.js
import { makeStyles } from '#material-ui/core/styles'
const useStyles = makeStyles({
root: {
flexGrow: 1,
marginLeft: '15rem',
marginRight: '15rem',
wordBreak: 'break-all',
whiteSpace: 'unset'
},
containerCard: {
border: 'solid 3px black',
marginRight: '2rem',
marginLeft: '2rem',
marginTop: '3rem',
marginBottom: '5rem',
borderRadius: '8px',
borderLeftStyle: 'dashed',
borderRightStyle: 'dashed',
},
topCard: {
backgroundColor: '#53E9B2',
border: 'none',
boxShadow: 'none',
paddingBottom: '7rem',
position: 'relative'
},
topCardText: {
position: 'absolute',
bottom: 0,
padding: 0,
marginLeft: '2rem'
},
middleCard: {
border: 'solid 2px black',
borderLeftStyle: 'dashed',
borderRightStyle: 'dashed',
backgroundColor: '#53E9B2',
marginRight: '2rem',
marginLeft: '2rem',
marginTop: '1em',
marginBottom: '3rem',
borderRadius: '8px'
},
middleCardText: {
whiteSpace: 'pre-wrap',
color: 'white'
},
bottomCard: {
backgroundColor: '#53E9B2',
border: 'none',
boxShadow: 'none',
paddingBottom: '1rem'
},
bottomCardText: {
color: 'white',
padding: 0,
marginLeft: '2rem'
}
})
export default useStyles
Grid.js
import React from 'react'
import useStyles from './GridStyles'
import {
Grid,
Card,
CardHeader
} from '#material-ui/core/'
const CardGrid = (props) => {
const classes = useStyles(props);
return (
<div className={classes.root}>
<Grid
container
spacing={0}
direction='row'
justifyContent='flex-start'
alignItems='flex-start'
>
{props.cardData.map(elem => (
<Grid
item
xs={4}
sm={4}
md={4}
xl={4}
key={props.cardData.indexOf(elem)}
>
<Card className={classes.containerCard}>
<Card className={classes.topCard}>
<CardHeader className={classes.topCardText} title={elem.name} />
</Card>
<Card className={classes.middleCard}>
<CardHeader
className={classes.middleCardText}
title={`Company Name:\n${elem.company.name}`}
/>
<CardHeader
className={classes.middleCardText}
title={`Company Catchphrase:\n${elem.company.catchPhrase}`}
/>
<CardHeader
className={classes.middleCardText}
title={`Company Bs:\n${elem.company.bs}`}
/>
</Card>
<Card className={classes.bottomCard}>
<CardHeader
className={classes.bottomCardText}
title={`Phone: ${elem.phone}`}
/>
<CardHeader
className={classes.bottomCardText}
title={`Email: ${elem.email}`}
/>
<CardHeader
className={classes.bottomCardText}
title={`Website: ${elem.website}`}
/>
</Card>
</Card>
</Grid>
))}
</Grid>
</div>
)
}
export default CardGrid
Next JS not properly loading Material UI styles until refresh
The accepted answer on this link fixed my problem. Converted the GridStyles page into one const with the styles and nothing else, arrow function with that calls this file with the makeStyles() function reference and it worked.

How can I stop my material UI Cards from going off screen creating a horizontal scroll situation?

I have created 5 components that are wrapped inside a component. I want all 5 of those cards to stay in one row whether the screen size increases or decreases. With my current code, the cards show up in one row, but they go over the screen. I would have to scroll to the right in order to see the fifth card.
I want to be able to see all five cards on the screen regardless of the screen size. At the very least, all of them together on a screen larger than an iPad. Please help.
Code Below:
"time-card" component
import * as React from 'react';
import {
Card,
CardHeader,
CardMedia,
CardContent,
Typography,
} from '#mui/material';
import Icon from './images/icon.png';
import { makeStyles } from '#mui/styles';
import { variables } from 'theme';
const useStyles = makeStyles({
card: {
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
textAlign: 'center',
minWidth: 400,
maxWidth: 500,
height: '100%',
position: 'relative',
backgroundColor: '#7b1f82',
backgroundImage: 'linear-gradient(#891f7e, #6d2386)',
minHeight: '600px',
maxHeight: '700px',
margin: 10,
padding: '30px',
flexGrow: 1,
flexShrink: 1,
},
mediaSize: {
width: 70,
display: 'flex',
alignSelf: 'center',
},
Icon: {
display: 'flex',
alignSelf: 'center',
marginTop: '20px',
boxShadow: `8px 7px 9px 2px #600562`,
borderRadius: '50%',
height: '400',
width: 90,
padding: 7,
},
cardHeader: {
marginBottom: 0,
padding: 0,
},
eventName: {
marginBottom: '15 0',
fontWeight: 600,
fontSize: 70,
align: 'center',
},
cardContent: {
marginTop: 0,
paddingTop: 0,
},
eventBeginTime: {
fontSize: 55,
fontWeight: 600,
},
eventEndTime: {
fontSize: 65,
fontWeight: 600,
},
});
const TimeCard = () => {
const classes = useStyles();
return (
<Card className={classes.card}>
<CardMedia
className={classes.Icon}
component="img"
image={Icon}
></CardMedia>
<CardHeader
flexGrow={1}
className={classes.cardHeader}
title={
<Typography className={classes.eventName} align="center">
Event
</Typography>
}
/>
<CardContent className={classes.cardContent}>
<Typography color={variables.primary.main} variant="h2">
Begins
<Typography className={classes.eventBeginTime}>5:00 AM</Typography>
</Typography>
<Typography color={variables.white} variant="h2">
Ends
<Typography className={classes.eventEndTime}>6:00 AM</Typography>
</Typography>
</CardContent>
</Card>
);
};
export default TimeCard;
"Time-card-grid" component
import * as React from 'react';
import Grid from '#mui/material/Grid';
import { makeStyles } from '#mui/styles';
import TimeCard from './time-card';
const useStyles = makeStyles({
timeCardGrid: {
margin: 0,
},
});
const TimesGrid = () => {
const classes = useStyles();
return (
<div style={{ maxWidth: '100%' }}>
<Grid container className={classes.timeCardGrid} wrap="nowrap">
<TimeCard />
<TimeCard />
<TimeCard />
<TimeCard />
<TimeCard />
</Grid>
</div>
);
};
export default PrayerTimesGrid;
The minWidth property on your card is what's causing the overflow. The card will never be narrower than 400px, which is wider than a lot of phone screens. If you want everything on the page the maxWidth of the card should be the width of the screen divided by 5. e.g maxWidth: '20vw' (20% of the viewport width) or even just width: '20vw' and forget about maximum and minimum.
To do what you want, you have to think about the screen size. For instance,
Each card can't be fixed at 400px. Because otherwise you'll have to run into the situation you got. Maybe remove this line:
minWidth: 400,
And then your TimesGrid can be a flex based. This way all cards can spread out in a row.
const useStyles = makeStyles({
timeCardGrid: {
margin: 0,
display: flex,
justifyContent: "space-between"
},
})

How to correctly align an image inside a React Material ui Grid?

I was working on a footer for my project.
I am using material UI grid for the footer so I wanted to add the logo at the bottom but I don't understand why it is having right padding (I guess it is padding, not sure).
ScreenShot ->
It's something like this. (Have blurred the logo for privacy reasons)
Code Footer.js React Compoennt ->
import React from "react";
import CssBaseline from "#material-ui/core/CssBaseline";
import Typography from "#material-ui/core/Typography";
import { makeStyles } from "#material-ui/core/styles";
import Container from "#material-ui/core/Container";
import Link from "#material-ui/core/Link";
import Grid from "#material-ui/core/Grid";
import Box from "#material-ui/core/Box";
import logoWhite from "../logos/logoWhite.png";
function Copyright() {
return (
<Typography variant="body2" color="textSecondary">
{"Copyright © "}
<Link color="inherit" href="https://material-ui.com/">
Your Website
</Link>{" "}
{new Date().getFullYear()}
{"."}
</Typography>
);
}
const useStyles = makeStyles((theme) => ({
root: {
display: "flex",
flexDirection: "column",
minHeight: "100vh",
},
main: {
marginTop: theme.spacing(8),
marginBottom: theme.spacing(2),
},
footer: {
padding: theme.spacing(3, 2),
marginTop: "auto",
backgroundColor: "#312D2D",
color: "white",
height: 167,
},
footerGrid: {
width: "20%",
marginLeft: 40,
marginTop: 35,
pading:0,
border: "2px solid red"
},
footerLogo: {
minWidth: "50%",
maxWidth: "60%",
width: "auto",
margin: "0 auto",
backgroundColor: "pink"
},
footerGridLogo: {
width: "fit-content",
pading:0,
borderRight: "1px solid white",
margin:0,
border: "4px solid green"
}
}));
export default function Footer() {
const classes = useStyles();
return (
<div className={classes.root}>
<CssBaseline />
<footer className={classes.footer}>
<Grid spacing={0} className={classes.footerGrid}>
<Grid item className={classes.footerGridLogo}>
<img
src={logoWhite}
alt="brandlogo"
className=""
className={classes.footerLogo}
/>
</Grid>
</Grid>
</footer>
</div>
);
}
Wanted it to look something like this ->
add this inline styling inside the Grid item tag style={{display:'flex',justifyContent:'center',alignItems:'center'}}

How to center vertically and horizontally in React with Material-UI?

I want to center a Button inside of a Card that is in the center of the screen. So far I haven't been able to.
This is wha I have so far:
import React from 'react'
import Button from '#material-ui/core/Button'
import { makeStyles } from '#material-ui/core/styles'
import { Card } from '#material-ui/core'
import Grid from '#material-ui/core/Grid'
const useStyles = makeStyles({
root: {
background: 'linear-gradient(45deg, #9013FE 15%, #50E3C2 90%)',
minWidth: '100%',
minHeight: '100vh',
display: "flex",
flexDirection: "column",
justifyContent: "center"
},
card: {
maxWidth: '40%',
minHeight: '20vh',
},
})
export default function LoginPage () {
const classes = useStyles()
return (
<Grid className={classes.root} spacing={0} align="center" justify="center">
<Card className={classes.card} align="center" justify="center">
<Button variant="contained" color="primary">
Hello World
</Button>
</Card>
</Grid>
)
}
It's centered vertically but not horizontally, when I change other parameter I get it centered horizontally but not vertically.
Screenshot of the result
Any Ideas?
Best regards.
You can use alignItems.
Code Sandbox demo: https://codesandbox.io/s/stack-overflow-material-ui-centering-0rgqx
const useStyles = makeStyles({
root: {
background: "linear-gradient(45deg, #9013FE 15%, #50E3C2 90%)",
minWidth: "100%",
minHeight: "100vh",
display: "flex",
flexDirection: "column",
justifyContent: "center"
},
card: {
maxWidth: "40%",
minHeight: "20vh",
display: "flex",
alignItems: "center"
}
});
export default function LoginPage() {
const classes = useStyles();
return (
<Grid
className={classes.root}
spacing={0}
alignItems="center"
justify="center"
>
<Card className={classes.card}>
<Button variant="contained" color="primary">
Hello World
</Button>
</Card>
</Grid>
);
}

Resources