The principle of border realizing triangle

Original link

Foreword: The most common way to realize triangles on the Internet is to control borders, so why is it possible?
principle

Let's first take a look at the form of border.

box{
width:100px;
height:100px;
background:yellow;
border-top: 20px solid red;
border-right:20px solid black;
border-bottom:20px solid green;
border-left:20px solid blue;
}

write picture description here

Observing the above figure, it can be found that the border is trapezoidal. When reducing the width and height of the box, the following changes will occur:
write picture description here

It is easy to see from the above figure that when the width of the box is reduced to a very small value, that is, the upper edge of the trapezoid of the border is reduced to a very small value. So think about it, when this value drops to 0, the border becomes a triangle. As shown below:

image

So we can get the desired triangle by setting the element width and height to 0 and controlling the border.
accomplish

Setting borders that do not require orientation to transparent can be used to implement triangles. For example, if you want to achieve a lower triangle, you can set border-left, border-bottom, and border-right to transparent.
copy code

box{
width:0px;
height:0px;
border-top: 20px solid red;
border-right:20px solid transparent;
border-bottom:20px solid transparent;
border-left:20px solid transparent;
}

write picture description here

box{
width:0px;
height:0px;
border-top: 20px solid red;
border-right:20px solid transparent;
border-bottom:20px solid transparent;
border-left:20px solid red;

write picture description here

box{
width:0px;
height:0px;
border-top: 60px solid red;
border-right:20px solid transparent;
border-bottom:0px solid transparent;
border-left:20px solid transparent;
}

write picture description here

box{
width:100px;
height:100px;
border-top: 60px solid red;
border-right:20px solid transparent;
border-bottom:0px solid transparent;
border-left:20px solid transparent;
}

write picture description here

Not to list them one by one, as long as you understand that the border in each direction is a triangle, you can obtain various triangles and trapezoids by adjusting the size and color/transparency of the border.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326629437&siteId=291194637