【问题标题】:CSS button - transparent arrow on both sidesCSS 按钮 - 两边透明箭头
【发布时间】:2016-08-23 15:30:57
【问题描述】:

我正在尝试通过使用前后的伪元素来创建下面的按钮。 我不想操作 HTML DOM。我正在寻找纯 CSS 的解决方案。

如何使这些三角形的当前白色边框颜色透明?

//编辑: marked topic 没有回答我的问题,因为我需要一个不同的角度而不是仅仅旋转一个正方形。它也不透明。我不想操纵 DOM。

//已解决:这是使用 Kate Millers 解决方案后的结果:

//EDIT2:我现在使用的解决方案还有一个问题:

有没有办法修复三角形(左右)的边框宽度? 在这里您可以看到大小如何更改为 14.4 和 26.4px、26.4px:

【问题讨论】:

  • 我不应该自我推销,但你看过我在那个帖子中的回答吗?它不使用 SVG,并且角度也不像正方形那样完全为 90 度。它使用两个透视旋转的伪元素。
  • 你也可以看看这个 - stackoverflow.com/questions/38006705/…。你的问题不是这个问题的欺骗,但它会让你了解如何创建角度不是 90 度的三角形。
  • 谢谢@Harry,你的解决方案看起来不错,但我有两个问题:1. 固定宽度 2. 改变宽度后角度发生变化。
  • 不客气@SimonHagmann。是的,您可以删除固定宽度。只需添加display: inline-block 使其与文本一样宽。但需要固定高度。还有一点需要注意的是,三角形的角度会根据元素的宽度而变化,这在这种方法中是不可避免的。
  • 我需要每边有 2 个三角形 - 仅使用一个 DOM 元素是不可能的。固定高度不会有问题,但角度必须相同。我想我必须得到更多的答案才能得到正确的解决方案。

标签: css less css-shapes


【解决方案1】:

最好的解决方案是反转三角形(因此您要添加与按钮匹配的顶部和底部三角形,但不在侧面)。从技术上讲,您可以制作“透明”三角形,但不能将这种透明度应用于不同的对象。

我更改的最重要的事情之一是按钮的背景颜色和填充必须应用于 span 元素(这意味着每个按钮都需要一个内部 span),而不是 .btn。

如果您将所有关于按钮的 CSS 替换为以下内容,您将获得一个解决方案,至少可以实现 90% 的目标。角度并不完美,因为它在文本处停止。如果你想让角度真正完美,你可能需要做一些绝对定位,这会随着按钮大小的变化而变得混乱。

您也可以实现此目的的非代码方法是创建一个 .png 或 .svg,其中包含与您的按钮颜色匹配的三角形,并将它们插入到 :before 和 :after 中,内容为:' ';

body { margin: 20px; background:#c2c2c2; }

.btn {
  display: inline-block;
  text-shadow: 0 1px 0 #000;
  font-weight: bold;
  text-transform: uppercase;
}

.btn {
  padding: 11px 40px;
  color: #fff;
  position: relative;
  background: #a00;
}

.btn:before, .btn:after {
  content: '';
  border-top: 20px solid #a00;
  border-bottom: 20px solid #a00;
  position: absolute;
  top: 0;
}

.btn:before {
  border-left:20px solid transparent;
  left: -20px;
}

.btn:after {
  border-right:20px solid transparent;
  right:-20px;
}


.btn.inset:before, .btn.inset:after {
  content: '';
  border-top: 20px solid transparent;
  border-bottom: 20px solid transparent;
  position: absolute;
  top: 0;
}

.btn.inset:before {
  border-right:20px solid #a00;
  left: -40px;
}

.btn.inset:after {
  border-left:20px solid #a00;
  right:-40px;
}
<div class="btn">Text in my little banner button</div>
<div class="btn inset">Text in my little banner button</div>

【讨论】:

  • 感谢 Kate 提供的解决方案,但仍然存在对 DOM 要素 (span) 的操作。有没有可能去掉这个标签?
  • 是的!我更新了它,现在你不需要跨度标签,它使角度完美。您只需要确保 left: 和 right: 是您制作的任何内容的负数 border-right: 或 border-left: 值。
  • 好的,谢谢你的提示。我也可以只显示此形状的边框还是使用标记主题 (OP) 中的代码?
  • 我忽略了.is--primary { border-radius: 0; background: #a9825c; border: 0 none; } .is--large { height: 53px; padding: 0px 60px 0px 60px; },但我的其余代码应该替换您的其余代码。
  • 它适用于我的代码。我还需要像这篇文章link 一样只使用边框创建相同的按钮。我也可以用你的代码来解决这个问题吗?提到的帖子有一个固定的宽度和一个可变的角度..
【解决方案2】:

我不使用,也不是很熟悉,LESS...但是您可以使用跨度和带有伪元素的包装器来创建类似的元素。每边确实需要 2 个三角形(因此是跨度)。

body { margin: 20px; background: #ddd; }

.btn {
  display: inline-block;
  padding: 11px 40px;
  color: #fff;
  position: relative;
  background: #a00;
  text-shadow: 0 1px 0 #000;
  font-weight: bold;
  text-transform: uppercase;
}

.btn:after,
.btn span:after,
.btn:before,
.btn span:before {
  content: "";
  width: 0;
  height: 0;
  display: block;
  position: absolute;
  right: 0;
  border: 10px solid transparent;
}
.btn:after {
  top: 0;
  border-right-color: #ddd;
  border-bottom-color: #ddd;
  }
.btn span:after {
  bottom: 0;
  border-right-color: #ddd;
  border-top-color: #ddd;
}
.btn:before {
  top: 0;
  left:0;
  border-left-color: #ddd;
  border-bottom-color: #ddd;
  }
.btn span:before {
  bottom: 0;
  left:0;
  border-left-color: #ddd;
  border-top-color: #ddd;
}
&lt;div class="btn"&gt;&lt;span&gt;Text in my little banner button&lt;/span&gt;&lt;/div&gt;

我意识到末端并不是真的透明的,它们只是将背景颜色匹配为出现透明。

【讨论】:

  • 感谢您的回答@Scott。箭头并不是真正透明的,因此形状和背景之间仍然存在空白。在这种情况下,我也无法更改 DOM 结构。
  • 没问题,西蒙。我不知道你怎么能用真正透明的末端和在不使用图像/精灵的情况下改变DOM来实现这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-21
  • 2013-09-12
  • 2013-01-13
  • 1970-01-01
  • 1970-01-01
  • 2015-09-20
相关资源
最近更新 更多