html – 仅限css3选项卡预选标签

我正在尝试使用:target伪类创建一个仅限
CSS3的选项卡

HTML:

<div class="tabs_wrapper" style="">
    <article class="tabs">
        <section id="tab1">
            <h2><a href="#tab1">Tab 1</a></h2>
            <p>This content appears on tab 1.</p>
        </section>

        <section id="tab2">
            <h2><a href="#tab2">Tab 2</a></h2>
            <p>This content appears on tab 2.</p>
        </section> 
    </article>
</div>

CSS:

article.tabs section:target h2
{
    color: #f2f2f2;
    background-color:#b8b63e;

}
article.tabs section:target{
    z-index: 2;
}

FIDDLE:https://jsfiddle.net/vchawla26/gxt708e7/

一切都完成了.

仅面临一个问题:如果没有单击选项卡且目标为空,则最初在加载时,选项卡不会突出显示:(

最佳答案 您可以使用单选按钮和:checked class来创建它.

它不使用:target,因此不会有任何页面跳转.

正如您在下面的代码示例中所看到的,每个选项卡都包含单选按钮(用于功能),其中隐藏了display:none,标签作为选项卡和内容div.

HTML

<div class="tabs_wrapper">
   <div class="tabs">

   <div class="tab">
       <input type="radio" id="tab-1" name="tab-group-1" checked>
       <label for="tab-1">Tab 1</label>

       <div class="content">
           <p>This content appears on tab 1.</p>
       </div> 
   </div>

   <div class="tab">
       <input type="radio" id="tab-2" name="tab-group-1">
       <label for="tab-2">Tab 2</label>

       <div class="content">
           <p>This content appears on tab 2.</p>
       </div> 
   </div>

    <div class="tab">
       <input type="radio" id="tab-3" name="tab-group-1">
       <label for="tab-3">Tab 3</label>

       <div class="content">
           <p>This content appears on tab 3.</p>
       </div> 
   </div>

</div>

内容区域以绝对定位位于彼此之上,当选中单选按钮时,使内容区域位于z-index的顶部.

CSS

.tabs {
  position: relative;   
  min-height: 200px; /* This part sucks */
  clear: both;
  margin: 25px 0;
}
.tab {
  float: left;
}
.tab label {
  background: #eee; 
  padding: 10px; 
  border: 1px solid #ccc; 
  margin-left: -1px; 
  position: relative;
  left: 1px; 
}
.tab [type=radio] {
  display: none;   
}
.content {
  position: absolute;
  top: 28px;
  left: 0;
  background: white;
  right: 0;
  bottom: 0;
  padding: 20px;
  border: 1px solid #ccc; 
}
[type=radio]:checked ~ label {
  background: white;
  border-bottom: 1px solid white;
  z-index: 2;
}
[type=radio]:checked ~ label ~ .content {
  z-index: 1;
}

DEMO

点赞