How do I center a vertical divider within a Material UI (v5) Grid?

Viewed 965

I'm trying to put two centered paragraphs into a column <Grid>, separated by a vertical <Divider>. I added the divider as a Grid item, but the divider appears justified to the right instead of the center, between the two paragraphs. Here is my code:

<Grid item container direction="row" spacing={{ xs: 1, md: 3 }}>
  <Grid item xs={5}>
    <Typography variant="body2" align="center">
      Paragraph A text
    </Typography>
  </Grid>
  <Grid item xs={2}>
    <Divider orientation="vertical" />
  </Grid>
  <Grid item xs={5}>
    <Typography variant="body2" align="center">
      Paragraph B text
    </Typography>
  </Grid>
</Grid>

With this code, the vertical divider is justified to the right. There is no change if I add justifyContent="center" to the container Grid, I assume because the divider is a right border. I think I need to add padding-right somehow, but calculated so that it is responsive to different screen widths. Any help would be much appreciated!

1 Answers

you need to remove spacing={{ xs: 1, md: 3 }} or change it and for change Divider orientation use container in grid :

   <Grid item container direction="row" >
            <Grid item xs={5}>
                <Typography variant="body2" align="center">
                    Paragraph A text
                </Typography>
            </Grid>
            <Grid item xs={2}
                  container
                  direction="row"
                  justifyContent="center"
                  alignItems="center"
            >
                <Divider orientation="vertical" style={{height:'100%',width:'1px'}}/>
            </Grid>
            <Grid item xs={5}>
                <Typography variant="body2" align="center">
                    Paragraph B text
                </Typography>
            </Grid>
</Grid>

enter image description here

codesandbox

Related