从CSS

时间:2016-06-22 14:21:43

标签: html css tags shape

我遇到并修改了一段有趣的代码片段,使链接看起来像标签。问题是只有当背景为白色时才能正确显示形状。当我改变背景时,形状变成矩形并破坏外观。

请查看代码段和小提琴示例,以便更好地理解。

body {
  font: 12px/1.5 'PT Sans', serif;
  margin: 25px;
  background: blue; /*the moment background is white, it's ok*/
}

.tags {
  list-style: none;
  margin: 0;
  overflow: hidden; 
  padding: 0;
}

.tags li {
  float: left; 
}

.tag {
  background: #eee;
  border-radius: 3px 0 0 3px;
  color: #999;
  display: inline-block;
  height: 26px;
  line-height: 26px;
  padding: 0 20px 0 23px;
  position: relative;
  margin: 0 10px 10px 0;
  text-decoration: none;
  -webkit-transition: color 0.2s;
}

.tag::before {
  background: #fff;
  border-radius: 10px;
  box-shadow: inset 0 1px rgba(0, 0, 0, 0.25);
  content: '';
  height: 6px;
  left: 10px;
  position: absolute;
  width: 6px;
  top: 10px;
}

.tag::after {
  background: #fff;
  border-bottom: 13px solid transparent;
  border-left: 10px solid #eee;
  border-top: 13px solid transparent;
  content: '';
  position: absolute;
  right: 0;
  top: 0;
}

.tag:hover {
  background-color: crimson;
  color: white;
}

.tag:hover::after {
   border-left-color: crimson; 
}
<h2>Example 2</h2>
<ul class="tags">
  <li><a href="#" class="tag">HTML</a></li>
  <li><a href="#" class="tag">CSS</a></li>
  <li><a href="#" class="tag">JavaScript</a></li>
</ul>

在上面的代码中,当您在第4行CSS中将background更改为白色时,它可以正常工作。

如何让它在所有背景颜色上运行?

2 个答案:

答案 0 :(得分:5)

将css更改为:

&#13;
&#13;
    body {
      font: 12px/1.5 'PT Sans', serif;
      margin: 25px;
      background: blue; /*the moment background is white, it's ok*/
    }
    
    .tags {
      list-style: none;
      margin: 0;
      overflow: hidden; 
      padding: 0;
    }
    
    .tags li {
      float: left; 
    }
    
    .tag {
      background: #eee;
      border-radius: 3px 0 0 3px;
      color: #999;
      display: inline-block;
      height: 26px;
      line-height: 26px;
      padding: 0 20px 0 23px;
      position: relative;
      margin: 0 20px 10px 0;
      text-decoration: none;
      -webkit-transition: color 0.2s;
    }
    
    .tag::before {
      background: #fff;
      border-radius: 10px;
      box-shadow: inset 0 1px rgba(0, 0, 0, 0.25);
      content: '';
      height: 6px;
      left: 10px;
      position: absolute;
      width: 6px;
      top: 10px;
    }
    
    .tag::after {
      background: transparent;
      border-bottom: 13px solid transparent;
      border-left: 10px solid #eee;
      border-top: 13px solid transparent;
      content: '';
      position: absolute;
      right: -10px;
      top: 0;
    }
    
    .tag:hover {
      background-color: crimson;
      color: white;
    }
    
    .tag:hover::after {
       border-left-color: crimson; 
    }
&#13;
<h2>Example 2</h2>
<ul class="tags">
  <li><a href="#" class="tag">HTML</a></li>
  <li><a href="#" class="tag">CSS</a></li>
  <li><a href="#" class="tag">JavaScript</a></li>
</ul>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

    body {
      font: 12px/1.5 'PT Sans', serif;
      margin: 25px;
      background: blue; /*the moment background is white, it's ok*/
    }
    
    .tags {
      list-style: none;
      margin: 0;
      overflow: hidden; 
      padding: 0;
    }
    
    .tags li {
      float: left; 
    }
    
    .tag {
      background: #eee;
      border-radius: 3px 0 0 3px;
      color: #999;
      display: inline-block;
      height: 26px;
      line-height: 26px;
      padding: 0 20px 0 23px;
      position: relative;
      margin: 0 20px 10px 0;
      text-decoration: none;
      -webkit-transition: color 0.2s;
    }
    
    .tag::before {
      background: blue;
      border-radius: 10px;
      box-shadow: inset 0 1px rgba(0, 0, 0, 0.25);
      content: '';
      height: 6px;
      left: 10px;
      position: absolute;
      width: 6px;
      top: 10px;
    }
    
    .tag::after {
      background: transparent;
      border-bottom: 13px solid transparent;
      border-left: 10px solid #eee;
      border-top: 13px solid transparent;
      content: '';
      position: absolute;
      right: -10px;
      top: 0;
    }
    
    .tag:hover {
      background-color: crimson;
      color: white;
    }
    
    .tag:hover::after {
       border-left-color: crimson; 
    }
<h2>Example 2</h2>
<ul class="tags">
  <li><a href="#" class="tag">HTML</a></li>
  <li><a href="#" class="tag">CSS</a></li>
  <li><a href="#" class="tag">JavaScript</a></li>
</ul>