简体   繁体   English

MUI 卡未按样式呈现

[英]MUI Cards are not rendering with style

I am importing the Card component from MUI but the component does not have any style.我正在从 MUI 导入Card组件,但该组件没有任何样式。

import * as React from "react";
import Box from "@mui/material/Box";
import Card from "@mui/material/Card";
import CardActions from "@mui/material/CardActions";
import CardContent from "@mui/material/CardContent";
import Button from "@mui/material/Button";
import Typography from "@mui/material/Typography";

const bull = (
  <Box
    component="span"
    
    sx={{ display: "inline-block", mx: "2px", transform: "scale(0.8)" }}
  >
    •
  </Box>
);

export default function BasicCard() {
  return (
    <Card sx={{ minWidth: 275 }}>
      <CardContent>
        <Typography sx={{ fontSize: 14 }} color="text.secondary" gutterBottom>
          Word of the Day
        </Typography>
        <Typography variant="h5" component="div">
          be{bull}nev{bull}o{bull}lent
        </Typography>
        <Typography sx={{ mb: 1.5 }} color="text.secondary">
          adjective
        </Typography>
        <Typography variant="body2">
          well meaning and kindly.
          <br />
          {'"a benevolent smile"'}
        </Typography>
      </CardContent>
      <CardActions>
        <Button size="small">Learn More</Button>
      </CardActions>
    </Card>
  );
}

What the component is supposed to look like: MUI Component组件应该是什么样子: MUI 组件

What the component actually looks like: Imported Component组件的实际外观:导入的组件

How can I add styling?如何添加样式?

You probably have text-align: center set somewhere in the parent.您可能将text-align: center设置在父级的某处。 This usually happens when you create a react project with a template like CRA that has some CSS files like this :当您使用 CRA 之类的模板创建 React 项目时,通常会发生这种情况,该模板具有一些像这样的CSS 文件:

.App {
  text-align: center;
}

You can fix it by finding and removing that text-align property or reset it in your Card :您可以通过查找并删除该text-align属性或在您的Card重置它来修复它:

<Card sx={{ minWidth: 275, textAlign: "initial" }}>

If what you mean is the dark background, you can achieve it by setting the theme mode to dark:如果你的意思是深色背景,你可以通过将主题模式设置为深色来实现:

import { ThemeProvider, createTheme } from "@mui/material/styles";

const theme = createTheme({
  palette: {
    mode: "dark"
  }
});
<ThemeProvider theme={theme}>
  <Card sx={{ minWidth: 275 }}>
    {...}
  </Card>
</ThemeProvider>

代码沙盒演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM