Skip to content
Related Articles

Related Articles

How to Draw a Pentagon using HTML and CSS?
  • Last Updated : 14 Aug, 2020

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




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




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

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




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

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :