【问题标题】:How to change box color in Material UI如何在 Material UI 中更改框颜色
【发布时间】:2020-09-03 17:11:10
【问题描述】:

我是网络开发的新手。话虽如此,我正在尝试使用 Material-UI 更改框颜色,但它不起作用。 (color=success.main)

目前,我得到的颜色是原蓝色。我试图改变每个盒子组件,但它并没有真正适用于其中任何一个。

import React from "react";
import { palette } from '@material-ui/system';
import {
    AppBar,
    Toolbar, 
    Box,
    Link,
    Hidden
} from '@material-ui/core';

import { makeStyles } from '@material-ui/core/styles';
import './Navbar.css';

const useStyles = makeStyles({
    links: {
      padding: '0 50px',
      color: 'white',
      "&:hover": {
        textDecorationColor: "green",
        cursor:'pointer'
      }
    },

  });
export default function Navbar() {
const Navbar = useStyles();
    return(
        <Box component='nav' color="success.main">
            <AppBar>
                <Toolbar>
                    VK Design 
                    <Box m='auto'>
                        <Hidden only='xs'>
                            <typography><Link className={Navbar.links} underline='hover'>HOME</Link></typography>
                            <typography><Link className={Navbar.links} underline='hover'>PORTFOLIO</Link></typography>
                            <typography><Link className={Navbar.links} underline='hover'>ABOUT</Link></typography>
                            <typography><Link className={Navbar.links} underline='hover'>CONTACT</Link></typography>
                        </Hidden>
                    </Box>
                </Toolbar>
            </AppBar>
        </Box>
    )
};

【问题讨论】:

    标签: material-ui


    【解决方案1】:

    使用“bgcolor”而不是“color”

    <Box        
        bgcolor="primary.main"        
      > ... </Box>
    

    【讨论】:

      【解决方案2】:

      我认为你应该定义一个类来为 . 你应该试试:

      ...
      const useStyles = makeStyles((theme) => ({
          root: {
            color: theme.palette.primary.main
          },
          links: {
            padding: '0 50px',
            color: 'white',
            "&:hover": {
              textDecorationColor: "green",
              cursor:'pointer'
            }
          },
      
        }));
      export default function Navbar() {
      const Navbar = useStyles();
          return(
              <Box component='nav' className={Navbar.root}>
                  ...
              </Box>
          )
      };

      【讨论】:

      • 它在盒子组件内不起作用,但在工具栏内起作用。这是正确的方法吗?看来我只是在解决这个问题。
      • 你想在盒子组件@KajiyamaVK里面改变什么?
      • 框的背景颜色。它已经奏效了。您的想法使我找到了解决方案-我将属性 backgroundColor 放在了 BOX 标记中,而是在 APPBAR 标记中进行了操作。非常感谢。
      猜你喜欢
      • 2020-12-26
      • 2021-12-07
      • 2020-07-16
      • 1970-01-01
      • 2022-08-23
      • 2021-08-15
      • 1970-01-01
      • 1970-01-01
      • 2019-08-28
      相关资源
      最近更新 更多