CSS3 Implementing triangles

Source: Internet
Author: User

Most of the time we use the triangle effect:

We can use CSS3 to achieve this effect, how to do it? First of all, we define an empty div, set the div width to 0, give the div a 100px border (here is easy to observe), get a square, the color and the definition of the border color is the same, we see the square is actually this div border.

<id= "D1"></div>style:    #d1 {        width:0;         height:0;         border:100px solid red;    }

Implementation results:

So what does this element now look like for each border? Let's see:

Corresponding Code:

<id= "D1"></div>style:    #d1 {        width:0;         height:0;        border-width:50px;        Border-style:solid;        border-color:red  Blue  black  yellow;
Upper right Down left
}

From the above we found that in fact, the width of the 0 div of each border is a triangle, the actual operation we may only need a triangle, then we just need to hide the unwanted border (with the Transparent property):

For example, I want a downward triangle:

Corresponding Code:

<id= "D1"></div>style:    #d1 {        width:0;         height:0;        border-width:50px;        Border-style:solid;        border-color:transparent transparent transparent;
Upper right lower left }

It can be seen that only the corresponding border hidden, the corresponding border-color order is:

Border-color: Upper right down left; Each color is separated by a space.

A 45-degree triangle:

Corresponding Code:

<id= "D1"></div>style:    #d1 {        width:0;         height:0;        border-width:50px;        Border-style:solid;       border-color:transparent transparent red red;
Border-color:transparent transparent red blue;
}

At the same time we can adjust the width of the corresponding border to adjust the shape of the triangle:

  

Corresponding Code:

Here we have modified the width of the border below

<id= "D1"></div>style:    #d1 {        width:0;         height:0;        border-width:50px;        border-bottom-width:150px;        Border-style:solid;        border-color:transparent transparent red transparent;        Border-color:blue green red Black;    }      

At the same time, we can draw a triangle with rounded corners:

Corresponding Code:

In order to look more comfortable here, add the transparency attribute;

<id= "D1"></div>style:        #d1 {        width:0;         height:0;        border-width:50px;        Border-style:solid;         border-top-left-radius:15px;        opacity:0.569;        border-color:red transparent transparent red;    }

About the production of the triangle is basically these, many times with pseudo-class before, after use will have unexpected effect:

For example, the label of the login window I wrote myself:

CSS3 Implementing 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.