简体   繁体   English

如何更改活动链接上的下划线颜色 - 反应

[英]how to change underline color on active link - react

I have some logic for styling the active link in React and I have CSS for an animated underline on nav links.我有一些逻辑来为 React 中的活动链接设置样式,并且我有 CSS 用于导航链接上的动画下划线。 The problem is that I don't know how to change the color of the underline when the link is active.问题是当链接处于活动状态时,我不知道如何更改下划线的颜色。

In CSS, I think the selector would be .nav-link:after:active在 CSS 中,我认为选择器将是.nav-link:after:active

How can I change the color of the underline when the link is active?链接处于活动状态时如何更改下划线的颜色?

const isActive = (history, path) => {
  if (history.location.pathname === path) {
    return { color: "#ff7315" };
  } else {
    return { color: "#232020" };
  }
};

styles.css styles.css

  .nav-item { 
    display: table-cell; 
    position: relative; 
    padding: 15px 0;
  }
  .nav-link {
    display: inline-block;
    position: relative;
  }
  .nav-link:after {    
    background: none repeat scroll 0 0 transparent;
    bottom: 0;
    content: "";
    display: block;
    height: 2px;
    left: 50%;
    position: absolute;
    background: #3a3535;
    transition: width 0.3s ease 0s, left 0.3s ease 0s;
    width: 0;
  }

  .nav-link:hover:after { 
    width: 100%; 
    left: 0; 
  }

You can do something like this with the one you've provided.你可以用你提供的那个做这样的事情。

 .nav-item { position: relative; padding: 15px 0; }.nav-link { display: inline-block; position: relative; text-decoration: none; color: #000; }.nav-link.active { color: blue }.nav-link:after { background: none repeat scroll 0 0 transparent; bottom: 0; content: ""; display: block; height: 2px; left: 50%; position: absolute; background: #000; transition: width 0.3s ease 0s, left 0.3s ease 0s; width: 0; }.nav-link:hover:after, .nav-link.active:after { width: 100%; left: 0; background: blue; }
 <div class="nav-item"> <a class="nav-link" href="#">Some Link 1</a> </div> <div class="nav-item"> <a class="nav-link active" href="#">Some Link 2</a> </div>

I'm not sure if you're using react-router or not.我不确定您是否使用 react-router。 But if you are you can do it using NavLink like但如果你是,你可以使用NavLink来做到这一点

<NavLink
   className="link-item"
   activeStyle={{ color: 'blue' }}
   to="/"
>
  Link
</NavLink>

Hope this helps !希望这可以帮助 !

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

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