Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

How to Draw a Pentagon using HTML and CSS?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

A Pentagon is a 5 sided polygon or geometric shape with 5 internal angles measuring 108 degrees each. The pentagon can be drawn using simple HTML and CSS, the below sections will guide you on how to draw the desired shape.

HTML Code: In this section we will create a simple div element using the div class with class name “pentagon”.

html




<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width,
                 initial-scale=1.0">
  <title>Pentagon</title>
</head>
<body>
  <div class="pentagon"></div>  
</body>
</html>

CSS Code:  In this section first we will design the div element with some basic CSS properties and the draw the  pentagon, we will use CSS :after Selector which inserts something after the content of  an element i.e, in our case the div element.

css




<style>
*{
    margin: 0;
    padding: 0;
    background-color: white;
    
  }
  /* creating the pentagon shape */
  .pentagon{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    height: 0;
    width: 180px;
    border-top: 180px solid rgb(0, 66, 128);
    border-left: 90px solid transparent;
    border-right: 90px solid transparent;
  }
 
  .pentagon:after{
    position: absolute;
    content: '';
    border-bottom: 180px solid rgb(0, 66, 128);
    border-left: 180px solid transparent;
    border-right: 180px solid transparent;
    bottom: 180px;
    left: -90px;
  }
 </style>

Final Code: It is the combination of the above two code sections.

html




<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width,
                 initial-scale=1.0">
  <title>Pentagon</title>
</head>
<style>
  *{
      margin: 0;
      padding: 0;
      background-color: white;
      
    }
    /* creating the pentagon shape */
    .pentagon{
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      height: 0;
      width: 180px;
      border-top: 180px solid rgb(0, 66, 128);
      border-left: 90px solid transparent;
      border-right: 90px solid transparent;
    }
   
    .pentagon:after{
      position: absolute;
      content: '';
      border-bottom: 180px solid rgb(0, 66, 128);
      border-left: 180px solid transparent;
      border-right: 180px solid transparent;
      bottom: 180px;
      left: -90px;
    }
   </style>
<body>
  <div class="pentagon"></div>  
</body>
</html>

Output:


My Personal Notes arrow_drop_up
Last Updated : 07 Feb, 2022
Like Article
Save Article
Similar Reads
Related Tutorials