【发布时间】:2016-09-30 11:51:45
【问题描述】:
我想要一个如下图所示的 3d 对角浮雕按钮:
我的第一个想法是使用阴影,然后使用::before和::after添加两个小三角形来缩小差距。
但它总是略微错位小于 1 个像素(参见下面的 sn-p)。我使用了不同的颜色以便于编码。
.button {
border: 1px solid red;
box-shadow: -10px 10px red;
display: inline-block; margin: 30px; position: relative; outline: none; font-size: 40px;
padding: 10px 30px; background-color: #333; font-weight: 700;
color: white; letter-spacing: 1px;
line-height: 1; text-transform: uppercase; text-align: center;
}
/* create triangle */
.button::before { content: ""; position: absolute;
top: -1px;
left: -11px;
width: 0; height: 0; font-size: 0; line-height: 0%; border-style: solid; border-color: transparent; border-width: 0 0 11px 11px; border-bottom-color: red; }
.button::after { content: ""; position: absolute;
right: -1px;
bottom: -11px;
width: 0; height: 0; font-size: 0; line-height: 0%; border-style: solid; border-color: transparent; border-width: 11px 11px 0 0; border-top-color: red; }
<a class="button">Gallery</a>
有没有比使用三角形和绝对定位更好的实现方式?
【问题讨论】:
-
我看不出你的图片和小提琴结果有什么区别。
-
这能解决轻微的错位吗,我不确定你在追求什么 - jsfiddle.net/Ls2qsjhc
-
@MoshFeu 如果我放大得刚好够,就会出现这种轻微的错位,imgur.com/DxfuU6d。这是介于 -1px 和 -2px 之间,但我尝试了 1.1、1.2 直到 1.9,但没有任何匹配项。
-
this 适合你吗?
-
@Harry 哇,太棒了!介意在答案部分解释更多吗?我猜它实际上是一个方形 div,但你把渐变的形状像三角形?
标签: html css css-shapes linear-gradients