如何创建具有尖锐边缘的div?

时间:2019-07-25 11:32:22

标签: html css css-shapes

您能帮我做这个div吗?

我的代码:

body{
        display: flex;
        justify-content: center;
      }
      #talkbubble {
      width: 160px;
      height: 80px;
      background: #bc0a14;
      position: relative;
      -moz-border-radius: 10px;
      -webkit-border-radius: 10px;
      border-radius: 10px;
    }
    #talkbubble:before {
      content: "";
      position: absolute;
      right: 100%;
      top: 26px;
      width: 0;
      height: 0;
      border-top: 13px solid transparent;
      border-right: 26px solid #bc0a14;
      border-bottom: 13px solid transparent;
    }
    #talkbubble:after {
      content: "";
      position: absolute;
      left: 100%;
      top: 26px;
      width: 0;
      height: 0;
      border-top: 13px solid transparent;
      border-left: 26px solid #bc0a14;
      border-bottom: 13px solid transparent;
    }
<div id="talkbubble"></div>

我想在图像中创建具有相同样式的div

2 个答案:

答案 0 :(得分:3)

这是一个具有伪元素和径向渐变的想法。我使用CSS变量轻松调整形状,但这不是强制性的

.box {
  width: 100px;
  height: 50px;
  margin:0 var(--w,20px);
  display:inline-block;
  border-radius: 15px;
  background: var(--c,red);
  position: relative;
}

.box:before,
.box:after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  width: var(--w,20px);
  right:calc(100% - 2px);
  background:
   radial-gradient(107% 100% at top    left,transparent 96%,var(--c,red) 100%) top, 
   radial-gradient(107% 100% at bottom left,transparent 96%,var(--c,red) 100%) bottom;
  background-size:100% 50.1%;
  background-repeat:no-repeat;
}
.box:after {
  left:calc(100% - 2px);
  right:auto;
  transform:scaleX(-1);
}
<div class="box"></div>

<div class="box" style="--c:blue;--w:30px;"></div>

<div class="box" style="--c:purple;--w:10px;height:60px"></div>

答案 1 :(得分:0)

请尝试以下代码:

body{
        display: flex;
        justify-content: center;
      }
      #talkbubble {
         width: 180px;
    height: 54px;
    background: #bc0a14;
    position: relative;
    -moz-border-radius: 20px;
    -webkit-border-radius: 20px;
    border-radius: 24px;
    }
    #talkbubble:before {
          content: "";
    position: absolute;
    right: 99%;
    top: 17px;
    width: 0px;
    height: 1px;
    border-top: 9px solid transparent;
    border-right: 10px solid #bc0a14;
    border-bottom: 9px solid transparent;
    }
    #talkbubble:after {
      content: "";
    position: absolute;
    left: 99%;
    top: 17px;
    width: 0;
    height: 1px;
    border-top: 9px solid transparent;
    border-left: 10px solid #bc0a14;
    border-bottom: 9px solid transparent;
    }
<div id="talkbubble"></div>