SVG Polygon

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 1 Code Example
Basic Shape

What You’ll Learn

SVG polygons let you create custom shapes by connecting multiple points. They are perfect for diamonds, stars, badges, and geometric illustrations.

You’ll learn how the points attribute works, how to style polygons with fill and stroke, and how polygons differ from polylines.

⚡ Quick Reference — SVG Polygon

Element <polygon>

A closed shape built from points

Points points

Space-separated x,y pairs

Fill fill

Interior colour

Stroke stroke

Outline colour

Stroke width stroke-width

Outline thickness

Basic Syntax
<svg>
  <polygon points="x1,y1 x2,y2 x3,y3" />
</svg>

👀 Live Preview — Common Polygons

Polygons are great for geometric icons and decorative shapes.

Diamond
Triangle
Chevron
1

Complete HTML Example

This example creates a diamond (4-point polygon) and styles it with fill and stroke:

index.html
<!DOCTYPE html>
<html>
<body>

<svg width="200" height="200">
  <polygon points="100,20 20,100 100,180 180,100" fill="yellow" stroke="black" stroke-width="2" />
</svg>

</body>
</html>
Try It Yourself

💡 Best Practices

Do

  • Keep points readable (one space between points)
  • Use stroke-linejoin="round" for nicer corners
  • Use a viewBox when polygons must scale responsively
  • Use CSS classes for consistent styling across multiple polygons
  • Prefer simple polygons for icons; use <path> for complex curves

Don’t

  • Forget that polygons automatically close the shape
  • Use fill="none" without a visible stroke (hard to see)
  • Mix inconsistent separators in points (stay consistent)
  • Put points outside the SVG canvas/viewBox unintentionally
  • Overuse many points when a simpler shape works

❓ Frequently Asked Questions

Use <polygon> and provide points as x,y pairs. Add fill and stroke to style it.
points is a list of coordinate pairs like "10,10 50,10 50,50". Pairs are separated by spaces.
A polygon is closed automatically (last point connects to the first). A polyline stays open unless you repeat the first point at the end.
Check that your points are inside the viewBox/canvas and that you have a visible fill or stroke. Also verify the points string is valid.
Yes. Use CSS to set fill, stroke, and stroke-width, and add hover or focus styles if needed.

Connect Points with Polylines

Next, learn <polyline> to draw open shapes and multi-segment strokes.

SVG Polyline →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

4 people found this page helpful