Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Create a Triangle Using CSS clip-path?
The CSS clip-path property allows you to create various shapes by clipping an element to a specific path. One of the most common shapes created using this property is a triangle, which can be achieved using the polygon() function.
Syntax
selector {
clip-path: polygon(x1 y1, x2 y2, x3 y3);
}
Triangle with clip-path Property
To create a triangle using clip-path, you define three points that form the triangle vertices using the polygon() function. The coordinates are specified as percentages −
clip-path: polygon(50% 0, 100% 100%, 0% 100%);
This creates a triangle with the top point at the center (50% 0), bottom-right at (100% 100%), and bottom-left at (0% 100%).
Example
The following example demonstrates how to create a triangle using the clip-path property −
<!DOCTYPE html>
<html>
<head>
<style>
.triangle {
width: 200px;
height: 200px;
background-color: #3498db;
clip-path: polygon(50% 0, 100% 100%, 0% 100%);
margin: 50px auto;
}
</style>
</head>
<body>
<div class="triangle"></div>
</body>
</html>
A blue triangle pointing upward appears centered on the page.
Triangle Using Border Technique
Another method to create triangles is using the CSS border technique with transparent borders. This approach uses the child selector to target specific elements −
Example
The following example creates a triangle using the border technique −
<!DOCTYPE html>
<html>
<head>
<style>
.container {
display: flex;
justify-content: center;
margin: 50px 0;
}
.container > div {
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 100px solid #e74c3c;
}
</style>
</head>
<body>
<div class="container">
<div></div>
</div>
</body>
</html>
A red triangle pointing upward appears centered on the page, created using the border technique.
Different Triangle Orientations
You can create triangles pointing in different directions by adjusting the polygon coordinates −
<!DOCTYPE html>
<html>
<head>
<style>
.triangle-container {
display: flex;
gap: 30px;
justify-content: center;
margin: 50px 0;
}
.triangle {
width: 100px;
height: 100px;
background-color: #9b59b6;
}
.up {
clip-path: polygon(50% 0, 100% 100%, 0% 100%);
}
.down {
clip-path: polygon(0% 0%, 100% 0%, 50% 100%);
}
.left {
clip-path: polygon(0% 50%, 100% 0%, 100% 100%);
}
.right {
clip-path: polygon(0% 0%, 100% 50%, 0% 100%);
}
</style>
</head>
<body>
<div class="triangle-container">
<div class="triangle up"></div>
<div class="triangle down"></div>
<div class="triangle left"></div>
<div class="triangle right"></div>
</div>
</body>
</html>
Four purple triangles appear in a row, pointing up, down, left, and right respectively.
Conclusion
The clip-path property with polygon() function provides the most flexible way to create triangles in CSS. You can easily adjust the coordinates to create triangles of different sizes and orientations, making it perfect for modern web design.
