Chapter 4: SVG Rectangle

SVG <rect> element — the simplest and most frequently used shape in SVG after the circle.

Imagine we’re sitting together with a whiteboard. I’ll explain everything slowly like a patient teacher, step by step, with real examples you can copy-paste right now.

What is <rect> really?

<rect> = rectangle (surprise 😄)

It draws a rectangle (or square if width = height). It is one of the basic shape elements in SVG (together with <circle>, <ellipse>, <line>, <polyline>, <polygon>).

The rectangle always starts from a top-left corner point → then goes right (width) and down (height).

Important: In SVG coordinates:

  • 0,0 is top-left corner of the <svg> canvas (very different from math graphs where y grows upward)
  • x grows to the right
  • y grows downward

The 6 most important attributes of <rect>

Attribute Meaning Required? Default Can be animated? Units / values
x Left edge position No 0 Yes number, %, etc.
y Top edge position No 0 Yes number, %, etc.
width How wide (horizontal size) Yes Yes number > 0, %
height How tall (vertical size) Yes Yes number > 0, %
rx Horizontal corner radius (rounding) No 0 (sharp) Yes number, %
ry Vertical corner radius (rounding) No = rx or 0 Yes number, %

You also almost always use these styling attributes (same as other shapes):

  • fill — inside color (default = black)
  • stroke — border color (default = none)
  • stroke-width — border thickness (default = 1)
  • opacity, fill-opacity, stroke-opacity, etc.

Example 1 – Basic sharp rectangle (most common starting point)

HTML

What you see:

  • Green filled rectangle
  • Dark green 6px border
  • Starts 80 units from left edge, 60 units from top

Here is a visual example of how x, y, width, height work (coordinate system):

SVG Rectangle | Create a rectangle using SVG | Rx and Ry (Round Corner) | HTML5 Basics
youtube.com

Example 2 – Square (width = height)

HTML

Example 3 – Rounded rectangle (very popular for buttons/cards/UI)

HTML

Key rule about rounding:

  • If you set only rx → ry becomes same as rx
  • If you set only ry → rx becomes same as ry
  • If rx or ry ≥ half the width/height → you get very round / capsule shape

Here is how rx and ry actually bend the corners:

A Practical Guide To SVG And Design Tools — Smashing Magazine
smashingmagazine.com
SVG Rectangle | Create a rectangle using SVG | Rx and Ry (Round Corner) | HTML5 Basics
youtube.com

Example 4 – CSS hover effect (very common in modern websites)

HTML

Try hovering — nice modern button feel, right?

Quick Cheat Sheet – When to use <rect>

Goal Use this…
Simple box / background <rect>
Card with rounded corners <rect rx=”12″ ry=”12″>
Pill / tag / button shape <rect rx=”9999″> or big rx
Border only (no fill) fill=”none”
Dashed border stroke-dasharray=”8 4″
Very complex shape Use <path> instead

Common beginner mistakes

  1. Forget width or height → nothing appears
  2. Put x=0 y=0 width=100% height=100% but forget <svg viewBox> → strange scaling
  3. Think y grows upward (like math class) → it grows downward
  4. rx=20 but width=30 → corners look strange (radius can’t be bigger than half size)

Ready for homework? 😄 Try to make:

  • A red rounded square button that says “Click Me”
  • A progress bar using two <rect> (one gray background + one green fill)

Paste your code here if you want feedback — I’ll review it like a real teacher!

Any part confusing? Just ask — we go again until it clicks! 🚀

You may also like...

Leave a Reply

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