Graphics & Scripts Lesson 36 of 42
HTML SVG Graphics — Draw Shapes with Code
Draw circles, rectangles, lines, text and even a Philippine flag directly in HTML with SVG, graphics that stay sharp at any size.
So far, every picture on your pages came from an image file like a JPG or PNG. SVG (Scalable Vector Graphics) is different: you draw the picture using code, right inside your HTML. You describe shapes ("a red circle here, a blue rectangle there"), and the browser draws them.
Why SVG?
- Always sharp. A JPG gets blurry when you zoom in, because it's made of tiny dots (pixels). SVG is made of shapes and math, so it looks crisp at any size, on any screen.
- Very small files. A simple logo or icon in SVG is often smaller than 1 KB. That's good for students on prepaid data.
- Editable with code and CSS. You can change a color by changing one word.
SVG is perfect for logos, icons, charts, diagrams and simple illustrations. It is not good for photos. Use JPG or WebP for those.
Your first SVG
An SVG starts with the <svg> element, which sets the drawing area's width and height. The shapes go inside it.
<svg width="160" height="160">
<circle cx="80" cy="80" r="60" fill="gold" stroke="orange" stroke-width="6" />
</svg>
The circle's attributes:
cx,cy— the x and y position of the centerr— the radius (distance from center to edge)fill— the inside colorstrokeandstroke-width— the outline color and thickness
The coordinate system
SVG positions things using x and y numbers, like the Cartesian plane in math class, with one big difference: y goes down. The point (0, 0) is the top-left corner. Bigger x moves right, bigger y moves down.
<svg width="300" height="200" style="border: 1px solid #ccc;">
<circle cx="0" cy="0" r="6" fill="red" />
<text x="10" y="18" font-size="14">(0, 0) top-left</text>
<circle cx="150" cy="100" r="6" fill="blue" />
<text x="160" y="105" font-size="14">(150, 100)</text>
<circle cx="280" cy="180" r="6" fill="green" />
<text x="160" y="185" font-size="14">(280, 180)</text>
</svg>
Basic shapes
<svg width="420" height="230">
<rect x="10" y="10" width="120" height="70" fill="#0038A8" rx="10" />
<line x1="150" y1="20" x2="280" y2="80" stroke="#CE1126" stroke-width="5" />
<ellipse cx="360" cy="45" rx="50" ry="30" fill="#7fd1b9" />
<polygon points="70,120 120,210 20,210" fill="#FCD116" stroke="#333" />
<text x="160" y="170" font-size="26" font-family="Arial" fill="#333">Mabuhay!</text>
</svg>
<rect>— a rectangle.x,yset the top-left corner. Addrxfor rounded corners.<line>— a straight line from (x1,y1) to (x2,y2). Lines need astroketo be visible.<ellipse>— an oval with two radii,rx(sideways) andry(up and down).<polygon>— any shape with straight sides. List the corners inpointsas "x,y" pairs.<text>— words. Theyvalue is the bottom of the letters (the baseline).
Project: the Philippine flag
Let's combine shapes. The flag has a blue stripe, a red stripe, a white triangle, a sun and three stars. Here's a simplified version:
<svg width="360" height="180" viewBox="0 0 360 180">
<rect x="0" y="0" width="360" height="90" fill="#0038A8" />
<rect x="0" y="90" width="360" height="90" fill="#CE1126" />
<polygon points="0,0 156,90 0,180" fill="#FFFFFF" />
<!-- the sun -->
<circle cx="52" cy="90" r="18" fill="#FCD116" />
<!-- the three stars (drawn as small circles to keep it simple) -->
<circle cx="16" cy="20" r="6" fill="#FCD116" />
<circle cx="16" cy="160" r="6" fill="#FCD116" />
<circle cx="128" cy="90" r="6" fill="#FCD116" />
</svg>
Each shape is drawn in order, so the white triangle covers part of the two stripes, and the sun sits on top of the triangle. In SVG, later shapes are drawn on top of earlier ones.
viewBox: making SVG resize
The flag example uses viewBox="0 0 360 180". This says "my drawing uses coordinates from (0, 0) to (360, 180)." Once a viewBox is set, you can change width and height freely, and the whole drawing scales to fit, staying sharp. Try changing the flag's width to 720 and height to 360, or to 100%.
Paths: any shape you want
The <path> element can draw anything, including curves. Its d attribute is a list of drawing commands, like instructions to a pen: M (move to), L (line to), Q or C (curve to), and Z (close the shape).
<svg width="160" height="160" viewBox="0 0 32 32">
<path d="M16 28 L4 16 C0 11 3 4 9 4 C12 4 14 6 16 9 C18 6 20 4 23 4 C29 4 32 11 28 16 Z" fill="#e0245e" />
</svg>
Nobody writes complicated paths by hand. Designers draw them in apps like Inkscape (free), Figma or Illustrator and export SVG code. Most of the icons you see on websites are SVG paths.
SVG as an image file
An SVG can also be saved as its own .svg file and used like any image:
<img src="logo.svg" alt="School logo" width="120">
The favicon of this website is an SVG file, too. Use inline SVG (written in the HTML) when you want to change it with CSS or JavaScript, and an .svg file when you just want to show it.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04