Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material UI card will not center. React JS

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);
like image 388
lakerskill Avatar asked Nov 07 '18 03:11

lakerskill


People also ask

How do you center a box in React?

js, set its display property to flex and its alignItems and justifyContent properties to center . The div's content will be horizontally and vertically centered.

How do you align an image center in material UI?

Try giving the Grid which wrapps the image, the container attribute and then center the image by adding justify="center" and / or alignItems="center" to this container.

How do you align items in material UI?

Material-UI Grid Align Right If you need to horizontally align using only CSS, remember that justify-content horizontally aligns using the following values: flex-start: horizontally aligns left. center: horizontally aligns center. flex-end: horizontally aligns right.

How to set the card height with react material UI?

To set the card height with React Material UI, we can set the style prop of the Card. to set the style prop of the Card to an object that has the height of the card. As a result, the card height should be set to 45vw. To set the card height with React Material UI, we can set the style prop of the Card.

What is the difference between card and paper in material UI?

Material UI Card vs Paper The <Card> component from Material UI is exactly the same as the <Paper> component, there are no differences except you have an option to add a raised prop which simply sets the elevation prop of the Paper to 8. You can also just use elevation itself, or any of the other props found in the Paper component.

What is the card component page on material UI's documentation?

What is different about the Card component page on Material UI's documentation is that it shows examples for other components in the Card family, as we'll describe below. These components below are all fairly simple in that they apply certain CSS styles that will help you space out your content in different ways.

Do I need to use these components inside a card?

In no way do you need to use these components inside a Card, you can use them anywhere. Wraps children in a single <div> and applies 16px of padding, and 24px padding-bottom when it is the last child in the group. Wraps children in a single Button, creating a ripple effect when clicked.


1 Answers

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

like image 188
Nadun Avatar answered Sep 18 '22 16:09

Nadun