Languages
[Edit]
EN

CSS - create triangle figure

6 points
Created by:
Inayah-Alexander
767

 In this short article, we would like to show how in simple way create triangle figures using only CSS.

Triangle figures created using pure CSS.
Triangle figures created using pure CSS.

Practical example

In this section, you can find practical example that shows how to create equilateral triangles.

Source code:

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>
    
    div {
        margin: 10px;
        display: inline-block;
    }
    
    .triangle-up {
        width: 0; 
        height: 0; 
        border-left: 20px solid transparent;
        border-right: 20px solid transparent;
        border-bottom: 35px solid blue;
    }
    
    .triangle-down {
        width: 0; 
        height: 0; 
        border-left: 20px solid transparent;
        border-right: 20px solid transparent;
        border-top: 35px solid blue;
    }

    .triangle-left {
        width: 0; 
        height: 0; 
        border-top: 20px solid transparent;
        border-bottom: 20px solid transparent;
        border-right: 35px solid blue;
    }

    .triangle-right {
        width: 0; 
        height: 0; 
        border-top: 20px solid transparent;
        border-bottom: 20px solid transparent;
        border-left: 35px solid blue;
    }
    
  </style>
</head>
<body>
  <div class="triangle-up"></div>
  <div class="triangle-down"></div>
  <div class="triangle-left"></div>
  <div class="triangle-right"></div>
</body>
</html>

Style template:

$size: 20px;
$color: blue;

.triangle-up {
    width: 0; 
    height: 0; 
    border-left: $size solid transparent;
    border-right: $size solid transparent;
    border-bottom: round(1.732050807568877 * $size) solid $color;
}

 

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join