html - 设置 className 并使用 styledComponents 创建一个动作
问题描述
为什么这不起作用?我想在我的 NavigationBar 中给这个 NavigationTab 一个不同的背景颜色。稍后当我选择它时,活动选项卡应显示为红色背景。就像我们知道不同的导航菜单一样。
<NavigationLinkContainer className={"active"} onClick={() => { window.location.href = "/" }}>
const NavigationLinkContainer = styled.div`
display: flex;
flex-flow: row;
align-items: center;
box-sizing: border-box;
transition: 0.3s ease-in-out;
margin: 0px;
cursor: pointer;
height: 50px;
.active {
background-color: #D1495B;
}
/* &:hover {
background-color: ${colors.softRed};
} */
&:hover {
color: ${colors.white};
background: ${colors.softRed};
&:before {
height: 100%;
transition: 0.3s ease-in-out;
}
}
@media (max-width: 1000) {
padding: 15px;
border-bottom: 4px solid transparent;
&:hover {
background-color: azure;
}
&:active, &:hover {
color: azure;
border-bottom: 4px solid azure;
}
&:before {
display: none;
}
}
`
解决方案
您没有为活动类使用正确的选择器。添加父选择器&
以在组件具有active
类时应用样式。
const NavigationLinkContainer = styled.div`
display: flex;
flex-flow: row;
align-items: center;
box-sizing: border-box;
transition: 0.3s ease-in-out;
margin: 0px;
cursor: pointer;
height: 50px;
// use the parent selector &
&.active {
background-color: #D1495B;
}
.
.
.
.
`
希望这可以帮助!
推荐阅读
- opengl - OpenGL重叠丑陋渲染
- java - Spring-Boot Thymeleaf 以 html 格式加载图像,列表不显示
- php - 如何创建创建 OpenVPN 文件的 PHP 脚本(以及 bash 文件的答案)
- c# - 如何反转 DispatcherTimer?
- android - 如何在 Android Studio 的 Activity 中调用 EditText onSelectionChanged 方法
- c++ - 将 strcpy_s() 和 strcat_s() 与动态分配的字符串一起使用
- python - 读取 csv 文件并拆分为保留列名的列。熊猫
- tensorflow - keras 模型训练的最大损失量是多少?
- azure - 来自 AAD 的带有 OAuth2 流的错误 access_token
- python-3.x - 使用 Python 打印一个列表,其中值彼此之间有特定距离