如何在CSS中创建带角度角的div

时间:2016-08-15 18:00:07

标签: html css css-shapes

我需要在我的div处创建带角度弯角(四舍五入)的div,边长为2px:

enter image description here

我需要一个仅限CSS的解决方案



.center-link {
    text-align: center;
}

.continue {
    text-decoration: none;
    border: 2px solid #26368d;
    border-radius: 10px;
    background-color: transparent;
    margin: 0 auto;
    padding: 10px 10px 9px;
    font-family: 'Raleway';
    text-transform: uppercase;
    font-size: 11px;
    font-weight: bold;
    color: #26368d;
    transition: all 0.5s ease;
}

<div class="center-link"><a href="#" class="continue">Продолжить читать</a></div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

您可以使用两个span用于左右边框,然后在这些跨度上使用:before:after来创建角落。

&#13;
&#13;
a {
  text-decoration: none;
  white-space: nowrap;
  margin: 10px;
}
.el {
  display: inline-flex;
  align-items: center;
  justify-content: space-between;
  border-top: 1px solid black;
  border-bottom: 1px solid black;
  margin: 50px;
  position: relative;
}
span {
  height: 20px;
  position: relative;
}
span.left {
  border-left: 1px solid black;
}
span.right {
  border-right: 1px solid black;
}
.left {
  margin-left: -10px;
}
.right {
  margin-right: -10px;
}
span:before,
span:after {
  content: '';
  position: absolute;
  width: 1px;
  background: black;
  height: 15px;
}
.left:before {
  transform: rotate(40deg);
  top: -13px;
  left: 4px;
}
.left:after {
  transform: rotate(-40deg);
  bottom: -13px;
  left: 4px;
}
.right:before {
  transform: rotate(-40deg);
  top: -13px;
  right: 4px;
}
.right:after {
  transform: rotate(42deg);
  bottom: -13px;
  left: -5px;
}
&#13;
<div class="el">
  <span class="left"></span>
  <a href="">Lorem ipsum dolor.</a>
  <span class="right"></span>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您应该能够通过使用倾斜和透视来保持命中测试的准确性,从而允许您创建此倾斜按钮而不会影响用户体验。

添加以下内容:

     transform: perspective(25px) rotateY(45deg); 

快速演示将是:

&#13;
&#13;
button {
  border: 0;
  border-top: 5px solid tomato;
  border-bottom: 5px solid tomato;
  color: tomato;
  margin-left: 20px;
  margin-right: 20px;
  background: transparent;
  padding: 10px;
  outline: none;
  padding-left: 50px;
  padding-right: 50px;
  position: relative;
  cursor: pointer;
}
button:before {
  content: "";
  position: absolute;
  top: -5px;
  left: 100%;
  height: 100%;
  width: 20px;
  background: inherit;
  transform: perspective(25px) rotateY(45deg);
  transform-origin: center left;
  border-top: 5px solid tomato;
  border-right: 8px solid tomato;
  border-bottom: 5px solid tomato;
}
button:after {
  content: "";
  position: absolute;
  top: -5px;
  right: 100%;
  height: 100%;
  width: 20px;
  background: inherit;
  transform: perspective(25px) rotateY(-45deg);
  transform-origin: center right;
  border-top: 5px solid tomato;
  border-left: 8px solid tomato;
  border-bottom: 5px solid tomato;
}
&#13;
<button>Button</button>
&#13;
&#13;
&#13;

作为旁注,通过将重复规则放在单个选择器(button:before, button:after{}

中,可以缩短此解决方案并提高效率。