在顶部和底部创建一个倾斜按钮

Creating a slanted button on top and bottom

我正在尝试创建一个带有两条斜线的按钮。我管理的从左下角到右中心的一个。下一张需要从左上往右

左边的高度是50px右边的高度应该是30px

.slantedButton {
  position: relative;
  box-sizing: border-box;
  padding: 5px;
  height: 30px;
  background-color: #3c50a2;
  line-height: 15px;
  color: white;
  width: 150px;
  z-index: 1000;
}

.slantedButton:after {
  position: absolute;
  width: 100%;
  height: 100%;
  content: '';
  z-index: -1;
  background-color: inherit;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  transform-origin: top right;
  transform: skewY(-4deg);
}
<div class="slantedButton">Hello World!</div>

我尝试用 :before 来做到这一点,但没有成功。

非常感谢您的建议。

非常感谢。

它可以使用 ::before 进行查找。只需更改 transform-origin:

.slantedButton {
  position: relative;
  box-sizing: border-box;
  padding: 5px;
  height: 30px;
  background-color: #3c50a2;
  line-height: 15px;
  color: white;
  width: 150px;
  margin: 30px;
  z-index: 1;
}

.slantedButton:before, .slantedButton:after {
  position: absolute;
  width: 100%;
  height: 100%;
  content: '';
  z-index: -1;
  background-color: inherit;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
}

.slantedButton:before {
    transform-origin: top right;
    transform: skewY(4deg);
}

.slantedButton:after {
  transform-origin: top right;
  transform: skewY(-4deg);
}
<div class="slantedButton">Hello World!</div>