Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align typography text to center

I am trying to align the text in the center by using <Typography align="center"> for the footer but it is not working. How can I align the text to center?

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import AppBar from "@material-ui/core/AppBar";
import CssBaseline from "@material-ui/core/CssBaseline";
import Toolbar from "@material-ui/core/Toolbar";
import Typography from "@material-ui/core/Typography";

const useStyles = makeStyles(theme => ({
  appBar: {
    top: "auto",
    bottom: 0
  }
}));

export default function Footer() {
  const classes = useStyles();

  return (
    <React.Fragment>
      <CssBaseline />
      <AppBar color="primary" className={classes.appBar}>
        <Toolbar>
          <Typography align="center">Visit again</Typography>
        </Toolbar>
      </AppBar>
    </React.Fragment>
  );
}

enter image description here

like image 685
Tanish Gupta Avatar asked Feb 03 '26 17:02

Tanish Gupta


2 Answers

Aligning text inside typography won't help coz its width would only span till the content which would not bring it in center of footer element.

You need to handle it from footer component which is its parent by using flex as I have done below.

  const useStyles = makeStyles(theme => ({
  appBar: {
    top: "auto",
    bottom: 0,
    textAlign:"center"
  },
  footer: {
    display:"flex",
    justifyContent:"center",
  }
}));

export default function App() {
  const classes = useStyles();

  return (
    <React.Fragment>
      <CssBaseline />
      <AppBar color="primary" className={classes.appBar}>
        <Toolbar className={classes.footer}>
          <Typography align="center">Visit again</Typography>
        </Toolbar>
      </AppBar>
    </React.Fragment>
  );
}

Using flex would bring all the content of footer in center,If you wish to have only Visit again in center this can be achieved by wrapping it up into a div and applying the same CSS that of footer

like image 125
PRATIK NAIK Avatar answered Feb 05 '26 05:02

PRATIK NAIK


This Code Working for me.

 flexGrow: 1,
 textAlign: "center"

Try This

const useStyles = makeStyles((theme) => ({
  appBar: {
    top: "auto",
    bottom: 0
  },
  typo: {
    flexGrow: 1,
    textAlign: "center"
  }
}));

export default function App() {
  const classes = useStyles();

  return (
    <React.Fragment>
      <CssBaseline />
      <AppBar color="primary" className={classes.appBar}>
        <Toolbar>
          <Typography className={classes.typo}>Visit again</Typography>
        </Toolbar>
      </AppBar>
    </React.Fragment>
  );
}

Working Code Sandbox Link: https://codesandbox.io/s/falling-dawn-y8mb2?file=/src/App.js

Screenshot: enter image description here

like image 25
sathishk2030 Avatar answered Feb 05 '26 05:02

sathishk2030



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!