SVG Polygon

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
pointsSpace-separated x,y pairs
Fill
fillInterior colour
Stroke
strokeOutline colour
Stroke width
stroke-widthOutline 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>💡 Best Practices
Do
- Keep points readable (one space between points)
- Use
stroke-linejoin="round"for nicer corners - Use a
viewBoxwhen 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.
4 people found this page helpful
