Home Web Front-end HTML Tutorial css triangle icon compatible with various browsers_html/css_WEB-ITnose

css triangle icon compatible with various browsers_html/css_WEB-ITnose

Jun 24, 2016 am 11:41 AM

CSS triangle icon compatible with various browsers

On currently popular websites, we often see some small triangle drop-down prompts (drop-down menu at the top of Weibo) , a simple way can be to use a picture instead, but with the development of front-end technology and developers' "nitpicking" about front-end performance, more and more front-end developers are beginning to "return to their original nature" in scenarios where pictures can not be used. To reduce the use of images, another advantage of CSS icons over images is that we can easily customize the size and color of the icons.

Implementing triangle icons with css is not a new technology. There have been many related technical articles before. This article mainly analyzes the problems encountered when using it in actual scenarios and how to avoid these problems.
The basic principles are similar. Here we mainly introduce the use of css border (of course, you can also use the rotation technology of css3, which will not be covered here due to compatibility issues).

css border implementation

The border of a div or element is not a line with height in our intuitive sense, but a equal-height trapezoid or triangle (when the width and height are 0), You can take a look at the effect:
div definition:

<div class="arrow1"></div>
Copy after login

css:

.arrow1{ width: 0px; height:0px; border-width: 30px; border-style: solid; border-color: #007998 #47A447 #3C6AB9 #D2322D; }
Copy after login

Final effect:

You can see that the border in each direction is a triangle, so we only need to set the borders in the corresponding directions to be transparent or hide them to get a triangle in any direction. If we want to get a drop-down icon, we can change the left and right borders of the border to transparent. The css changes are as follows:

.arrow1{ width: 0px; height:0px; border-width: 30px; border-style: solid; border-color: #007998 transparent transparent transparent; }
Copy after login

Take another look at the effect:

Note: The transparent attribute is used to set the background-color option in the background to the background color transparent

bingo! This is the effect we want, but it is a mess under ie6!

The long black frame makes it difficult for people to look directly at it. This is because IE6 does not support the transparent attribute. At this time, we can set the border style of the corresponding area to dashed. Dashed has a large border width. will be hidden. The css is modified as follows:

  .arrow1{ width: 0px; height:0px; border-width: 30px; border-style: solid; border-color: #007998 transparent transparent transparent; }
Copy after login

The effect is as follows (view under ie6):

However, it is not over yet, we set a shadow to see the final result Effect:

Even if we see that a triangle we need has been generated, the occupied height of the triangle is still the original height, which will cause the effect of moving up when used with other elements. At this time, we need to set the height of the lower border to 0:

  .arrow1{ width: 0px; height:0px; border-width: 30px 30px 0; border-style: solid; border-color: #007998 transparent transparent transparent; }
Copy after login

Let’s take a look at the effect:

still seems not very friendly when used. We still need to modify the corresponding color. Can we display the corresponding color according to the color set by the parent element? We need to modify the border-color:

.arrow1{ width: 0px; height:0px; line-height: 0px; border-width: 30px 30px 0; border-style: solid dashed dashed dashed; border-left-color: transparent; border-right-color: transparent; }
Copy after login

Effect (using the current font color):

Of course, in addition to using css border generation, we You can also use the special character ◇ to generate overlay positioning, or you can use CSS3 rotation to generate it (hack processing is required under IE6). Using borders is a common and simple and compatible way

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

See all articles