响应式CSS三角形,百分比宽度

sdv*_*ksv 31 css geometry css3 responsive-design css-shapes

下面的代码将在<a>元素正下方创建一个箭头:

的jsfiddle

.btn {
    position: relative;
    display: inline-block;
    width: 100px;
    height: 50px;
    text-align: center;
    color: white;
    background: gray;
    line-height: 50px;
    text-decoration: none;
}
.btn:after {
    content: "";
    position: absolute;
    bottom: -10px;
    left: 0;
    width: 0;
    height: 0;
    border-width: 10px 50px 0 50px;
    border-style: solid;
    border-color: gray transparent transparent transparent;   
}
Run Code Online (Sandbox Code Playgroud)
<a href="#" class="btn">Hello!</a>
Run Code Online (Sandbox Code Playgroud)

问题是我们必须指示链接宽度以获得适当大小的箭头,因为我们无法以像素为单位指示边框宽度.

如何制作响应三角形百分比?

web*_*iki 52

您可以使用倾斜和旋转的伪元素在链接下创建响应三角形:

DEMO(调整结果窗口的大小以查看它是如何反应的)

三角形保持它与padding-bottom属性的纵横比.

如果您希望形状根据其内容调整其大小,则可以删除.btn类中的宽度

.btn {
  position: relative;
  display: inline-block;
  height: 50px; width: 50%;
  text-align: center;
  color: white;
  background: gray;
  line-height: 50px;
  text-decoration: none;
  padding-bottom: 15%;
  background-clip: content-box;
  overflow: hidden;
}
.btn:after {
  content: "";
  position: absolute;
  top:50px;  left: 0;
  background-color: inherit;
  padding-bottom: 50%;
  width: 57.7%;
  z-index: -1;
  transform-origin: 0 0;
  transform: rotate(-30deg) skewX(30deg);
}
/** FOR THE DEMO **/

body {
  background: url('http://i.imgur.com/qi5FGET.jpg');
  background-size: cover;
}
Run Code Online (Sandbox Code Playgroud)
<a href="#" class="btn">Hello!</a>
Run Code Online (Sandbox Code Playgroud)

有关响应三角形以及如何制作它们的更多信息,您可以查看 具有变换旋转的三角形(简单和花式响应三角形)

  • +1用于深入理解CSS功能. (8认同)
  • 虽然这个解决方案在这种情况下有效,但我只想向其他人指出它只能在白色(或纯色)背景上工作,因为你的 border-top:300px solid #fff; 用于隐藏 :after 旋转方块的其余部分。 (2认同)
  • @GreatBlakes thx指出它,我已经解决了问题并编辑了答案.顶部边框不再是必需的,``after`伪元素的其余部分用`overflow:hidden;`隐藏. (2认同)

Pro*_*cop 14

另一个解决方案是使用CSS剪辑路径从彩色块中剪切三角形.但是没有IE支持,但可以用于内部工具等.

DEMO

用SCSS编写以方便使用.

.outer {
  background: orange;
  width: 25%;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 1em;

  p {
    margin: 0;
    text-align: center;
    color: #fff;
  }

  &:after {
    content: '';
    position: absolute;
    top: 100%;
    left: 0; 
    right: 0;
    padding-bottom: 10%;
    background: orange;
    -webkit-clip-path: polygon(0% 0%, 100% 0%, 50% 100%);
    clip-path: polygon(0% 0%, 100% 0%, 50% 100%);
  }

}
Run Code Online (Sandbox Code Playgroud)


Ale*_*tin 5

我发现适用于任何宽度/高度的解决方案.你可以使用两个带linear-gradient背景的伪元素,像这样,(小提琴):

.btn {
    position: relative;
    display: inline-block;
    width: 100px;
    height: 50px;
    text-align: center;
    color: white;
    background: gray;
    line-height: 50px;
    text-decoration: none;
}
.btn:before {
    content: "";
    position: absolute;
    top: 100%;
    right: 0;
    width: 50%;
    height: 10px;
    background: linear-gradient(to right bottom, gray 50%, transparent 50%)
}

.btn:after {
    content: "";
    position: absolute;
    top: 100%;
    left: 0;
    width: 50%;
    height: 10px;
    background: linear-gradient(to left bottom, gray 50%, transparent 50%)
}
Run Code Online (Sandbox Code Playgroud)

  • 这是一个不错的主意,但边缘真的"撕裂" (2认同)