SVG Shapes – Basics & SVG Code, Examples

Simple vector shapes are the building blocks of logos, diagrams, icons, charts, and many other SVG graphics. In an SVG file, each basic shape is represented by an element whose attributes define its position, size, and visual style. You can customize fill color, opacity, corner rounding, stroke width, and more.

In this article, you will learn to:

  • Create SVG rectangles, circles, ellipses, lines, polylines, and polygons.
  • Read the coordinate attributes used by each basic shape.
  • Apply fill, stroke, opacity, and rounded corners.
  • Combine several SVG shape elements in one SVG image.

SVG <rect> – Rectangle Element

The <rect> element creates rectangles and rounded-corner rectangles. A rectangle is positioned by its top-left corner and then sized with width and height.

Rectangle Attributes

AttributeDescriptionDefault
xX‑coordinate of the top‑left corner0
yY‑coordinate of the top‑left corner0
widthRectangle width
heightRectangle height
rxHorizontal radius for rounded corners0
ryVertical radius for rounded corners0

You can style the rectangle with presentation attributes or with the style attribute. The example below uses fill, stroke, opacity, and corner radius values to show how geometry and painting work together.

1<svg width="500" height="550" xmlns="http://www.w3.org/2000/svg">
2    <rect x="60" y="100" width="70" height="40" rx="10" ry="10" style="fill:#778899; stroke:#FF4500; stroke-width:5; fill-opacity:0.7; stroke-opacity:0.6" />
3</svg>

The first example creates a rectangle whose top-left corner is at (60, 100). The rectangle is 70 units wide and 40 units high, with rounded corners and an orange stroke. The second example removes rounded corners and uses a larger square-like rectangle, so the difference between rx/ry and ordinary sharp corners is easy to see.

1<svg width="500" height="550" xmlns="http://www.w3.org/2000/svg">
2    <rect x="120" y="140" width="90" height="90" style="fill:grey; stroke-width:3; stroke:rgb(0,0,0)" />
3</svg>

Two grey SVG rectangles illustrating basic and rounded‑corner rectangle shapes

Style Properties Used

Colors can be specified as:

  1. Named colors, e.g., fill:blue (140+ CSS color names).
  2. RGB function, e.g., fill:rgb(0,0,255).
  3. Hex notation, e.g., fill:#0000ff.

SVG <circle> – Circle Element

The <circle> element draws a perfect circle. Its position is defined by the center coordinates cx and cy, and its size is defined by the radius r.

Circle Attributes

AttributeDescriptionDefault
cxX‑coordinate of the circle center0
cyY‑coordinate of the circle center0
rRadius of the circle
1<svg width="300" height="550" xmlns="http://www.w3.org/2000/svg">
2    <circle cx="250" cy="100" r="60" style="fill:red; stroke-width:3; stroke:rgb(0,0,0); fill-opacity:0.7" />
3</svg>

The code below sets cx and cy for the center point and r for the radius. Because the circle is painted with both fill and stroke, the output also demonstrates how shape geometry and painting properties combine.

Two red SVG circles demonstrating fill, stroke, and opacity

The circle extends beyond the 300 px viewport width; a width of at least 310 px (cx + r) is required for full visibility.

SVG <ellipse> – Ellipse Element

The <ellipse> element creates an oval shape. It also uses a center point, but it has two radii: rx for horizontal size and ry for vertical size.

Ellipse Attributes

AttributeDescriptionDefault
cxX‑coordinate of the ellipse center0
cyY‑coordinate of the ellipse center0
rxHorizontal radius (half the width)
ryVertical radius (half the height)
1<svg width="500" height="550" xmlns="http://www.w3.org/2000/svg">
2    <ellipse cx="140" cy="310" rx="90" ry="20" style="fill:OrangeRed" />
3    <ellipse cx="120" cy="280" rx="110" ry="20" style="fill:grey; fill-opacity:0.5" />
4</svg>

The example draws two ellipses with different radii and opacity. The second ellipse (grey, 50% opacity) is rendered on top of the first because SVG paints elements in source order: later elements appear above earlier ones.

Two SVG ellipses

SVG <line> – Line Element

The <line> element draws a straight segment between two coordinate points. It has no fill area, so visible lines normally need stroke and stroke-width.

Line Attributes

AttributeDescriptionDefault
x1X‑coordinate of the start point
y1Y‑coordinate of the start point
x2X‑coordinate of the end point
y2Y‑coordinate of the end point
style (stroke, stroke-width)Visual styling of the line
1<svg width="500" height="550" xmlns="http://www.w3.org/2000/svg">
2    <line x1="30" y1="30" x2="350" y2="290" style="stroke:rgb(255,0,0); stroke-width:3" />
3    <line x1="30" y1="50" x2="300" y2="350" style="stroke:grey; stroke-width:5" />
4    <line x1="20" y1="80" x2="100" y2="200" style="stroke:orangered; stroke-width:8" />
5</svg>

Three SVG lines with varying colors and thicknesses

Each line in the example has a different start point, end point, color, and thickness. The stroke property defines line color, while stroke-width sets its thickness.

SVG <polyline> – Polyline Element

The <polyline> element draws a series of connected straight segments. It is commonly used for broken lines, chart lines, simple outlines, and open angular shapes.

Polyline Attributes

AttributeDescriptionDefault
pointsList of x,y coordinate pairs defining the polyline vertices
style (fill, stroke, stroke-width)Visual styling of the shape
1<svg width="500" height="550" xmlns="http://www.w3.org/2000/svg">
2    <polyline points="280,290 300,220 320,290" style="fill:grey; stroke:grey; stroke-width:2; fill-opacity:0.5" />
3    <polyline points="220,200 240,180 260,200 280,180 300,200 320,180 340,200" style="fill:none; stroke:red; stroke-width:6" />
4</svg>

Red polyline forming a zig‑zag and a grey filled triangle

The first polyline uses three points and visually forms a triangle-like shape. The second polyline is an open zig-zag line with fill="none", which is usually the clearest choice when a polyline should behave like a line rather than a filled shape.

For more details on SVG styling, see Fills and Strokes in SVG.

SVG <polygon> – Polygon Element

The <polygon> element defines a closed shape formed by a series of connected points. SVG automatically connects the last point back to the first point.

Polygon Attributes

AttributeDescriptionDefault
pointsList of x,y coordinate pairs for each vertex (must contain at least three points)
style (fill, stroke, stroke-width)Visual styling of the polygon
1<svg width="500" height="550" xmlns="http://www.w3.org/2000/svg">
2    <polygon points="160,10 350,140 210,350 50,199" style="fill:orange;stroke:purple;stroke-width:1" />
3</svg>

The polygon example uses four coordinate pairs in the points attribute. Since <polygon> is always closed, the purple stroke returns from the last point to the first point automatically.

Orange polygon with a purple border illustrating a four-point shape

How to Create SVG Shapes

  1. Create an outer <svg> element with width, height, and xmlns.
  2. Choose a basic shape element such as <rect>, <circle>, <ellipse>, <line>, <polyline>, or <polygon>.
  3. Set the required coordinate and size attributes for that element.
  4. Add visual attributes such as fill, stroke, stroke-width, or opacity.
  5. Preview the SVG and adjust coordinates, size, or viewport if the shape is clipped.

SVG Basic Shapes – Combined Example

A minimal SVG document can combine all basic shapes into a single illustration. This is useful when you need to build diagrams, badges, icons, or generated graphics from simple primitives.

 1<svg width="500" height="550" xmlns="http://www.w3.org/2000/svg">
 2    <line x1="30" y1="30" x2="350" y2="290" style="stroke:rgb(255,0,0); stroke-width:3" />
 3    <line x1="30" y1="50" x2="300" y2="350" style="stroke:grey; stroke-width:5" />
 4    <rect x="60" y="100" width="70" height="40" rx="10" ry="10" style="fill:#778899; stroke:#FF4500; stroke-width:5; fill-opacity:0.7; stroke-opacity:0.6" />
 5    <polygon points="160,10 350,140 210,350 50,199" style="fill:orange; stroke:purple; stroke-width:1; fill-opacity:1" />
 6    <rect x="120" y="150" width="90" height="90" style="fill:grey; stroke-width:3; stroke:rgb(0,0,0)" />
 7    <circle cx="250" cy="100" r="60" style="fill:red; stroke:black; stroke-width:3; fill-opacity:0.7" />
 8    <ellipse cx="140" cy="310" rx="90" ry="20" style="fill:OrangeRed" />
 9    <ellipse cx="120" cy="280" rx="110" ry="20" style="fill:grey; fill-opacity:0.5" />
10    <polyline points="220,200 240,180 260,200 280,180 300,200 320,180 340,200" style="fill:none; stroke:red; stroke-width:6" />
11    <line x1="20" y1="80" x2="100" y2="200" style="stroke:orangered; stroke-width:8" />
12    <polyline points="280,290 300,220 320,290" style="fill:grey; stroke:grey; stroke-width:2; fill-opacity:0.5" />
13</svg>

Composite SVG illustration showing all basic shape elements

The illustration “SVG Basic Shapes” contains all the figures described above: lines, rectangles, a circle, ellipses, polylines, and a polygon. Nearby is the famous painting “Pink Accent” by Wassily Kandinsky, which shows how simple geometric forms can become a more expressive composition.

Common SVG Shape Mistakes and Fixes

ProblemLikely CauseHow to Fix
Shape is not visibleSVG viewport is smaller than the shapeIncrease width / height or adjust the viewBox
Shape is clippedCoordinates exceed the viewport boundsRecalculate coordinates or expand the SVG canvas
Fill color does not appearfill="none" or fill-opacity="0" is setSet a valid fill value and opacity
Stroke is invisiblestroke-width="0" or missing strokeDefine both stroke and a positive stroke-width
Rounded corners do not workrx / ry values are missing or zeroSet non-zero rx and ry attributes
Elements overlap unexpectedlySVG renders elements in source orderReorder elements in the markup or use grouping

Quick Color Recipes for SVG Shapes

TaskExample
Solid fill colorfill="blue"
Semi-transparent fillfill="red" fill-opacity="0.5"
Transparent fill with visible outlinefill="none" stroke="black"
Semi-transparent strokestroke="black" stroke-opacity="0.4"
Thick colored outlinestroke="orange" stroke-width="6"
Dashed outlinestroke-dasharray="5,3"

For advanced SVG styling techniques, see Fills and Strokes in SVG.

FAQ – SVG Shapes

How do I choose the right SVG shape element?

Use <rect> for rectangles, <circle> for equal-radius round shapes, <ellipse> for ovals, <line> for one straight segment, <polyline> for an open chain of segments, and <polygon> for a closed shape made from points.

What is the difference between polyline and polygon?

<polyline> draws connected points but stays open unless you manually repeat the first point. <polygon> closes the shape automatically by connecting the last point back to the first point.

Can SVG shapes use fill and stroke together?

Yes. Most SVG shapes can use both fill and stroke. The fill paints the inside of the shape, while stroke and stroke-width draw the outline.

Can basic SVG shapes replace path data?

Yes, when the graphic is a simple rectangle, circle, ellipse, straight line, polyline, or polygon. Use SVG Path Data when you need curves, arcs, custom outlines, or a single complex contour.

Specification and Related Resources