Adding CSS Trapezoid Shape :After Button

元气小坏坏 提交于 2019-12-02 08:55:30

I believe the key element missing is that you need to include a content:"" in your :after pseudoclass. See the example below.

.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:"";
}
<div class="btn">Button</div>

This will work - I had to convert your SCSS to CSS, but it's clear enough.

.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>

Unfortunately, you can't have "transparent" overlay, it just wont work. I had to use white for it.

I found a solution that works where the "cut" is transparent. You can use regular background or image background for the button:

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%);
}

I used this generator to get the correct polygon css: http://bennettfeely.com/clippy/

Gradients:

You could use gradients in order to achieve this, and that way you can apply it to any element (this one's done with a button element):

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;
}
<button>PressMe</button>

Pseudo Element (not great for gradient/image backgrounds)

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;
  }
<div>Press Me!</div>

Clip Path

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;
  }
<button>press me!!!</button>

Dynamic length

by using the following snippet, you can make a great button, which isn't affected by length!

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;
}
<button>press me and plus i can get really long!</button>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!