【问题标题】:How to correctly align an image inside a React Material ui Grid?如何在 React Material ui Grid 中正确对齐图像?
【发布时间】:2021-04-02 06:23:02
【问题描述】:

我正在为我的项目制作页脚。 我正在为页脚使用材质 UI 网格,所以我想在底部添加徽标,但我不明白为什么它有正确的填充(我猜它是填充,不确定)。

屏幕截图-> 是这样的。 (出于隐私原因模糊了徽标)

Code Footer.js React 组件 ->

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>
  );
}

希望它看起来像这样 ->

【问题讨论】:

  • @NearHuscarl 它似乎还没有工作。
  • 在您的 footerGridLogo 样式中,通过将显示设置为 flex 并将 justify-content 设置为居中来尝试居中对齐图像。
  • 好吧,有点工作,但网格没有环绕徽标;-(
  • 我把footerGrid样式中的width改成fit-contentthis是你想要的吗?
  • 是的,谢谢!

标签: reactjs material-ui react-grid-layout


【解决方案1】:

在 Grid 项目标签 style={{display:'flex',justifyContent:'center',alignItems:'center'}}

中添加此内联样式

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
  • 感谢您的回答,请务必添加更多信息,阅读更多关于good answersmarkdown 的正确用法的信息。
猜你喜欢
  • 2021-12-02
  • 2020-05-21
  • 1970-01-01
  • 1970-01-01
  • 2021-04-24
  • 2020-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多