I am trying to create a triangle shape to be within its parent with a 100% width of whatever the parent's width would be.
This is what I tried:
* {
margin:0;
padding:0;
}
.test {
width: 500px;
height: 500px;
background-color: green;
position: relative;
box-sizing: border-box;
}
.triangle-topleft {
width: 0;
height: 0;
border-top: 100px solid red;
border-right: 100vw solid transparent;
position: absolute;
box-sizing: border-box;
}
<div class="test">
<div class="triangle-topleft"></div>
</div>
So, as you can see, I have the child div within the parent (absolute position). Why is the triangle shape still going out of the parent? Is there a way where I can make the triangle be 100% width?
Most of the answers here are about box-sizing: border-box
, but it does not work.
Anyone knows how to fix this OR is there a better way of making a top triangle other than using borders?