Cyber Code Academy
Tutorials Academy Courses
Home / html-tutorials-md / HTML SVG Graphics

HTML SVG Graphics

> Learn scalable vector graphics in HTML using SVG elements, shapes, paths, viewBox, and accessible labels.

**Cyber Code Academy · HTML Tutorial · Lesson 38 · Intermediate**

## 1. What is SVG?
SVG stands for Scalable Vector Graphics. It describes graphics using XML-based markup and vector geometry. Unlike a bitmap image, SVG shapes can scale without becoming pixelated in the same way.

SVG is suitable for icons, diagrams, logos, charts, and illustrations.

## 2. Basic SVG element

“`html


“`

– svg is the root SVG element.
– width and height define its display dimensions.
– viewBox defines the internal coordinate system.
– rect creates a rectangle.
– circle creates a circle.
– fill sets the filled colour.

## 3. Draw a line

“`html


“`

The line element uses start and end coordinates. stroke controls the outline colour; stroke-width controls its thickness.

## 4. Draw a path

“`html

“`

The path element describes geometry using commands. M moves to a coordinate, L draws a line, and Z closes the path.

## 5. Use viewBox for scaling

viewBox defines the coordinate system inside the SVG. It allows the same drawing to be displayed at different sizes while preserving its proportions.

“`html


“`

CSS can also control how an SVG scales and where it appears.

## 6. Accessible SVG
For meaningful graphics, provide an accessible name and description. A role and aria-label can be used for a simple graphic:

“`html

“`

Decorative SVG can be hidden from assistive technology when appropriate. If the graphic contains complex information, provide a longer text alternative or data table.

## 7. Inline SVG versus image file
Inline SVG is placed directly in HTML and can be styled with CSS. An external SVG can be loaded using img:

“`html
Cyber Code Academy icon
“`

Use inline SVG when you need direct control over individual vector elements. Use an external image when you simply need to display the graphic.

## 8. Common mistakes
– Forgetting viewBox and creating unexpected scaling.
– Using SVG for text that should be normal HTML text.
– Missing accessible name for informative graphics.
– Making a complex chart without providing the data.
– Using untrusted SVG files without reviewing their content.

## 9. Practice exercises
1. Create an SVG rectangle and circle.
2. Draw a line with a custom stroke.
3. Draw a simple shape using path.
4. Change viewBox and compare scaling.
5. Add accessible labels to an informative graphic.
6. Save an SVG file and display it with img.

## 10. Knowledge check
1. What does SVG stand for?
2. How is vector graphics different from raster graphics?
3. What does viewBox do?
4. What is the purpose of path?
5. How can an SVG be made accessible?

**Next lesson:** Responsive Images and Picture.

Want to learn this professionally?

Explore practical IT training at Cyber Code Academy.

View Academy Courses