Open In App

How to Draw a Pentagon using HTML and CSS?

Last Updated : 07 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads