lead Some common triangles on web pages can be drawn directly using CSS without having to be made into pictures or font icons. As stated on Xiaomi’s official website: And from the JD website: So how are these triangles implemented through CSS? Triangle Implementation We can do this by setting the width and height of a div container to 0, and then setting the border of the container. .box { height: 0; width: 0; border-color: wheat skyblue pink rgb(154, 121, 230); border-style: solid; border-width: 40px; } The above code can achieve the following effects: In the above code, the width and height of the box must be 0, the color values of the borders are top, right, bottom, and left, respectively, the borders are solid lines, and the width of the four borders is 40px, thus resulting in 4 isosceles right triangles. The size of the triangle depends on the width of the border. The larger the border-width value, the larger the triangle. If only one of the triangles is needed , this can be achieved by setting the other border colors to be transparent. To achieve the following effects: You can set the color value of border-top, border-bottom, and border-left to transparent to achieve this. .box { height: 0; width: 0; border-color: transparent skyblue transparent transparent; border-style: solid; border-width: 40px; } What if what is needed is not an isosceles triangle? How to achieve it? .box { height: 0; width: 0; border-color: transparent skyblue transparent transparent; border-style: solid; border-width: 80px 40px 0 0; } Then we get the following triangle: Application of small triangle In addition to drawing triangles, it can be achieved through position positioning . Such as the effect of displaying prices on the JD.com website mentioned at the beginning. The trapezoid in this rendering can be achieved by positioning a right triangle as shown below on the right side of the rectangle and setting the background color of the triangle to white. Of course, the small triangle can use pseudo-elements to simplify the web page structure, but pseudo-elements are inline elements and need to be converted into inline block elements or block-level elements first. Summarize When drawing a triangle with CSS, you need to pay attention to the following points:
This is the end of this article about drawing and cleverly applying triangles in CSS. For more relevant CSS triangle drawing content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! |
<<: An article explains Tomcat's class loading mechanism
>>: The shortest JS to determine whether it is IE6 (IE writing method)
Table of contents Implementation ideas There are ...
mysql create table sql statement Common SQL state...
We live in a visual world and are surrounded by m...
GitHub has all kinds of magic tools. Today I foun...
Problem Description A Spring + Angular project wi...
Inline format <colgroup>...</colgroup>...
This article example shares the specific code of ...
Lottie is an open source animation library for iO...
Table of contents Preface Preview text Graphics C...
There are many MySQL variables, some of which are...
Today I had a sneak peek at IE8 beta 1 (hereafter...
Project Background Recently, there is a project w...
1. Download and install Download the community ed...
Table of contents background What is tablespace f...
Preface In today's increasingly convenient In...