Chapter 8: SVG polygon

SVG polygon element!

This one is super useful because it lets you draw any closed shape made of straight lines — triangles, stars, houses, arrows, hexagons, irregular shapes — basically anything with straight sides (no curves).

I’m explaining it like we’re side by side at the computer, step by step, with examples you can copy-paste immediately.

What is <polygon> exactly?

<polygon> draws a closed shape by connecting a list of points with straight lines — and automatically connects the last point back to the first (that’s the big difference from <polyline>).

  • <polyline> → open chain of lines (doesn’t close)
  • <polygon> → closed shape (forms a filled area)

Think of it as giving SVG a list of corners (“vertices”), and it draws walls between them and closes the loop.

The most important attribute: points

Attribute Meaning Required? Format / Example Notes
points List of all x,y coordinate pairs Yes “x1,y1 x2,y2 x3,y3 …” Space separated, commas between x and y
fill Color inside the shape No “#FF5722”, “red”, etc. Default = black
stroke Color of the outline No “black”, “#333” Default = none
stroke-width Thickness of outline No 4, 10, etc.

Example of points syntax:

text

→ four corners → connects 1→2→3→4→back to 1

Here are some labeled point diagrams showing how coordinates connect:

File:Topojson shapes-en.svg - Wikimedia Commons
commons.wikimedia.org
Data Graphs: Smart Knowledge Graph Solutions
datagraphs.com

Example 1 – Basic triangle (the simplest polygon)

HTML

Result: Nice green triangle pointing upward. Points = top (150,40), bottom-left (40,240), bottom-right (260,240)

Example 2 – Regular pentagon (classic use case)

HTML

(Regular pentagons have equal sides & angles — coordinates often come from math formulas or tools like Inkscape / Figma.)

Here are various regular polygon shapes for reference:

Polygon - Definition, Properties, Types, Formulas
mathmonks.com
Polygons - Online Coding and Math Classes for Kids | eduSeed
eduseed.in

Example 3 – Star (very popular — 5-pointed star)

HTML

This is a classic star — notice the points go around in a zig-zag order (outer → inner → outer).

Here are some polygon-based star and shape examples:

Polygon shapes printable, polygon pdf, coloring pages, homeschool printable
etsy.com
Polygon shapes printable, polygon pdf, coloring pages, homeschool printable

Example 4 – Irregular shape (real-world use: country border, arrow, house)

HTML

Example 5 – Hover / interactive polygon (modern web fun)

HTML

Hover → it grows and glows!

Quick Cheat Sheet: <polygon> vs similar elements

Shape type Best element Why?
3–any straight sides, closed <polygon> Clean, fillable, closed automatically
Open chain of lines <polyline> Does not close
Any shape (curves too) <path> Most powerful (but longer code)
Rectangle / rounded <rect> Shorter & more semantic
Circle / oval <circle> / <ellipse> Much simpler for round shapes

Common beginner mistakes

  • Forgetting to close the shape in your mind → but <polygon> does it automatically (unlike <polyline>)
  • Wrong order of points → shape crosses itself (twisted / bow-tie look)
  • Points listed clockwise or counterclockwise — both work, but consistent direction helps with complex fills
  • No fill and no stroke → invisible shape!
  • Too many points → better to use <path> for very complex outlines

Practice mini-challenges

  1. Draw a blue hexagon (6 sides — regular or almost regular)
  2. Make a red arrowhead using one <polygon>
  3. Create a cute diamond / rotated square (hint: points like 150,20 230,100 150,180 70,100)

Paste your code here if you want — I’ll review it like we’re in a live coding session 😄

Any part confusing? Which example should we redraw bigger / slower? Just say — we’ll make it crystal clear together! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *