How to Draw a Pentagon using HTML and CSS? - GeeksforGeeks

07 Feb, 2022
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:
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.
varshagumber28
CSS-Misc
CSS
HTML
Web Technologies
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to set space between the flexbox ?
Design a web page using HTML and CSS
Form validation using jQuery
How to style a checkbox using CSS?
Search Bar using HTML, CSS and JavaScript
How to set the default value for an HTML <select> element ?
Hide or show elements in HTML using display property
How to set input type date in dd-mm-yyyy format using HTML ?
REST API (Introduction)
How to Insert Form Data into Database using PHP ?