CSS 之 :target 伪类实现 Tab 效果

在网页上实现 Tab 效果的次数已数不清,一直都是用 javascript 来实现。因前端知识的匮乏,而没能以实现得更酷。

纯CSS实现Tab主要是基于:target这个伪类实现的,它是CSS3的新特性之一,用来匹配URI中某个标志符的目标元素。URI中的标志符,就是包含一个“#”,然后带有一个标识符名称,比如“#content”,而target就是用来匹配ID为content的元素的。

通常点击带标识符的URI时,页面会跳转到相应的位置,现在还可以CSS来控制点击后的样式。而Tab的实现其实就是控制以该标识符名称为ID的HTML元素的display属性来达到目的的。

:target的用法和其他伪类相同,如

div:target{
/* styles */
}

网上找到个不错的示例,代码稍微精简了一下,效果如下:

123

CSS代码如下:

body {
  font: 12px/20px 'Lucida Grande', Verdana, sans-serif;
  color: #404040;
  background: #eee;
}

.widget {
  position: relative;
  margin: 30px auto 10px;
  width: 320px;
  background: white;
  border: 1px solid #ccc;
  border-radius: 4px;
  -webkit-box-shadow: 0 0 8px rgba(0, 0, 0, 0.07);
  box-shadow: 0 0 8px rgba(0, 0, 0, 0.07);
}

.widget-tabs {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  padding: 9px 12px 0;
  text-align: left;
  direction: rtl;
  background: #f5f5f5;
  border-bottom: 1px solid #ddd;
  border-radius: 3px 3px 0 0;
}

.widget-tab,
.widget-list:target:first-of-type ~ .widget-tabs > .widget-tab:first-child ~ .widget-tab,
.widget-list:target:nth-of-type(2) ~ .widget-tabs > .widget-tab:nth-child(2) ~ .widget-tab,
.widget-list:target:last-of-type ~ .widget-tabs > .widget-tab:last-child ~ .widget-tab {
  position: relative;
  display: inline-block;
  vertical-align: top;
  margin-top: 3px;
  line-height: 36px;
  font-weight: normal;
  color: #999;
  background: #fcfcfc;
  border: solid #ddd;
  border-width: 1px 1px 0;
  border-radius: 5px 5px 0 0;
  padding-bottom: 0;
  bottom: auto;
}

.widget-tab > .widget-tab-link,
.widget-list:target:first-of-type ~ .widget-tabs > .widget-tab:first-child ~ .widget-tab > .widget-tab-link,
.widget-list:target:nth-of-type(2) ~ .widget-tabs > .widget-tab:nth-child(2) ~ .widget-tab > .widget-tab-link,
.widget-list:target:last-of-type ~ .widget-tabs > .widget-tab:last-child ~ .widget-tab > .widget-tab-link {
  margin: 0;
  border-top: 0;
}

.widget-tab + .widget-tab {
  margin-right: -1px;
}

.widget-tab:last-child,
.widget-list:target:first-of-type ~ .widget-tabs > .widget-tab:first-child,
.widget-list:target:nth-of-type(2) ~ .widget-tabs > .widget-tab:nth-child(2),
.widget-list:target:last-of-type ~ .widget-tabs > .widget-tab:last-child {
  bottom: -1px;
  margin-top: 0;
  padding-bottom: 2px;
  line-height: 34px;
  font-weight: bold;
  color: #555;
  background: white;
  border-top: 0;
}

.widget-tab:last-child > .widget-tab-link,
.widget-list:target:first-of-type ~ .widget-tabs > .widget-tab:first-child > .widget-tab-link,
.widget-list:target:nth-of-type(2) ~ .widget-tabs > .widget-tab:nth-child(2) > .widget-tab-link,
.widget-list:target:last-of-type ~ .widget-tabs > .widget-tab:last-child > .widget-tab-link {
  margin: 0 -1px;
  border-top: 4px solid #4cc8f1;
}

.widget-tab-link {
  display: block;
  min-width: 60px;
  padding: 0 15px;
  color: inherit;
  text-align: center;
  text-decoration: none;
  border-radius: 4px 4px 0 0;
}

.widget-list {
  display: none;
  padding-top: 50px;
}

.widget-list:target {
  display: block;
}

.widget-list:target ~ .widget-list {
  display: none;
}

HTML代码如下:

 <body>
  <div>
    <ol id="managers">
      <li>
        <a>
          <img src="http://www.gravatar.com/avatar/47?f=y&amp;s=64&amp;d=identicon">
          Manager #1 <span>481 followers</span>
        </a>
      </li>
    </ol>

    <ol id="designers">
      <li>
        <a>
          <img src="http://www.gravatar.com/avatar/5?f=y&amp;s=64&amp;d=identicon">
          Designer #1 <span>481 followers</span>
        </a>
      </li>
    </ol>

    <ol id="developers">
      <li>
        <a>
          <img src="http://www.gravatar.com/avatar/6?f=y&amp;s=64&amp;d=identicon">
          Developer #1 <span>481 followers</span>
        </a>
      </li>
    </ol>

    <ul>
      <li>
        <a href="#managers">Managers</a>
        <!-- Omitting the end tag is valid HTML and removes the space in-between inline blocks. -->
      <li>
        <a href="#designers">Designers</a>
      <li>
        <a href="#developers">Developers</a>
    </ul>
  </div>
</body>

说一下上面用到的CSS选择器:
直接子选择器: E > F
相邻兄弟选择器: E + F  紧挨着的
一般兄弟选择权: E ~ F  同一个父节点的

平时工作都是偏后端的,偶尔体验一下前端的小而美。

——

我想擦干每个人的眼泪,不再让任何人拉掉别人的一根头发。