如何使用CSS3添加另一个选项卡?

时间:2014-04-16 01:25:01

标签: html css css3 tabs

我找到了CSS标签here的示例; 我正在使用第三个例子" target"。但是,当我尝试添加第四个选项卡时,即使使用前3个(工作)选项卡,一切都会崩溃。我的尝试是here;

我相信需要调整以下原始CSS代码,我不知道如何理解;

/* Target Tabs */
.tabs-target span:nth-of-type(1):not(:target) ~ span:nth-of-type(2):not(:target) ~ .tab:nth-of-type(1),
.tabs-target span:nth-of-type(2):target ~ .tab:nth-of-type(2),
.tabs-target span:nth-of-type(1):target ~ .tab:nth-of-type(3)
{
  display: block;
}

我需要调整什么才能让第四个标签正常运行?

2 个答案:

答案 0 :(得分:0)

这有效:

.tabs-target span:nth-of-type(1):not(:target) ~ span:nth-of-type(2):not(:target) ~ span:nth-of-type(3):not(:target) ~ span:nth-of-type(4):not(:target) ~ .tab:nth-of-type(1),
 .tabs-target span:nth-of-type(4):target ~ .tab:nth-of-type(1),
 .tabs-target span:nth-of-type(3):target ~ .tab:nth-of-type(2),
 .tabs-target span:nth-of-type(2):target ~ .tab:nth-of-type(3),
 .tabs-target span:nth-of-type(1):target ~ .tab:nth-of-type(4) {
        display: block;
}

示例fiddle

答案 1 :(得分:0)

通过这个你可以通过添加html

添加任意数量的标签

HTML

<div class="tabs">

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

       <div class="content">
           Tab One
       </div> 
   </div>

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

       <div class="content">
           Tab Two
       </div> 
   </div>

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

       <div class="content">
           Tab Three
       </div> 
   </div>

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

       <div class="content">
           Tab Four
       </div> 
   </div>


</div>

&lt; - 添加更多标签只需完全复制....并相应地更改其值。 - &GT;

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