标签:target伪添加活动样式没有Javascript

时间:2017-02-05 09:35:19

标签: css css3 css-selectors pseudo-class

我有简单的导航,它在哪里用:target伪。但是我有一些当前选中的标签样式,如.active。我也有样式:目标大致相同。问题是Actively selected选项卡使用some :: before伪来添加向下箭头。这是我无法实现的目标:目标。可能吗? 我想在内容之前::: target:

        <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}

1 个答案:

答案 0 :(得分:2)

当然它有效.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>