THE VECTOR FIELD GUIDE

SVG circle and rect: the basic shape tags

How the SVG circle and rect tags work: cx, cy, r, x, y, width, height, rx, fill and stroke, with copy-ready examples you can edit live.

By Shahab Uddin · Updated

The short answer

In SVG, <circle> draws a circle from its center (cx, cy) and radius (r), and <rect> draws a rectangle from its top-left corner (x, y), width and height, with optional rounded corners (rx, ry). Both accept fill and stroke attributes and live inside an <svg> element with a viewBox.

A minimal example

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 120" width="200" height="120">
  <rect x="10" y="10" width="110" height="100" rx="12" fill="#243d2e"/>
  <circle cx="150" cy="60" r="40" fill="#c74b31" stroke="#202721" stroke-width="4"/>
</svg>

Paste this into the free SVG editor to see it and experiment with the values.

Circle attributes

AttributeMeaning
cx, cyCenter point in user units
rRadius
fillInterior color, or none
stroke, stroke-widthOutline color and thickness

Rect attributes

AttributeMeaning
x, yTop-left corner
width, heightSize; zero hides the shape
rx, ryCorner radii for rounded rectangles
fill, strokePaint, as for every shape

The viewBox controls scaling

viewBox="0 0 200 120" defines the internal coordinate system. The width and height attributes, or CSS, set how large it appears. Remove width and height to let the SVG fill its container while keeping proportions.

Other basic shapes

  • <ellipse cx cy rx ry> for ovals.
  • <line x1 y1 x2 y2> for a single line; it needs a stroke to be visible.
  • <polygon points> for closed shapes and <polyline points> for open ones.
  • <path d> for anything, including curves; traced images use paths.

Sources

Written by Shahab Uddin. Published ; updated . Corrections are welcome through the contact page.

A few useful answers

Before you hit download

How do I make a circle outline only?

Set fill="none" and give it a stroke, for example stroke="black" stroke-width="2".

Why is my rect invisible?

Check that width and height are above zero and that fill is not none without a stroke.

Can I center a circle?

Set cx and cy to half the viewBox width and height.

Next, make it useful.