Skip to content Skip to sidebar Skip to footer

How To Make Border Inside Triangle?

i am making block with arrow and border looks like And i have tried this. How can i make block like image? And can we make this arrow height responsive?

Solution 1:

I would consider a mix of skew transformation, inset box-shadow and some linear-gradient:

* {
  box-sizing: border-box;
}

.block-arr {
  padding: 50px;
  margin: 20px;
  margin-right: 100px;
  position: relative;
  background: linear-gradient(#fff, #fff)2px 0/2px 100% no-repeat, purple;
  border-left: 2px solid purple;
  z-index: 0;
}

.block-arr:before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 50%;
  left: 0;
  right: 0;
  background: purple;
  border: 5px solid purple;
  border-bottom: none;
  border-left: none;
  box-shadow: -2px 2px 0px #fff inset;
  transform: skew(25deg);
  transform-origin: top left;
  z-index: -1;
}

.block-arr:after {
  content: "";
  position: absolute;
  top: 50%;
  bottom: 0;
  left: 0;
  right: 0;
  background: purple;
  border: 5px solid purple;
  border-top: none;
  border-left: none;
  box-shadow: -2px -2px 0px #fff inset;
  transform: skew(-25deg);
  transform-origin: bottom left;
  z-index: -1;
}
<div class="block-arr">
  <strong>Main Heading</strong>
  <span>Sub Heading</span>
</div>
<div class="block-arr">
  <strong>Main Heading</strong><br/>
  <span>Sub Heading</span>
</div>
<div class="block-arr">
</div>

And here is a more compressed version with some CSS variable to easily handle color. You can also do the same to handle others variables:

* {
  box-sizing: border-box;
}

.block-arr {
  --c1:purple;
  --c2:#fff;
  padding: 50px;
  margin: 20px;
  margin-right: 100px;
  position: relative;
  background: linear-gradient(var(--c2), var(--c2))2px 0/2px 100% no-repeat, var(--c1);
  border-left: 2px solid var(--c1);
  z-index: 0;
}

.block-arr:before,
.block-arr:after {
  left: 0;
  right: 0;
  content: "";
  position: absolute;
  background: var(--c1);
  border: 5px solid var(--c1);
  border-left: none;
  z-index: -1;
}

.block-arr:before {
  top: 0;
  bottom: 50%;
  border-bottom: none;
  box-shadow: -2px 2px 0px var(--c2) inset;
  transform: skew(25deg);
  transform-origin: top left;
}

.block-arr:after {
  top: 50%;
  bottom: 0;
  border-top: none;
  box-shadow: -2px -2px 0px var(--c2) inset;
  transform: skew(-25deg);
  transform-origin: bottom left;
}
<div class="block-arr">
</div>

<div class="block-arr" style="--c1:red;--c2:yellow">
  <strong>Main Heading</strong>
  <span>Sub Heading</span>
  <p>And yes it is reponsive and grow when height grow</p>
</div>

BONUS

Another fancy and more complex way with only linear-gradient:

* {
  box-sizing: border-box;
}

.block-arr {
  --c1:purple;
  --c2:#fff;
  padding: 50px;
  margin: 20px;
  margin-right: 100px;
  position: relative;
  border:1px solid;
  background:
  linear-gradient(to top left,transparent calc(50% + 4px),var(--c2) calc(50% + 4px),var(--c2) calc(50% + 6px),transparent 0) 100% 100%/50px 50% no-repeat,
  linear-gradient(to bottom left,transparent calc(50% + 4px),var(--c2) calc(50% + 4px),var(--c2) calc(50% + 6px),transparent 0) 100% 0/50px 50% no-repeat,
  linear-gradient(var(--c2),var(--c2)) 4px calc(100% -  4px)/calc(100% - 58px) 2px no-repeat,
  linear-gradient(var(--c2),var(--c2)) 4px 4px/calc(100% - 58px) 2px no-repeat,
  linear-gradient(var(--c2),var(--c2)) 4px 4px/2px calc(100% - 8px) no-repeat,
  linear-gradient(to top left ,transparent 50%,var(--c1) 50%) 100% 100%/50px 50% no-repeat,
  linear-gradient(to bottom left,transparent 50%,var(--c1) 50%) 100% 0/50px 50% no-repeat,
  linear-gradient(var(--c1),var(--c1)) 0 0/calc(100% - 50px) 100%  no-repeat;
}
<div class="block-arr">
</div>

Solution 2:

Using :after and :before pseudo elements, i have made this design.

Hope it fulfills your requirement.

Thanks

CSS and HTML:

* {
    box-sizing: border-box;
}
p { margin:0; }
.block-arr {
    background: purple;
    margin: 20px;
    margin-right: 100px;
    position: relative;
}

.block-arr .inner {
    min-height: 100px;
    /*display: flex;*/
    padding: 20px;
    align-items: center;
    position: relative;
}

.block-arr .inner:after {
    border-top: 50px solid transparent;
    border-bottom: 50px solid transparent;
    border-left: 50px solid purple;
    content: '';
    position: absolute;
    left: 100%;
    top: 0;
}

.block-arr:after {
    border-top: 50px solid transparent;
    border-bottom: 50px solid transparent;
    border-left: 50px solid purple;
    content: '';
    position: absolute;
    left: 100%;
    top: 0;
}
.bordered { position:relative; border:1px solid #fff; border-right:none; display: flex; align-items: center; padding:20px; }
.bordered:before, .bordered:after {
    content: "";
    position: absolute;
    height: 72%;
    width: 1px;
    background: #fff;
    top: 0;
    right: 0;
    z-index: 4;
}
.bordered:before {
    transform: rotate(45deg);
    top: auto;
    right: -3.3%;
    bottom: -11%;
}
.bordered:after {
    transform: rotate(-45deg);
    top: -12%;
    right: -3.3%;
}
<div class="block-arr">
    <div class="inner"><div class="bordered">
        <p><strong>Main Heading</strong>
        <span>Sub Heading</span></p>
        
    </div></div>
</div>

Post a Comment for "How To Make Border Inside Triangle?"