【问题标题】:Remove white background from tag shape built in CSS从 CSS 中内置的标签形状中删除白色背景
【发布时间】:2016-06-22 14:21:42
【问题描述】:

我遇到并修改了一个有趣的 sn-p 代码,它使链接看起来像标签。问题是只有在背景为白色时才能正确看到该形状。在我改变背景的那一刻,形状变成矩形并破坏了外观。

请查看代码 sn-p 和 fiddle 示例以获得更好的理解。

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>

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

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

【问题讨论】:

    标签: html css tags shape


    【解决方案1】:

    把css改成这样:

        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; 
        }
    <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>

    【讨论】:

    • 效果很好!谢谢。你能解释一下你所做的改变吗?我会接受答案
    • .tag::after 将背景设置为白色。它只显示在border-color中定义颜色的css三角形,所以我们需要将此背景设置为透明。然后它被包含在标签内,所以我将标签的右边距设置为较高,并将三角形放在标签外。
    • 酷。无论如何,我们可以让标签中的那个小圆圈也变得透明吗?从某种意义上说,我们可以通过它看到背景。我尝试改变东西,但没有成功
    • 因为它在标签之上:您只能将背景颜色设置为与正文背景相同。在 .tag::before
    • 你能在小提琴中改变它吗?我不太确定你在说什么
    【解决方案2】:

        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>

    【讨论】:

    • 酷。再次感谢!
    猜你喜欢
    • 2023-02-24
    • 2019-06-13
    • 2016-01-05
    • 2020-07-26
    • 2010-11-02
    • 2016-08-06
    • 2020-11-09
    • 1970-01-01
    相关资源
    最近更新 更多