Detailed explanation of techniques for drawing triangular arrow patterns using CSS

高洛峰
Release: 2017-03-08 14:14:45
Original
1635 people have browsed it

I want to modify this website recently, and I want to put a prompt box on it. This is easy enough, but I want the tooltip to have a triangular arrow. However, it is almost a disaster when I think about the need to use pictures and prepare countless arrows of various colors and directions. Fortunately, Darren Waddell, the core developer of MooTools, told me about a great technique: drawing triangular arrows with CSS. Using pure CSS, you only need very little code to create triangular arrows that are compatible with various browsers!

CSS code

/* create an arrow that points up */
p.arrow-up {   
 width: 0;    
 height: 0;    
 border-left: 5px solid transparent;  /* left arrow slant */
 border-right: 5px solid transparent; /* right arrow slant */
 border-bottom: 5px solid #2f2f2f; /* bottom, add background color here */
 font-size: 0;   
 line-height: 0;   
}   

/* create an arrow that points down */
p.arrow-down {   
 width: 0;    
 height: 0;    
 border-left: 5px solid transparent;   
 border-right: 5px solid transparent;   
 border-top: 5px solid #2f2f2f;   
 font-size: 0;   
 line-height: 0;   
}   

/* create an arrow that points left */
p.arrow-left {   
 width: 0;    
 height: 0;    
 border-bottom: 5px solid transparent;  /* left arrow slant */
 border-top: 5px solid transparent; /* right arrow slant */
 border-right: 5px solid #2f2f2f; /* bottom, add background color here */
 font-size: 0;   
 line-height: 0;   
}   

/* create an arrow that points right */
p.arrow-rightright {   
 width: 0;    
 height: 0;    
 border-bottom: 5px solid transparent;  /* left arrow slant */
 border-top: 5px solid transparent; /* right arrow slant */
 border-left: 5px solid #2f2f2f; /* bottom, add background color here */
 font-size: 0;   
 line-height: 0;   
}
Copy after login

The key to drawing these triangles is that you want the two sides in the direction of the arrow to have Very thick borders. The side facing away from the arrow also has the same thick border, and the color of this side is the color of your triangle. The thicker the border, the larger the triangle. This way you can draw arrows of various colors, sizes, and orientations. The best part is, you only need a few lines of CSS code to achieve this effect.

Use :before and :after to draw CSS triangles

The above CSS examples use real page elements to draw, but sometimes the real elements are also It is used, but you can't go on it and operate it directly. What should I do? Pure CSS triangles can actually be drawn using pseudo-elements. Here's how to draw it:

p.tooltip {   
 /* tooltip content styling in here; nothing to do with arrows */
}   

/* shared with before and after */
p.tooltip:before, p.tooltip:after {   
 content: ' ';   
 height: 0;   
 position: absolute;   
 width: 0;   
 border: 10px solid transparent; /* arrow size */
}   

/* these arrows will point up */

/* top-stacked, smaller arrow */
p.tooltip:before {   
 border-bottom-color: #fff;  /* arrow color */

 /* positioning */
 position: absolute;   
 top: -19px;   
 left: 255px;   
 z-index: 2;   
}   

/* arrow which acts as a background shadow */
p.tooltip:after {   
 border-bottom-color: #333;  /* arrow color */

 /* positioning */
 position: absolute;   
 top: -24px;   
 left: 255px;   
 z-index: 1;   
}
Copy after login

The color of the border on the side facing away from the arrow is the color of the triangle arrow. You don't need to use both :before and :after pseudo-elements to draw this arrow - one is enough. And the other one, you can use it as a background shadow or background edge for the previous one.

I really should have known about this technology earlier! I believe this simple and hassle-free technology will come in handy when making interface improvements in the future.

The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.

The above is the detailed content of Detailed explanation of techniques for drawing triangular arrow patterns using CSS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
css
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template