I’m using HTML and CSS and trying to split a webpage into 4 triangles that join in the middle of the screen.
So far there is the best result I got.
body {
margin: 0;
padding: 0;
}
.container {
position: relative;
height: 100vh;
overflow: hidden;
}
.triangle {
position: absolute;
width: 0;
height: 0;
z-index: 1;
}
.top {
top: 0;
left: 50%;
transform: translateX(-50%);
border-left: 50vw solid transparent;
border-right: 50vw solid transparent;
border-bottom: 100vh solid #FF5733;
}
.left {
top: 50%;
left: 0;
transform: translateY(-50%);
border-top: 50vh solid transparent;
border-bottom: 50vh solid transparent;
border-right: 100vw solid #C70039;
}
.bottom {
bottom: 0;
left: 50%;
transform: translateX(-50%);
border-left: 50vw solid transparent;
border-right: 50vw solid transparent;
border-top: 100vh solid #581845;
}
.right {
top: 50%;
right: 0;
transform: translateY(-50%);
border-top: 50vh solid transparent;
border-bottom: 50vh solid transparent;
border-left: 100vw solid #900C3F;
}
@media (max-width: 767px) {
.triangle {
width: 100vw;
height: 0;
}
.top, .left, .bottom, .right {
border-style: solid;
border-width: 0;
}
.top {
border-color: #FF5733 transparent transparent transparent;
border-bottom-width: 50vh;
transform: none;
}
.left {
border-color: transparent #C70039 transparent transparent;
border-right-width: 50vw;
transform: none;
}
.bottom {
border-color: transparent transparent #581845 transparent;
border-top-width: 50vh;
transform: none;
}
.right {
border-color: transparent transparent transparent #900C3F;
border-left-width: 50vw;
transform: none;
}
}
Every time i try to get those half width everything got messed up.
The idea is the display 4 triangle that join in the middle
How can i achieve such resluts ?
Thank you very much
2
Answers
You almost had it. For each side, set that side position to 0 (i.e., for top, set
top: 0
, etc.). Then just give each side’s corresponding border width to 50% of the viewport height for the top and bottom triangles, and 50% of the viewport width for the left and right triangles.The best part is that since you’re using viewport lengths, there’s no media query needed. It will always fill the viewport.
Of course this can also be done with just one element too…
I think
clip-path
is how I would do it, rather than fussing around with borders.