Navbar样式有效的下划线

问题描述 投票:0回答:1

我有一个导航栏,当用户将鼠标悬停在链接上时,该导航栏会为下划线设置动画效果。我正在尝试添加CSS,以便活动导航栏具有永久下划线。

我的代码

<Navbar className="navbar-expand-sm navbar-toggleable-sm" light>
                <Container id="ContainerGrnH">
            <NavbarToggler onClick={this.toggleNavbar} className="mr-2" />
                    <Collapse className="d-sm-inline-flex flex-sm-row-reverse" isOpen={!this.state.collapsed} navbar>
            <div class="topnav">
              <ul className="navbar-nav flex-grow">
                <NavItem>
                  <NavLink tag={Link} className="text-white"  to="/home">HOME <div className="underline"></div></NavLink>
                </NavItem>
                <NavItem>
                    <NavLink tag={Link} className="text-white" to="/people-profiles">PEOPLE PROFILES<div className="underline"></div></NavLink>
                </NavItem>
                <NavItem>
                    <NavLink tag={Link} className="text-white" to="/Role-types">ROLE TYPES<div className="underline"></div></NavLink>
                </NavItem>
                <NavItem>
                    <NavLink tag={Link} className="text-white" to="/support">SUPPORT<div className="underline"></div></NavLink>
                </NavItem>
                <NavItem>
                    <NavLink tag={Link} className="text-white" id="contactushead" to="/contact-us">CONTACT US<div className="underline"></div></NavLink>
                </NavItem>
                </ul>
                </div>
            </Collapse>
          </Container>
            </Navbar> 
.topnav ul a {
    color: #ffffff;
    text-decoration: none;
    padding: 10px;
    transition: color 0.5s;
}
.topnav ul li .underline {
    height: 3px;
    background-color: transparent;
    width: 0%;
    transition: width 0.2s, background-color 0.5s;
    margin: 0 auto;
}

.topnav ul li.active-link .underline {
    width: 100%;
    background-color: white;
}

.topnav ul li:hover .underline {
    background-color: white;
    width: 100%;
}

.topnav ul li:hover a {
}

.topnav ul li:active a {
    transition: none;
    color: rgba(255,255,255,0.76);
}

.topnav ul li:active .underline {
    transition: none;
    background-color: rgba(255,255,255,0.76);
}

最有可能是一个小错误,但是我已经玩了一段时间,似乎找不到很多解决方案。

enter image description here

如您从图像中看到的,由于已选择主页,因此应在主页上加上下划线。

css reactjs navbar nav
1个回答
0
投票

您是否正在使用react-router-dom的NavLink?

如果是这样,实际上有一个activeClassName道具可以用来识别当前正在访问的路线。

<NavLink tag={Link} className="text-white" exact activeClassName="underline" to="/people-profiles">PEOPLE PROFILES</NavLink>

以及您的CSS中

.underline {
 border-bottom-width: 2px;
}

不要忘记在NavLink的道具中添加精确以与路线正确匹配。

在此处阅读更多信息:https://github.com/ReactTraining/react-router/blob/master/packages/react-router-dom/docs/api/NavLink.md

© www.soinside.com 2019 - 2024. All rights reserved.