How to create a triangle with CSS that scales to parent div height?

6.7k Views Asked by At

I am currently creating a content block that scales in height dependent on it's content. The block needs to have an arrow at one end that scales to the height of the block.

I would ideally like a pure CSS solution for this if possible. I am currently using the border triangles method: https://css-tricks.com/snippets/css/css-triangle/

This works fine as shown in my fiddle below, but if you increase the height of the div then it doesn't re-scale the triangle to the new height.

https://jsfiddle.net/xq5wwf3h/10/

<div id="triangle-container">
    Some interesting content goes in here!
</div>

body * {
    box-sizing: border-box;
}

#triangle-container {
    position: relative;
    height: 100px;
    width: 100%;
    background: grey;
    margin-left:50px;
    color: #fff;
    padding: 15px;
}

#triangle-container:before {
    content: '';
    position: absolute;
    left: -50px;
    top: 0;
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 50px 50px 50px 0;
    border-color: transparent #007bff transparent transparent;
}
3

There are 3 best solutions below

2
On BEST ANSWER

I have found a solution (albeit not supported in IE) so it may not be the best way depending on circumstances.

The solution uses the background clip property:

https://jsfiddle.net/xq5wwf3h/32/

body * {
    box-sizing: border-box;
}

#triangle-container {
    position: relative;
    height: 100px;
    width: 100%;
    background: grey;
    margin-left:50px;
    color: #fff;
    padding: 15px;
}

#triangle-container:before {
    content: '';
    position: absolute;
    display: block;
    left: -25px;
    top: 0;
    bottom: 0;
    width: 25px;
    height: 100%;
    background: #007bff;
    -webkit-clip-path: polygon(100% 0, 100% 100%, 0 50%);
    clip-path: polygon(100% 0, 100% 100%, 0 50%);
}
4
On

CSS

This is due to the border having to fit in with whatever the triangles height is. Just change with the width in .triangle-left and you will see the responsiveness.

It will only resize up to 500px high though but this should be more than adequate.

.contain {
  width: 100%;
}

/*Left pointing*/
.triangle-left {
    width: 5%;
    height: 0;
    padding-top: 5%;
    padding-bottom: 5%;
    overflow: hidden;
}
.triangle-left:after {
    content: "";
    display: block;
    width: 0;
    height: 0;
    margin-top:-500px;
    
    border-top: 500px solid transparent;
    border-bottom: 500px solid transparent;
    border-right: 500px solid #4679BD;
}
<div class="contain">
  <div class="triangle-left"></div>
</div>
  

SVG

The SVG version just requires positioning.

.contain {
  height: 30px;
}
<div class="contain">
  <svg width="100%" height="100%" viewBox="0 0 500 500">
    <polygon points="0,250 500,0 500,500" style="fill:red;stroke:black;stroke-width:2" />
  </svg>
</div>

0
On

This is my approach using jQuery. It works but you must use a little script :/

https://jsfiddle.net/64w58wL4/

html

<div id="triangle-container">
    <div id="triangle"></div>
    Some interesting content goes in here!
</div>

css

body * {
    box-sizing: border-box;
}

#triangle-container {
    position: relative;
    min-height: 100px;
    width: 100%;
    background: grey;
    margin-left:50px;
    color: #fff;
    padding: 15px;
}

#triangle {
    position: absolute;
    left: -50px;
    top: 0;
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 50px 50px 50px 0;
    border-color: transparent #007bff transparent transparent;
}

jQuery script

$(document).ready(function(){
    var triengleHeight = $('#triangle-container').outerHeight()
    $('#triangle').css('border-width', triengleHeight/2 + 'px 50px ' + triengleHeight/2 + 'px 0')
})