Pure CSS Implementation triangles

Source: Internet
Author: User

Read a written question first:

Please use CSS to implement the style as shown in the relevant dimensions, where the DOM structure is:
<div id= "Demo" ></div>

At first I saw this topic, I do not know how to do, before I saw this small triangle, think just a PNG, have not paid attention to this problem.

Don't say much, get to the point: you can understand that triangle as a small square, first set its width and height to 0, and then set its border width of 10px (see).

1 {2width: 0;  3height: 0; 4 border: 10px solid #000; 5 }

This gives a square with a 20px edge.

Next, let's take a look at this example:

1 {2width: 0;  3height: 0; 4 border-top: 40px solid #000; 5 border-right: 40px solid #ff0000; 6 border-left: 40px solid #ff0000; 7 border-bottom: 40px solid #000; 8 }

Results:

Why is this, we can imagine, in addition to the same color of the same side of the same four edges into a square, each side do not want to let, the best way is the average, all have this result.

Now go back to the question above, if only one side has color, the other three edges are transparent (although transparent, but still exist), is not we see a triangle.

{ width: 0; height: 0; border: 10px solid Transparent; border-left: 10px solid #000; position: absolute; top: 18px; Left: 100%; content: ';}

Compared with the topic, basically OK. Just draw a small white triangle inside the black triangle, and it's done.

Full code:

1 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd ">2 <HTMLLang= "en">3 <Head>4     <Metahttp-equiv= "Content-type"content= "Text/html;charset=utf-8">5     <title>Pure CSS Implementation triangles</title>6     <style>7 #triangle{width:96px;Height:96px;Border:2px solid #000;position:relative;}8 #triangle: Before{width:0;Height:0;Border:10px Solid Transparent;Border-left:10px Solid #000;position:Absolute;Top:18px; Left:100%;content:"';}9 #triangle: after{width:0;Height:0;Border:8px Solid Transparent;Border-left:8px Solid #fff;position:Absolute;Top:20px; Left:100%;content:"';}Ten     </style> One </Head> A <Body> -     <DivID= "Triangle"></Div> - </Body> the </HTML>

:

Reference: finally understand the CSS implementation of the triangle icon principle . Push Cool

Any reprint: Please specify the address of this article

Pure CSS Implementation triangles

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.