带三角形的3D按钮

时间:2018-03-28 09:44:13

标签: css css3 css-transforms

我尝试创建一个带有三角形边缘的3d-ish按钮。 enter image description here

不幸的是,我遇到了三角形部分的问题。底层与顶层不匹配。 我使用边框技术制作三角形。底层应距离顶层4px。

JSFiddle也在底部。

<a class="btn" href="#"><span>Button text here</span></a>

scss:

.btn {
    display: inline-block;
    vertical-align: middle;
    position: relative;
    background: #EAC118;
    padding: 0 30px;
    line-height: 55px;

    span {
        display: block;

        &::after {
            content: '';
            position: absolute;
            top: 0;
            right: -16px;
            width: 0;
            height: 0;
            border-style: solid;
            border-width: 27px 0 28px 16px;
            border-color: transparent transparent transparent #EAC118;
        }
    }

    &::before {
        content: '';
        position: absolute;
        left: 4px;
        bottom: -4px;
        right: -6px;
        height: 100%;
        z-index: -1;
        background: #ff0000;
    }    

    &::after {
        content: '';
        position: absolute;
        bottom: 0;
        right: -22px;
        width: 0;
        height: 0;
        z-index: -1;
        border-style: solid;
        border-width: 24px 0px 28px 16px;
        border-color: transparent transparent transparent #ff0000;
    }
}

https://jsfiddle.net/th2hqmLz/6/

3 个答案:

答案 0 :(得分:1)

这是怎么回事 - 我刚刚让span继承了相同的样式并将其偏移到底部和右边-5px然后再做一个箭头与外部箭头对齐:

setattr
.btn,
.shadow {
  display: inline-block;
  font-weight: 400;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  position: relative;
  background: #EAC118;
  padding: 0 30px;
  text-decoration: none;
  line-height: 56px;
  text-transform: uppercase;
  font-family: Arial;
  color: #ffffff;
}

.btn::after,
.shadow:after,
.shadow:before {
  content: '';
  position: absolute;
  bottom: 0;
  left: 100%;
  width: 0;
  height: 0;
  z-index: 1;
  border-style: solid;
  border-width: 28px 0px 28px 15px;
  border-color: transparent transparent transparent #EAC118;
}

.shadow {
  position: absolute;
  top: 5px;
  left: 5px;
  bottom: -5px;
  right: -5px;
  z-index: -1;
  background: #ff0000;
}

.shadow::after {
  border-color: white white white #ff0000;
  left: calc(100% - 3px);
  z-index: 1;
}

.shadow::before {
  border-color: transparent transparent transparent #ff0000;
  bottom: 5px;
  z-index: 2;
}

.shadow > span {
  display: block;
  width: 5px;
  height: 5px;
  border-top: 5px solid #ffffff;
  background: #ff0000;
  position: absolute;
  top: -5px;
  right: -3px;
  z-index: 3;
}

使用背景图片的示例:

<a class="btn" href="#">
  Button text here
  <span class="shadow"><span></span></span>
</a>
body {
  background-size: cover;
  margin: 0;
  min-height: 100vh;
  background: url(http://www.eelt.org.uk/img/eso1716a.jpg) left top no-repeat;
}

.btn {
  margin:100px;;
  display: inline-block;
  font-weight: 400;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  position: relative;
  background: #EAC118;
  padding: 0 30px;
  text-decoration: none;
  line-height: 56px;
  text-transform: uppercase;
  font-family: Arial;
  color: #ffffff;
}

.btn::before {
  content: '';
  display: block;
  position: absolute;
  height: 0px;
  left: 5px;
  right: 0;
  bottom: -5px;
  border-bottom: 5px solid #A5CAE5;
}

.btn::after {
  content: '';
  position: absolute;
  bottom: -5px;
  top: 0;
  left: 100%;
  width: 15px;
  background: transparent url(https://i.stack.imgur.com/IFGSV.png) top left no-repeat;
  background-size: 100% 100%;
}

答案 1 :(得分:0)

filter: drop-shadow可能会很快简化工作

.btn {
  display: inline-block;
  font-weight: 400;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  position: relative;
  background: #EAC118;
  padding: 0 30px;
  text-decoration: none;
  line-height: 55px;
  text-transform: uppercase;
  font-family: Arial;
  color: #ffffff;
  filter: drop-shadow(6px 4px 0px #ff0000);
}
.btn span {
    display: block;
}
.btn span::after {
    content: '';
    position: absolute;
    top: 0;
    right: -16px;
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 27px 0 28px 16px;
    border-color: transparent transparent transparent #EAC118;
}
<a class="btn" href="#"><span>Button text here</span></a>

从现在开始意识到

  

Chrome和Safari(基于WebKit的浏览器)不支持此参数;如果使用效果将不会呈现。

答案 2 :(得分:0)

这是一个线性渐变的想法:

:root {
  --c: yellow;
  --triangle: 
  linear-gradient(var(--c), var(--c))0 0/calc(100% - 30px) 100% no-repeat, 
  linear-gradient(to bottom left, transparent 50%, var(--c) 0) 100% 0/30px 50% no-repeat, 
  linear-gradient(to top left, transparent 50%, var(--c) 0) 100% 100%/30px 50% no-repeat
}

.box {
  width: 200px;
  height: 40px;
  margin: 20px;
  padding: 10px;
  background: var(--triangle);
  position: relative;
}

.box:before {
  content: "";
  position: absolute;
  top: 5px;
  left: 10px;
  width: 100%;
  height: 100%;
  background: var(--triangle);
  filter: hue-rotate(100deg);
  z-index: -1;
}
<div class="box">
  This is a 3D button
</div>