css - 带有 :target pseudo adding active style w/o Javascript 的标签

标签 css css-selectors pseudo-class

我有简单的导航,它在哪里用 :target 伪。但是,对于当前选定的选项卡,我有一些样式,例如 .active。我也有 :target 的样式,大致相同。问题是主动选择的选项卡使用一些::before 伪来添加向下箭头。这是我无法与 :target 结合实现的。是否可以? 我想在 :target 上有::before 内容:

        <nav>
            <div class="nav nav-wrapper">
                <ul role="navigation">
                    <li><a href="#top" class="active">Top 50</a></li>
                    <li><a href="#mid">Mid</a></li>
                    <li><a href="#bottom">Bottom</a></li>
                    <li><a href="#about">About</a></li>
                </ul>
            </div>
        </nav>

.nav ul li a:hover, 
.nav ul li a.active, 
.nav ul li a:target {
    background-color: white;
    color: #585858;
    position: relative;
}

.nav ul li a::before {
    content: " ";
    position: absolute;
    top: 0;
    left: 50%;
    ...
}


.nav ul li a.active::before {// will obviously not work, but it is something I can achieve}

最佳答案

当然它可以做 .active::before,你也可以做 :target::before,如下例所示

.nav ul li a:hover,
.nav ul li a.active,
.nav ul li a:target {
  background-color: white;
  color: #585858;
  position: relative;
}
.nav ul li a::before {
  content: " ";
  position: absolute;
  top: 0;
  left: 100%;
}
.nav ul li a:target::after {
  content: " ...Hey there, 123";
  white-space: nowrap;
}
<nav>
  <div class="nav nav-wrapper">
    <ul role="navigation">
      <li><a href="#top" class="active">Top 50</a>
      </li>
      <li><a href="#mid">Mid</a>
      </li>
      <li><a href="#bottom" id="bottom">Bottom - click me to show :target::before</a>
      </li>
      <li><a href="#about">About</a>
      </li>
    </ul>
  </div>
</nav>


更新了基于评论的示例,如何使用 :target 伪类定位 2 个元素

.nav ul li a:hover,
.nav ul li a.active,
.nav ul li a:target {
  background-color: white;
  color: #585858;
  position: relative;
}
.nav ul li a::before {
  content: " ";
  position: absolute;
  top: 0;
  left: 100%;
}
div:target ~ nav #bottom1::after,
div:target ~ #bottom2::after {
  content: " ...Hey there, 123";
  white-space: nowrap;
}
<div id="bottom"></div>

<nav>
  <div class="nav nav-wrapper">
    <ul role="navigation">
      <li><a href="#top" class="active">Top 50</a>
      </li>
      <li><a href="#mid">Mid</a>
      </li>
      <li><a href="#bottom" id="bottom1">Bottom - click me to show :target::before</a>
      </li>
      <li><a href="#about">About</a>
      </li>
    </ul>
  </div>
</nav>

<section id="bottom2"></section>

关于css - 带有 :target pseudo adding active style w/o Javascript 的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42050507/

相关文章:

html - 链接 :visited doesn't work with sibling combinator

css - 在 Sass 中使用带有变量的伪类

css - 将 div 高度设置为 100%

javascript - react.js - 处理固定页眉和页脚的 React-router

javascript - IE9/10/11/Firefox 中的 iFramed 页面在页面大小变化时不会更新 document.scrollHeight?

javascript - querySelectorAll 在 Firefox Addon 中返回包含空对象的数组

css - 是否有包含输入的输入字段的 CSS 选择器?

html - Logo 和导航栏内联

css - 是:not(:hover) and :hover a safe way to hide accessible elements?

html - :not() is not ignoring class in list-items with links