Detailed explanation of triangle drawing and clever application examples in CSS

Detailed explanation of triangle drawing and clever application examples in CSS

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:

insert image description here

And from the JD website:

insert image description here

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:

insert image description here

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:

insert image description here

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?
As mentioned above, the size of the triangle depends on the value of border-width. The values ​​in the four directions are set to the same value, so the result is an isosceles triangle. By changing the border-width value, you can get right triangles of different sizes.

.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:

insert image description here

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.

insert image description here

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.

insert image description here

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:

  • The width and height of the container must be 0
  • The size of the triangle depends on the border-width value.

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)

Recommend

JavaScript gets the scroll bar position and slides the page to the anchor point

Preface This article records a problem I encounte...

Top 10 Js Image Processing Libraries

Table of contents introduce 1. Pica 2. Lena.js 3....

Prevent HTML and JSP pages from being cached and re-fetched from the web server

After the user logs out, if the back button on the...

A brief discussion on read-only and disabled attributes in forms

Read-only and disabled attributes in forms 1. Rea...

Detailed explanation of the solution to Ubuntu dual system stuck when starting

Solution to Ubuntu dual system stuck when startin...

Practice of implementing custom search bar and clearing search events in avue

Table of contents 1. Customize the search bar con...

Detailed explanation of the use of Vue h function

Table of contents 1. Understanding 2. Use 1. h() ...

Detailed explanation of the new features of ES9: Async iteration

Table of contents Asynchronous traversal Asynchro...

Detailed explanation of the use of router-view components in Vue

When developing a Vue project, you often need to ...

Example of how to exit the loop in Array.forEach in js

Table of contents forEach() Method How to jump ou...

Tutorial on installing rabbitmq using yum on centos8

Enter the /etc/yum.repos.d/ folder Create rabbitm...

Ubuntu20.04 VNC installation and configuration implementation

VNC is a remote desktop protocol. Follow the inst...

Bootstrap 3.0 study notes grid system principle

Through the brief introduction in the previous tw...