Using CSS to Create Triangles

With CSS it’s super easy to create shapes out of pretty much any div. Some shapes are pretty straightforward to code, like squares and rectangles. Even circles are just squared with a border-radius set to 100%. Triangles, however, are a little more complicated to create. In order to make them, you have to manipulate the border property. For a triangle that points up, the border-left should be 50px solid transparent and the border-right should be the same — that will create the lines that come to a point. Then make sure the border-bottom is 100px solid #9fd175 (or whatever the color of your choice may be — also, the numbers are arbitrary: as long as the border-left and right px size is half of the border-bottom size, it should work to create a triangle shape). See the code below:

  1. .triangle{
  2. width: 0;
  3. height: 0;
  4. }
  6. .triangle.up{
  7. border-left: 50px solid transparent;
  8. border-right: 50px solid transparent;
  9. border-bottom: 100px solid #9fd175;
  10. }

Screen Shot 2016-09-09 at 8.04.01 PM

Join us in our newest publication:

To create a triangle pointing down, the border properties will need to be tweaked a bit to make the point face downwards:

  1. .down{
  3. border-left: 50px solid transparent;
  4. border-right: 50px solid transparent;
  5. border-top: 100px solid #9fd175;
  7. }

Screen Shot 2016-09-09 at 8.04.49 PM

See the code below for code that will create triangles that point to the left or to the right:

  1. .left{
  3. border-top: 50px solid transparent;
  4. border-right: 100px solid #9fd175;
  5. border-bottom: 50px solid transparent;
  7. }
  9. .right{
  11. border-top: 50px solid transparent;
  12. border-left: 100px solid #9fd175;
  13. border-bottom: 50px solid transparent;
  15. }

Screen Shot 2016-09-09 at 8.08.01 PM

Screen Shot 2016-09-09 at 8.08.12 PM

Now that you’ve mastered creating triangles, use them to add some dimension to your next projects!

Share and Enjoy !

0 0