Html/Css Triangle with pseudo elements

The issue is with the use of border. you can check this link How do CSS triangles work? and you will understand how border works and why you get this output.

An alternative solution is to use rotation and border like this :

.box {
  border: 1px solid;
  margin: 50px;
  height: 50px;
  position:relative;
  background: #f2f2f5;
}

.box:before {
  content: "";
  position: absolute;
  width: 20px;
  height: 20px;
  border-top: 1px solid;
  border-left: 1px solid;
  top: -11px;
  left: 13px;
  background: #f2f2f5;
  transform: rotate(45deg);
}
<div class="box">

</div>

And in case you want your box with the arrow to be transparent, here is another trick to achieve it (as the above solution consider solid color as background):

body {
 margin:0;
 background-image:linear-gradient(to right,yellow,pink);
}

.box {
  border: 1px solid;
  border-top:transparent; /*make border-top transparent*/
  margin: 50px;
  height: 50px;
  position:relative;
  /* Use gradient to mimic the border top with a transparent gap */
  background:linear-gradient(to right,black 10px,transparent 10px,transparent 39px,black 39px) top/100% 1px no-repeat;
}

.box:before {
  content: "";
  position: absolute;
  width: 20px;
  height: 20px;
  border-top: 1px solid ;
  border-left: 1px solid;
  top: -11px;
  left: 14px;
  transform: rotate(45deg);
}
<div class="box">

</div>

Here is another version with dashed border:

body {
 margin:0;
 background-image:linear-gradient(to right,yellow,pink);
}

.box {
  border: 1px dashed;
  border-top:transparent; /*make border-top transparent*/
  margin: 50px;
  height: 50px;
  position:relative;
  background:
   repeating-linear-gradient(to right,black 0,black 3px,transparent 3px,transparent 6px) top left/10px 1px,
   repeating-linear-gradient(to right,black 0,black 3px,transparent 3px,transparent 6px) top right/calc(100% - 40px) 1px;
  background-repeat:no-repeat;
}

.box:before {
  content: "";
  position: absolute;
  width: 20px;
  height: 20px;
  border-top: 1px dashed;
  border-left: 1px dashed;
  top: -11px;
  left: 13px;
  transform: rotate(45deg);
}
<div class="box">

</div>

Leave a Comment