Chapter 6: SVG Ellipse
What is an Ellipse really?
An ellipse is basically a squashed or stretched circle.
- A circle = special case of ellipse where horizontal radius = vertical radius
- An ellipse = circle that has been stretched or compressed in one direction (or both)
So <ellipse> is more general than <circle>. You can make circles with <ellipse>, but you cannot make true ellipses with <circle>.
The 4 core attributes of <ellipse>
| Attribute | Full name | Meaning | Required? | Default | Notes |
|---|---|---|---|---|---|
| cx | center x | Horizontal position of the center | No | 0 | Same as circle |
| cy | center y | Vertical position of the center | No | 0 | Same as circle |
| rx | radius x (horizontal) | Distance from center → edge horizontally | Yes | — | Half the total width |
| ry | radius y (vertical) | Distance from center → edge vertically | Yes | — | Half the total height |
Key points:
- rx controls horizontal stretch/squash
- ry controls vertical stretch/squash
- If rx = ry → you get a perfect circle (same as <circle r=”…”>)
- Center is still (cx, cy) — very convenient!
Here is how the ellipse is built from center + two radii:
(If you see a diagram with arrows showing rx horizontal and ry vertical from center — that’s exactly it.)
Example 1 – Basic ellipse (horizontal oval)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<svg width="400" height="220" style="border:1px solid #eee; background:#f9f9f9;"> <ellipse cx="200" cy="110" rx="160" ry="70" fill="#FF9800" /> </svg> |
Result: Wide, flattened orange oval — like a horizontal egg or a rugby ball lying flat.
Example 2 – Vertical ellipse (tall oval)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<svg width="220" height="400"> <ellipse cx="110" cy="200" rx="80" ry="160" fill="#2196F3" /> </svg> |
Result: Tall, narrow blue oval — like a standing egg or portrait orientation balloon.
Example 3 – Circle made with ellipse (just to prove the point)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<svg width="240" height="240"> <!-- This is exactly the same as <circle cx="120" cy="120" r="100"> --> <ellipse cx="120" cy="120" rx="100" ry="100" fill="#4CAF50" stroke="#2E7D32" stroke-width="10" /> </svg> |
So yes — <ellipse rx=”r” ry=”r”> = <circle r=”r”>
Example 4 – Ellipse with stroke, different fill & stroke opacity
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<svg width="360" height="240"> <ellipse cx="180" cy="120" rx="140" ry="90" fill="#E91E63" fill-opacity="0.7" stroke="#C2185B" stroke-width="16" stroke-opacity="0.9"/> <text x="180" y="135" font-size="42" text-anchor="middle" fill="white"> ELLIPSE </text> </svg> |
Example 5 – Many ellipses — bubbles / eggs / abstract art feel
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
<svg width="500" height="300"> <ellipse cx="120" cy="100" rx="90" ry="50" fill="#FFEB3B" opacity="0.8"/> <ellipse cx="250" cy="140" rx="110" ry="70" fill="#9C27B0" opacity="0.7"/> <ellipse cx="380" cy="180" rx="70" ry="120" fill="#00BCD4" opacity="0.75"/> <ellipse cx="200" cy="220" rx="140" ry="40" fill="#F44336" opacity="0.6"/> </svg> |
Example 6 – Rounded rectangle using <ellipse> (creative trick)
You can fake soft rounded corners or capsule shapes by overlapping ellipses + rectangles — but most people just use <rect rx ry> for that nowadays.
Still, fun to see:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<svg width="340" height="180"> <rect x="40" y="40" width="260" height="100" fill="#673AB7" rx="50"/> <!-- The same shape using ellipses + rectangle --> <rect x="90" y="40" width="160" height="100" fill="#512DA8"/> <ellipse cx="90" cy="90" rx="50" ry="50" fill="#512DA8"/> <ellipse cx="250" cy="90" rx="50" ry="50" fill="#512DA8"/> </svg> |
(Modern advice: prefer <rect rx=”50″ ry=”50″> — cleaner code.)
Quick comparison table: <circle> vs <ellipse>
| Feature | <circle> | <ellipse> |
|---|---|---|
| Shape | Perfect circle only | Circle or oval/egg/elliptical |
| Defining attributes | cx, cy, r | cx, cy, rx, ry |
| Can make horizontal oval? | No | Yes (rx > ry) |
| Can make vertical oval? | No | Yes (ry > rx) |
| Used for | Dots, buttons, avatars, rings | Stretched buttons, eyes, leaves, etc. |
| Code length (circle case) | Shorter | Slightly longer |
When should you choose <ellipse> over <circle>?
- You need an oval / egg / stretched shape → <ellipse>
- You need a perfect round shape → <circle> (shorter, clearer)
- You’re making eyes in a cartoon character → often <ellipse> for realistic almond shape
- You’re making horizontal/vertical pills → <ellipse> or <rect rx ry>
Common beginner mistakes with <ellipse>
- Forgetting rxorry → shape doesn’t appear
- Setting rx=0 or ry=0 → degenerates to a line (or nothing)
- Thinking rx is diameter (no — it’s radius — half the width)
- Placing cx, cy wrong → ellipse gets cut off at edge
Okay — mini practice for you:
- Make a yellow egg shape (slightly taller than wide)
- Make three nested ellipses with decreasing size and different colors (like a target)
- Create a simple smiling face using one big ellipse (face) + two small tilted ellipses (eyes)
Paste your code anytime — I’ll give feedback like we’re pair-programming together 😄
Any part still confusing? Just say the word — we’ll zoom in until it’s super clear! 🚀
