Example code for implementing triangles and arrows through CSS borders

Example code for implementing triangles and arrows through CSS borders

1. CSS Box Model

The box includes: margin, border, padding, content
The borders have smooth oblique lines at their junctions. By using this feature, you can get small triangles and the like by setting the width and color of each border.
The div element is a block-level element and is displayed as a block box, which can be used for specific implementation.

<div class="triangle"></div>
<div class="arrow"></div>

**Example 1, **Generally, after setting the height, width and border, the box will appear as follows:

.triangle {
   width: 25px;  
   height: 25px;  
   overflow: hidden;
   font-size: 0;
   line-height: 0;            
   border-width: 50px;               
   border-style: solid;
   border-color: rgb(235, 54, 241) rgb(86, 245, 139) rgb(76, 0, 255) rgb(46, 205, 245);
}

Note: overflow, font-size, and line-height are set because IE6 will have the default font size and line height, causing the box to appear as a stretched long rectangle.

Example 2: After setting the width and height in Example 1 to 0, the box appears as follows:

.triangle {
  width: 0;  
  height: 0;  
  overflow: hidden;
  font-size: 0;
  line-height: 0;            
  border-width: 50px;               
  border-style: solid;
  border-color: rgb(235, 54, 241) rgb(86, 245, 139) rgb(76, 0, 255) rgb(46, 205, 245);
}

At this point, you can see that the box is made up of four triangles. If you keep only one color and set the other three colors to be transparent or the same color as the background, you can achieve a triangle. Depending on the edges you choose to leave in different positions, you can present triangles with different orientations.

Example 3: Keep only the bottom edge

.triangle {
   width: 0;  
   height: 0;  
   overflow: hidden;
   font-size: 0;
   line-height: 0;            
   border-width: 50px;               
   border-style: solid;
   border-color: transparent transparent rgb(76, 0, 255) transparent;
}

Example 4: The width and height in Example 3 are retained to obtain a trapezoid

width: 0;  
height: 0;

Example 5: Implementing Arrows

The arrow is actually achieved by stacking two triangles at an offset position.
Cover the blue triangle with a white triangle offset by 1px to form an arrow.

The following style implements an upward arrow:

. arrow {
   position: absolute;
}
. arrow:before,. arrow:after{
   position: absolute;
   content: '';
   border-top: 10px transparent solid;
   border-left: 10px transparent solid;
   border-right: 10px transparent solid;
   border-bottom: 10px #fff solid;
}
. arrow:before{
   border-bottom: 10px #0099CC solid;
}
. arrow:after{
   top: 1px; /*Override and stagger 1px*/
   border-bottom: 10px #fff solid;
}

Summarize

The above is the example code that I introduced to you to realize triangles and arrows through CSS borders. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

<<:  Detailed explanation of the misunderstanding between MySQL and Oracle

>>:  Summary of clipboard.js usage

Recommend

Detailed explanation of nginx's default_server definition and matching rules

The default_server directive of nginx can define ...

React implements the addition, deletion, modification and query of todolist

Table of contents Take todolist as an example The...

MySQL learning database search statement DQL Xiaobai chapter

Table of contents 1. Simple retrieval of data 2. ...

MySQL conditional query and or usage and priority example analysis

This article uses examples to illustrate the usag...

A brief discussion on spaces and blank lines in HTML code

All consecutive spaces or blank lines (newlines) ...

JavaScript example code to determine whether a file exists

1. Business Scenario I have been doing developmen...

CSS sets Overflow to hide the scroll bar while allowing scrolling

CSS sets Overflow to hide the scroll bar while al...

Detailed steps and problem solving methods for installing MySQL 8.0.19 on Linux

I recently bought a Tencent Cloud server and buil...

Summary of common tool functions necessary for front-end development

1. Time formatting and other methods It is recomm...

Three ways to prevent MySQL from inserting duplicate data

Create a new table CREATE TABLE `person` ( `id` i...

How to install and deploy zabbix 5.0 for nginx

Table of contents Experimental environment Instal...

Detailed explanation of mixed inheritance in Vue

Table of contents The effect of mixed inheritance...

Summary of Docker configuration container location and tips

Tips for using Docker 1. Clean up all stopped doc...

Pure CSS header fixed implementation code

There are two main reasons why it is difficult to...

How to use VirtualBox to simulate a Linux cluster

1. Set up HOST on the host Macbook The previous d...