How to Draw a Pentagon using HTML and CSS?

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”.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<style>
*{
    margin: 0;
    padding: 0;
    background-color: white;
     
  }
  /* creating tthe 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>

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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 tthe 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>

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.