[英]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
并为该类定义样式。
<Breadcrumbs aria-label="breadcrumb" separator=" " className="menu">
<Link className="custom-link" to="/">
Home
</Link>
</Breadcrumbs>
.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:有两种方法可以让它工作:
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.