添加CSS Trapezoid形状:按钮后

时间:2015-01-20 19:49:52

标签: css html5 css3 sass css-shapes

我正在尝试为客户端创建按钮样式,并且似乎无法使用after伪类使其工作。

<style>
$varBase: 40px;
$imsblue: #012169;
$imsgrey: #012169;

body {
  background:grey;
}

.btn {
  position: relative;
  float: left;
  height: $varBase;
  font-family: sans-serif;
  text-align: center;
  line-height: $varBase;
  color: white;
  white-space: nowrap;
  text-transform: uppercase;
  background: $imsblue;
  &:before {
    float: left;
    content:"";
    width: ($varBase/4);
    height: ($varBase/2);
  }
  &:after {
    position: absolute;
    content:"";
    height: ($varBase/2);
    border-left: ($varBase/2) solid $imsblue;
    border-bottom: ($varBase/2) solid transparent;
  }
  a {
    color: white;
    text-decoration:none;
    padding: ($varBase/4) ($varBase/2);
    margin-right: -10px;
  }
}
.btn3 {
  display: inline;
  color: white;
  background: linear-gradient(135deg, rgba(1,33,105,1) 0%, rgba(1,33,105,1) 93%, rgba(30, 87, 153, 0) 93%, rgba(30, 87, 153, 0) 100%);
  outline: 0;
  border: 0;
  padding: 10px 0px;
  a {
    color: inherit ;
    text-transform: uppercase;
    text-decoration:none;
    padding: ($varBase/4) $varBase;
  }
}
</style>
<div class="btn"><a href="#">Click to Submit</a></div>
<div class="btn3"><a href="#">Click to Submit</a></div>

我可以使用两个DIV来显示它,但我需要这个才能使用一个类。有人能帮我看看我做错了吗?

它应该看起来像这样(当然除了颜色和大小):btn example

4 个答案:

答案 0 :(得分:2)

我认为缺少的关键要素是您需要在content:""伪类中加入:after。请参阅下面的示例。

&#13;
&#13;
.btn {
  height: 40px;
  background: red;
  width: 128px;
  float:left;
}

.btn:after {
    width: 0px;
    height: 20px;
    border-left: 20px solid red;
    border-bottom: 20px solid white;
    float:right;
    content:"";
}
&#13;
<div class="btn">Button</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

这样可行 - 我必须将您的SCSS转换为CSS,但它已经足够清楚了。

.btn {
  height: 40px; width: 200px;
  background: red;
  position: relative; /* work as container */
}
.btn:after {
  content: ''; /* needed */
  display: block;
  position: absolute; /* position to container */
  right: 0; bottom: 0;
  border-left: 20px solid red;
  border-bottom: 20px solid white;
}
<div class="btn">Button</div>

不幸的是,你不能有“透明”叠加,它只是不会工作。我不得不使用白色。

答案 2 :(得分:1)

我找到了一个解决方案,“切割”是透明的。您可以使用常规背景或图像背景作为按钮:

enter image description here

http://jsfiddle.net/q45w2f78/

<div class="buttoncut gon">My button</div>

CSS:

.gon {
  width: 220px;
  height: 220px;
  background: darkblue;
  background-size: 220px 220px;

  /* Text styling */
  line-height: 220px;
  text-align: center;
  font-family: sans-serif;
  font-size: 15px;
  font-weight: bold;
  letter-spacing: 6px;
  color: beige;
}
.gon:hover {
  color: #fff;
  text-shadow: 0 0 10px white;
}

.buttoncut {
  height: 200px;
  -webkit-clip-path: polygon(0% 0%, 100% 0, 100% 50%, 60% 100%, 0% 100%); clip-path: polygon(0% 0%, 100% 0, 100% 50%, 60% 100%, 0% 100%);
  -moz-clip-path: polygon(0% 0%, 100% 0, 100% 50%, 60% 100%, 0% 100%); clip-path: polygon(0% 0%, 100% 0, 100% 50%, 60% 100%, 0% 100%);
  -ms-clip-path: polygon(0% 0%, 100% 0, 100% 50%, 60% 100%, 0% 100%); clip-path: polygon(0% 0%, 100% 0, 100% 50%, 60% 100%, 0% 100%);
  clip-path: polygon(0% 0%, 100% 0, 100% 50%, 60% 100%, 0% 100%); clip-path: polygon(0% 0%, 100% 0, 100% 50%, 60% 100%, 0% 100%);
}

我使用此生成器来获取正确的多边形css:http://bennettfeely.com/clippy/

答案 3 :(得分:1)

梯度:

你可以使用渐变来实现这一点,这样你就可以将它应用于任何元素(这是用按钮元素完成的):

&#13;
&#13;
html,body{
  background:red;
  }

button {
  background: -moz-linear-gradient(-45deg, rgba(30, 87, 153, 1) 0%, rgba(30, 87, 153, 1) 89%, rgba(30, 87, 153, 0) 90%, rgba(30, 87, 153, 0) 100%);
  /* FF3.6+ */
  background: -webkit-gradient(linear, left top, right bottom, color-stop(0%, rgba(30, 87, 153, 1)), color-stop(89%, rgba(30, 87, 153, 1)), color-stop(90%, rgba(30, 87, 153, 0)), color-stop(100%, rgba(30, 87, 153, 0)));
  /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(-45deg, rgba(30, 87, 153, 1) 0%, rgba(30, 87, 153, 1) 89%, rgba(30, 87, 153, 0) 90%, rgba(30, 87, 153, 0) 100%);
  /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(-45deg, rgba(30, 87, 153, 1) 0%, rgba(30, 87, 153, 1) 89%, rgba(30, 87, 153, 0) 90%, rgba(30, 87, 153, 0) 100%);
  /* Opera 11.10+ */
  background: -ms-linear-gradient(-45deg, rgba(30, 87, 153, 1) 0%, rgba(30, 87, 153, 1) 89%, rgba(30, 87, 153, 0) 90%, rgba(30, 87, 153, 0) 100%);
  /* IE10+ */
  background: linear-gradient(135deg, rgba(30, 87, 153, 1) 0%, rgba(30, 87, 153, 1) 89%, rgba(30, 87, 153, 0) 90%, rgba(30, 87, 153, 0) 100%);
  /* W3C */
  filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#1e5799', endColorstr='#001e5799', GradientType=1);
  /* IE6-9 fallback on horizontal gradient */
  
  outline:0;
  border:0;
  padding:5px;
}
&#13;
<button>PressMe</button>
&#13;
&#13;
&#13;


伪元素(对于渐变/图像背景不太好)

&#13;
&#13;
div {
  position: relative;
  display:inline-block;
  padding:5px;
  background:gray;
}
div:after{
  content:"";
  position:absolute;
  border-bottom:10px solid blue;
  border-left:10px solid transparent;
  bottom:0;
  right:0;
  }
html,body{
  background:blue;
  }
&#13;
<div>Press Me!</div>
&#13;
&#13;
&#13;


剪辑路径

&#13;
&#13;
button {
  padding: 10px;
  height: 60px;
  width: 60px;
  -webkit-clip-path: polygon(0% 0%, 100% 0, 100% 75%, 75% 100%, 0% 100%);
  clip-path: polygon(0% 0%, 100% 0, 100% 75%, 75% 100%, 0% 100%);
}
html,body{
  background:green;
  }
&#13;
<button>press me!!!</button>
&#13;
&#13;
&#13;


动态长度

通过使用以下代码段,您可以创建一个不受长度影响的好按钮!

&#13;
&#13;
button {
  position: relative;
  border: 0;
  outline: 0;
  height: 20px;
  background: gray;
}
button:after {
  position: absolute;
  content: "";
  right: -10px;
  width: 0px;
  height: 0px;
  bottom: 0;
  border-bottom: 10px solid transparent;
  border-left: 10px solid gray;
}
button:before {
  position: absolute;
  content: "";
  right: -10px;
  width: 10px;
  height: 10px;
  top: 0;
  background: gray;
}
html,
body {
  background: red;
}

/*HOVER EFFECTS*/
button:hover,
button:hover:before {
  background: yellow;
}
button:hover:after {
  border-left: 10px solid yellow;
}
&#13;
<button>press me and plus i can get really long!</button>
&#13;
&#13;
&#13;