Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a transparent triangle with border using CSS?

How can I create the following shape with CSS?

Enter image description here

I tried this to be a solution:

 .triangle:after {
        position:absolute;
        content:"";
        width: 0;
        height: 0;
        margin-top:1px;
        margin-left:2px;
        border-left: 10px solid transparent;
        border-right: 10px solid transparent;
        border-bottom: 10px solid white;
    }

    .triangle:before {
        position:absolute;
        content:"";
        width: 0;
        height: 0;
        border-left: 12px solid transparent;
        border-right: 12px solid transparent;
        border-bottom: 12px solid black;
    }

You can see it working at Triangle. This is working, but with a trick of borders. Is there another way it could be done?

Using SVG vectors this can be done easily, but I don't want to go that lengthy way.

like image 783
Manoz Avatar asked Apr 26 '13 07:04

Manoz


People also ask

How do you make a border transparent in CSS?

Step 1: Create a div tag. Step 2: Specify the border-style property to be double to set two borders around the box. Step 3: Set the background-clip property to padding-box which clips the background color to the padding of the element.


2 Answers

I've found a webkit-only solution, using the ▲ character:

.triangle {
  -webkit-text-stroke: 12px black;
  color: transparent;
  font-size: 200px;
}
<div class="triangle">&#9650;</div>

Extras:

  • CanIUse reference for text-stroke - all major browsers covered as of 2019
  • CSS-tricks article
  • Useful HTML shapes
like image 158
Bigood Avatar answered Oct 06 '22 13:10

Bigood


CSS-border version:

.triangle {
    position: relative;
    width:0;
    border-bottom:solid 50px black;
    border-right:solid 30px transparent;
    border-left:solid 30px transparent;
}
.triangle .empty {
    position: absolute;
    top:9px;
    left:-21px;
    width:0;
    border-bottom:solid 36px white;
    border-right:solid 21px transparent;
    border-left:solid 21px transparent;
}

Adding a white triangle inside the black one: http://jsfiddle.net/samliew/Hcfsx/

like image 41
2 revs Avatar answered Oct 06 '22 14:10

2 revs