Open In App

How to Create Triangle with CSS?

Last Updated : 15 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Creating a triangle in CSS involves a clever use of borders to form various shapes without actual images. By setting the width and height of an HTML element to zero and applying specific borders, you can create directional triangles.

To create a triangle:

  • Add a single <div> element in your HTML for each triangle.
  • Set the width and height of the <div> to zero.
  • Use the border property to create the triangle by defining the thickness and color of one side of the border, while setting the other sides to transparent.

1. Upward-Facing Triangle

To create an upward-facing triangle, we use the border-bottom property with a specified color and set the border-left and border-right properties to transparent.

HTML
<h3 >How do CSS triangles work?</h3>

<div style="width: 0; height: 0; border-left: 50px solid transparent; 
            border-right: 50px solid transparent; 
            border-bottom: 50px solid green; margin: 2rem;">
</div>

Output

upward-triangle

upward-triangle


2. Downward-Facing Triangle

To create a downward-facing triangle, we use the border-top property, with the left and right borders set to transparent.

HTML
<h3>How do CSS triangles work?</h3>

<div style="width: 0; height: 0; border-left: 50px solid transparent; 
            border-right: 50px solid transparent; 
            border-top: 50px solid green; margin: 2rem;">
</div>

Output

Triangle-in-CSS-Example-Output

Triangle in CSS Example Output



Next Article

Similar Reads