简体   繁体   English

自定义链接悬停下划线高亮material-ui

[英]Customize link hover underline highlight material-ui

I'm trying to follow material-ui custom styles and link but am not sure how to override classes and use withStyles我正在尝试遵循material-ui 自定义样式链接,但不确定如何覆盖classes和使用withStyles

      <Breadcrumbs aria-label="breadcrumb" separator=" " className="menu">
        <Link color="inherit" href="/home">
          Home
        </Link>
      </Breadcrumbs>

I've modified global app.css file我修改了全局app.css文件

a:hover{
  border-bottom: 1px solid red
}

And the outcome is like结果就像

Home
----
----  <- I have 2 underlines now when hovering over the link. The bottom one will be red.

How do I override this such as following?我如何覆盖它,例如以下内容?

Home
---- <- only 1 red underline shown when hovering over the link

For those not doing it through CSS but rather classes, you could do对于那些不是通过 CSS 而是通过类来做的人,你可以做

link: {
    color: "#00ff00",
    "&:hover": {
        color: "#000000",
        textDecoration: "underline #000000"
    }
},

Considering that a Link is an <a> tag, I think what you need to override is text-decoration-color, not border-bottom.考虑到Link是一个<a>标签,我认为您需要覆盖的是 text-decoration-color,而不是border-bottom。 Additionally, to make your styling a bit more specific, you can give the Link a className and define styles for that class.此外,为了使您的样式更加具体,您可以为Link提供一个className并为该类定义样式。

Component:零件:

  <Breadcrumbs aria-label="breadcrumb" separator=" " className="menu">
    <Link className="custom-link" to="/">
      Home
    </Link>
  </Breadcrumbs>

Style:风格:

.custom-link:hover {
  color: red;
  text-decoration-color: red;
}

See working example: https://codesandbox.io/s/cool-bush-wpn4m请参阅工作示例: https ://codesandbox.io/s/cool-bush-wpn4m

As suggested by the other answer the code would surely be正如其他答案所建议的那样,代码肯定是

 a { text-decoration-color: red; }

There are two ways you can get this to work:有两种方法可以让它工作:

  • Declare the anchor tag styling in index.css and applying it over the whole web-app like you have done.在 index.css 中声明锚标记样式并将其应用于整个 Web 应用程序,就像您所做的那样。
  • Or you could declare it in the style object that you pass while using withStyles HOC.或者,您可以在使用 withStyles HOC 时传递的样式对象中声明它。 If it helps, you can wrap the Link tag in a div and apply the styles on that div.如果有帮助,您可以将 Link 标记包装在 div 中并在该 div 上应用样式。 This will keep the change local, and the Link tag will inherit the property from its parent div.这将使更改保持在本地,并且 Link 标签将从其父 div 继承该属性。

Provide Material UI with a custom theme, as documented here .为 Material UI 提供自定义主题,如此所述。

The relevant bits for link hover underlining are:链接悬停下划线的相关位是:

import { createTheme } from "@mui/material";

export const myTheme = createTheme({
  components: {
    MuiLink: {
      styleOverrides: {
        root: {
          textDecoration: "none",
          ":hover": {
            textDecoration: "underline",
          },
        },
      },
    },
  },
});

Regarding the answers that address changing this globally (which doesn't seem to be what was asked, but was how I ended up here), the MUI Way is to override the default props for MuiLink in your theme.关于解决全局更改此问题的答案(这似乎不是所问的,而是我在这里结束的方式),MUI Way 是覆盖主题中MuiLink的默认道具。 For example:例如:

export const muiTheme = responsiveFontSizes(
  createTheme({
    components: {
      MuiLink: {
        defaultProps: {
          underline: "hover",
        },
        styleOverrides: {
          underlineHover: {
            "&:hover": {
              textDecorationColor: "red",
            },
          },
        },
      },
    },
  })
);

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

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