【问题标题】:Tabs with :target pseudo adding active style w/o Javascript带有 :target 的选项卡添加了没有 Javascript 的活动样式
【发布时间】:2017-02-05 22:43:49
【问题描述】:

我有简单的导航,它在哪里使用 :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}

【问题讨论】:

    标签: css css-selectors pseudo-class


    【解决方案1】:

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

    【讨论】:

    • 我想做 :target::before 并避免 .active 因为它需要涉及一些 javascript。 :target::before 似乎对我不起作用。
    • @Vladyn 可以,但是你需要给元素定位一个id
    • 这样,在我向导航元素添加伪之前添加 target::before,我将失去对 id="bottom" 部分的引用。这是为了破坏导航功能。我希望当部分被定位时 ./.../#bottom 不仅是可见的,而且是对显示该部分的选项卡的引用并将其应用于伪 ::before。
    • @Vladyn 我用第二个示例更新了我的答案,展示了如何使用 :target 伪类来定位 2 个项目......如果你在评论中有其他意思,并且没有关于问题中有一个部分,请接受这个并发布第二个
    • 就这样!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 2011-09-24
    相关资源
    最近更新 更多