简体   繁体   English

子组合子和 ::after 伪元素

[英]Child combinator and ::after pseudo-element

Well, that should be quite simple.嗯,那应该很简单。 I have the following CSS snippet:我有以下 CSS 片段:

.nav-item > .nav-link:not(.active)::after {
  content: "test";
  display: block;
  width: 0;
  border-bottom: solid 2px #019fb6;
  transition: width .3s;
}

However, I'd like to do something like that:但是,我想做这样的事情:

.nav-item::after > .nav-link:not(.active) {
  content: "test";
  display: block;
  width: 0;
  border-bottom: solid 2px #019fb6;
  transition: width .3s;
}

And finally:最后:

.nav-item:hover::after > .nav-link:not(.active) {
  width: 100%;
}

I'm trying to create a hover effect that only works on nav-items that doesn't have the "active" class.我正在尝试创建一个仅适用于没有“活动”类的导航项目的悬停效果。 But, that ins't working.但是,那行不通。 What am I doing wrong?我究竟做错了什么?

Html:网址:

<li class="nav-item"><a class="nav-link text-left" href="#">Second Item</a></li>
<li class="nav-item"><a class="nav-link text-left active" href="#">FirstItem</a></li>

Thanks in advance.提前致谢。

The main issue is that, at the time of your post, CSS doesn't allow you to go up .主要问题是,在您发帖时,CSS 不允许您使用up It seems you're trying to detect an active class at the nested <a> level and apply something to the pseudo content of <a> 's parent element.您似乎正在尝试检测嵌套的<a>级别的活动类,并将某些内容应用于<a>父元素的伪内容。 Instead, move the active class to the parent ( li ) level.相反,将active类移动到父 ( li ) 级别。 Then you can control the pseudo content.然后就可以控制伪内容了。 Note that I added overflow: hidden to the un-hovered state, so that the content was not visible.请注意,我添加了overflow: hidden到 un-hovered 状态,因此content不可见。 In addition, I added opacity to the transition to make it a little smoother looking.此外,我为过渡添加了opacity ,使其看起来更平滑。

 .nav-item:not(.active)::after { content: "test"; display: block; width: 0; opacity: 0; border-bottom: solid 2px #019fb6; transition: width 0.3s, opacity 0.3s; overflow: hidden; } .nav-item:not(.active):hover::after { width: 100%; opacity: 1; }
 <ul> <li class="nav-item active"> <!-- active moved here --> <a class="nav-link text-left" href="#">FirstItem</a></li> <li class="nav-item"> <a class="nav-link text-left" href="#">Second Item (active)</a></li> </ul>

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

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