HTML SVG
Learn how to create scalable vector graphics in HTML using the <svg> element. Understand shapes, coordinates, attributes, and why SVG is perfect for logos, icons, charts, and diagrams.
Introduction
In the previous lesson, you learned about HTML Iframes and how external webpages and services can be displayed inside a webpage.
Modern websites also depend heavily on visual graphics such as company logos, interface icons, charts, diagrams, illustrations, maps, and infographics.
Traditional image formats such as JPG and PNG are made from pixels. When these images are enlarged beyond their original dimensions, individual pixels become visible and the image can appear blurry.
HTML supports SVG, or Scalable Vector Graphics, to solve this problem for graphics that can be described using shapes, lines, curves, coordinates, and mathematical instructions.
SVG graphics can remain sharp when displayed as a small icon, a large website logo, or even a graphic on a very large screen.
Raster Image
- Made from a fixed grid of pixels.
- Can become blurry when enlarged.
- Common formats include JPG and PNG.
- Best suited for photographs and detailed pixel-based images.
SVG Graphic
- Created using mathematical instructions.
- Remains sharp when resized.
- Can be written directly in HTML.
- Best suited for logos, icons, diagrams, and charts.
Instead of storing the color of every individual pixel, SVG describes what shapes should be drawn and where they should appear.
What is SVG?
SVG stands for Scalable Vector Graphics.
SVG is an XML-based graphics format used to create two-dimensional vector graphics.
Unlike raster images, SVG graphics are built using elements that describe shapes such as circles, rectangles, lines, ellipses, polygons, paths, and text.
The browser reads these elements and their attributes, calculates their geometry, and draws the final graphic on the screen.
<svg width="200" height="200">
<circle
cx="100"
cy="100"
r="80"
fill="blue"
/>
</svg>Vector-Based
Graphics are described using mathematical shapes and coordinates.
Scalable
Graphics can be enlarged without becoming pixelated.
Code-Based
SVG markup can be written directly inside HTML.
Styleable
Many SVG properties can be controlled using CSS.
Interactive
SVG elements can respond to JavaScript and user interaction.
Animatable
SVG properties and shapes can be animated.
SVG is a way to create graphics using code, mathematical shapes, coordinates, and drawing instructions instead of a fixed grid of pixels.
Why Do We Need SVG?
Websites must display graphics on many different screen sizes and pixel densities.
A logo may appear as a tiny icon in one location and as a large banner graphic in another.
Using a fixed-size raster image for every situation can create quality, responsiveness, and file-management problems.
Without SVG
- Logos can become blurry when enlarged.
- Multiple image sizes may be required.
- Icons may lose sharpness on high-density screens.
- Charts can be difficult to scale responsively.
- Simple graphics may require separate image files.
With SVG
- Graphics remain sharp at different sizes.
- One graphic can adapt to many dimensions.
- Icons remain crisp on high-density screens.
- Charts and diagrams can scale responsively.
- Simple graphics can be written directly in HTML.
Sharp Logos
Company logos remain crisp from small navigation bars to large displays.
Crisp Icons
Interface icons remain sharp on different screen densities.
Responsive Charts
Data visualizations can adapt to changing container sizes.
Interactive Graphics
Individual parts of maps and diagrams can respond to interaction.
Dynamic Styling
Colors and other properties can be changed through CSS and JavaScript.
Responsive Design
SVG graphics can scale with modern responsive layouts.
Real-World Analogy
Imagine comparing a photograph with a set of drawing instructions.
A photograph contains a fixed collection of tiny colored pixels. If the photograph is enlarged enough, those pixels become visible.
Now imagine instructions that say: draw a circle at this position with this radius and this color.
The same instructions can be used to draw a small circle or a very large circle while preserving a perfectly smooth shape.
Photograph
- Stores individual pixels.
- Has a fixed pixel resolution.
- Enlargement can reveal pixelation.
- Excellent for detailed real-world images.
SVG Drawing Instructions
- Stores shapes and coordinates.
- Can be recalculated at different sizes.
- Scaling preserves sharp edges.
- Excellent for structured graphics.
Raster Image SVG Graphic
──────────── ───────────
Made of Pixels Made of Shapes
Fixed Resolution Mathematical Geometry
Zoom In → Pixelation Zoom In → Sharp
Best for Photos Best for Logos & IconsA raster image stores the final pixels. SVG stores instructions that tell the browser how to construct the graphic.
SVG Element & Syntax
The <svg> element creates the drawing area for an SVG graphic.
Shape elements placed inside the <svg> element describe what the browser should draw.
<svg width="200" height="200">
<!-- SVG shapes go here -->
</svg>The width and height attributes define the displayed dimensions of the SVG drawing area.
<svg width="200" height="200">
<circle
cx="100"
cy="100"
r="80"
fill="blue"
/>
</svg>| Part | Purpose |
|---|---|
| <svg> | Creates the SVG drawing area |
| width | Sets the displayed width |
| height | Sets the displayed height |
| Shape Elements | Describe what should be drawn |
| Coordinates | Control where shapes appear |
| Styling Attributes | Control colors and borders |
The <svg> element defines the area where SVG shapes are positioned and displayed.
SVG Coordinate System
SVG uses a two-dimensional coordinate system to position shapes.
The coordinate system begins at the top-left corner of the SVG drawing area.
The top-left position is represented by the coordinate (0, 0).
The X-axis increases as you move toward the right, while the Y-axis increases as you move downward.
(0,0) ───────────────────────→ X
│
│
│ ● (100,100)
│
│
│
▼
Y| Direction | Coordinate Change |
|---|---|
| Move Right | X increases |
| Move Left | X decreases |
| Move Down | Y increases |
| Move Up | Y decreases |
<svg width="250" height="250">
<circle
cx="100"
cy="100"
r="5"
fill="red"
/>
</svg>Beginners often expect positive Y values to move upward. In the default SVG coordinate system, increasing Y moves downward.
Common SVG Elements
SVG provides different elements for drawing different types of shapes and content.
<circle>
Draws a circle using a center position and radius.
<rect>
Draws rectangles and squares.
<line>
Draws a straight line between two points.
⬭ <ellipse>
Draws an oval using horizontal and vertical radii.
<polygon>
Draws a closed shape from multiple coordinate points.
<text>
Displays text inside the SVG drawing area.
| Element | Purpose | Important Attributes |
|---|---|---|
| <circle> | Draws a circle | cx, cy, r |
| <rect> | Draws a rectangle | x, y, width, height |
| <line> | Draws a straight line | x1, y1, x2, y2 |
| <ellipse> | Draws an ellipse | cx, cy, rx, ry |
| <polygon> | Draws a closed multi-point shape | points |
| <text> | Draws text | x, y |
A circle needs a center and radius, while a line needs a starting point and an ending point.
Example 1: Drawing a Circle
The <circle> element draws a circle inside the SVG drawing area.
<svg width="200" height="200">
<circle
cx="100"
cy="100"
r="80"
fill="blue"
/>
</svg>| Attribute | Meaning | Value |
|---|---|---|
| cx | Horizontal center position | 100 |
| cy | Vertical center position | 100 |
| r | Circle radius | 80 |
| fill | Interior color | blue |
Example 2: Drawing a Rectangle
The <rect> element draws a rectangle or square.
<svg width="200" height="100">
<rect
x="10"
y="10"
width="180"
height="80"
fill="green"
/>
</svg>| Attribute | Meaning | Value |
|---|---|---|
| x | Horizontal starting position | 10 |
| y | Vertical starting position | 10 |
| width | Rectangle width | 180 |
| height | Rectangle height | 80 |
| fill | Interior color | green |
The x and y attributes position the top-left corner of the rectangle.
Example 3: Drawing a Line
The <line> element draws a straight line between two coordinate points.
<svg width="200" height="100">
<line
x1="10"
y1="10"
x2="190"
y2="90"
stroke="black"
stroke-width="2"
/>
</svg>| Attribute | Meaning |
|---|---|
| x1 | X coordinate of the starting point |
| y1 | Y coordinate of the starting point |
| x2 | X coordinate of the ending point |
| y2 | Y coordinate of the ending point |
| stroke | Line color |
| stroke-width | Line thickness |
Example 4: Drawing an Ellipse
The <ellipse> element draws an oval shape.
Unlike a circle, an ellipse has separate horizontal and vertical radii.
<svg width="200" height="100">
<ellipse
cx="100"
cy="50"
rx="90"
ry="40"
fill="purple"
/>
</svg>| Attribute | Meaning |
|---|---|
| cx | Horizontal center position |
| cy | Vertical center position |
| rx | Horizontal radius |
| ry | Vertical radius |
| fill | Interior color |
Circle
- Uses one radius.
- Uses the r attribute.
- Equal distance from center in every direction.
⬭ Ellipse
- Uses two radii.
- Uses rx and ry.
- Can be wider or taller than a circle.
Example 5: Drawing a Polygon
The <polygon> element creates a closed shape by connecting multiple coordinate points.
The points attribute contains a sequence of X and Y coordinate pairs.
<svg width="200" height="200">
<polygon
points="
100,10
120,80
190,80
130,120
150,190
100,150
50,190
70,120
10,80
80,80
"
fill="gold"
/>
</svg>The browser connects each coordinate pair to the next point and automatically closes the final shape.
Changing the coordinate pairs inside the points attribute changes the structure of the polygon.
Example 6: Adding Text
The <text> element displays text inside an SVG drawing area.
<svg width="200" height="100">
<text
x="40"
y="60"
fill="red"
font-size="24"
>
Hello SVG
</text>
</svg>| Attribute | Purpose |
|---|---|
| x | Horizontal position of the text |
| y | Vertical baseline position |
| fill | Text color |
| font-size | Text size |
Text inside an SVG can be positioned and styled as part of the overall vector graphic.
Complete SVG Example
Multiple SVG elements can be combined inside one drawing area to create a complete graphic.
<svg width="300" height="200">
<rect
x="10"
y="10"
width="280"
height="180"
fill="lightblue"
/>
<circle
cx="150"
cy="100"
r="50"
fill="red"
/>
<text
x="150"
y="105"
fill="white"
font-size="20"
text-anchor="middle"
>
SVG Demo
</text>
</svg>SVG elements are generally painted in document order. Elements written later can appear above elements written earlier.
Common SVG Attributes
SVG elements use attributes to control their dimensions, coordinates, colors, borders, and other visual properties.
| Attribute | Purpose |
|---|---|
| width | Controls the displayed width |
| height | Controls the displayed height |
| viewBox | Defines the internal coordinate system |
| fill | Controls the interior color |
| stroke | Controls the outline color |
| stroke-width | Controls outline thickness |
| x | Sets a horizontal position |
| y | Sets a vertical position |
| cx | Sets the horizontal center |
| cy | Sets the vertical center |
| r | Sets a circle radius |
| rx | Sets a horizontal radius |
| ry | Sets a vertical radius |
| points | Defines polygon coordinate points |
| text-anchor | Controls text alignment around its position |
<svg width="200" height="200">
<circle
cx="100"
cy="100"
r="70"
fill="lightblue"
stroke="navy"
stroke-width="8"
/>
</svg>fill
- Controls the inside of a shape.
- Can use color values.
- Can be set to none.
stroke
- Controls the shape outline.
- Uses stroke-width for thickness.
- Can be styled independently from fill.
The viewBox attribute is essential for building SVG graphics that scale responsively while preserving their internal coordinate system.
SVG vs Normal Images
SVG and raster images solve different types of visual problems.
| Feature | SVG Vector Graphics | JPG / PNG Raster Images |
|---|---|---|
| Composition | Shapes and mathematical instructions | Grid of pixels |
| Scaling | Remains sharp | Can become blurry |
| Resolution | Resolution independent | Fixed pixel resolution |
| Code Editing | Can be edited as markup | Requires image editing tools |
| CSS Styling | Many properties can be styled | Limited to the image as a whole |
| JavaScript Interaction | Individual elements can be targeted | Pixels are not normal DOM elements |
| Best For | Logos, icons, charts, diagrams | Photographs and complex raster artwork |
| Simple Graphic Size | Often compact | Depends on dimensions and compression |
| Complex Photos | Usually unsuitable | Ideal use case |
Choose SVG For
- Logos.
- Interface icons.
- Charts and graphs.
- Diagrams.
- Simple illustrations.
- Scalable interface graphics.
Choose Raster Images For
- Photographs.
- Detailed natural scenes.
- Complex textures.
- Pixel-based artwork.
- Camera images.
- Highly detailed color information.
SVG is excellent for structured vector graphics, but photographs are normally better represented by raster image formats.
Browser Rendering Flow
When the browser encounters inline SVG markup, it reads the SVG elements and calculates how each shape should be drawn.
HTML Document
│
▼
Find <svg>
│
▼
Create Drawing Area
│
▼
Read Shape Elements
│
▼
Read Coordinates
│
▼
Calculate Geometry
│
▼
Apply Fill & Stroke
│
▼
Draw Graphic
│
▼
Display SVGRaster Rendering
- Browser decodes stored pixels.
- Pixels have a fixed original resolution.
- Large scaling can reveal pixelation.
SVG Rendering
- Browser interprets drawing instructions.
- Geometry is calculated for display.
- Shapes remain sharp when scaled.
SVG markup describes the graphic. The browser uses those instructions to calculate and paint the final visual result.
Real-World Applications
SVG is used throughout modern websites and web applications.
Company Logos
Brand marks that remain sharp at many sizes.
Interface Icons
Navigation, buttons, controls, and status indicators.
Charts
Bar charts, line charts, pie charts, and data visualizations.
Educational Diagrams
Geometry, scientific diagrams, and visual explanations.
Interactive Maps
Regions and locations that can respond to user interaction.
Loading Indicators
Animated spinners and progress graphics.
Dashboards
Graphs, gauges, indicators, and analytics visuals.
Illustrations
Responsive decorative graphics and interface artwork.
Advantages of SVG
SVG provides several important benefits for web graphics.
Infinite Scaling
Vector shapes remain sharp when resized.
Responsive Graphics
SVG can adapt to different screen sizes.
CSS Styling
Many SVG properties can be controlled through CSS.
JavaScript Interaction
Individual SVG elements can respond to application logic.
Animation
Shapes and properties can be animated.
Text-Based Format
SVG markup can be inspected, edited, and generated.
Searchable Text
Text inside inline SVG can remain actual text.
DOM Integration
Inline SVG elements can participate in the document structure.
Visual Advantages
- Sharp scaling.
- Crisp edges.
- Responsive dimensions.
- Flexible colors.
- Animation support.
Development Advantages
- Editable markup.
- CSS integration.
- JavaScript integration.
- Reusable graphics.
- Dynamic generation.
SVG provides the greatest advantage when the visual can be represented efficiently using shapes, paths, text, and mathematical geometry.
Common Beginner Mistakes
SVG is vector-based, while PNG is a raster format made from pixels.
Complex photographs are normally better represented using raster image formats.
SVG coordinates begin at the top-left and positive Y values move downward.
Coordinates beyond the visible SVG region can make shapes appear missing.
Responsive SVG graphics are much easier to manage when an appropriate viewBox is defined.
Fill controls the interior, while stroke controls the outline.
A circle uses cx, cy, and r rather than x, y, width, and height.
A line requires starting and ending coordinates.
Polygon points must contain valid coordinate pairs.
Later SVG elements can cover earlier elements.
Exported graphics can contain unnecessary metadata and excessive path data.
Meaningful graphics should provide appropriate accessible context.
<!-- ❌ Circle center is outside the visible area -->
<svg width="200" height="200">
<circle
cx="500"
cy="500"
r="50"
fill="red"
/>
</svg>
<!-- ✅ Circle is inside the visible area -->
<svg width="200" height="200">
<circle
cx="100"
cy="100"
r="50"
fill="red"
/>
</svg>Poor SVG Usage
- Using SVG for photographs.
- Ignoring the coordinate system.
- No responsive scaling strategy.
- Unoptimized generated markup.
- Missing accessibility information.
Better SVG Usage
- Use SVG for vector-friendly graphics.
- Understand coordinates.
- Use viewBox for scaling.
- Optimize exported files.
- Provide accessible context.
Best Practices
- Use SVG for logos, icons, diagrams, charts, and other vector-friendly graphics.
- Use raster formats for photographs and highly detailed pixel-based images.
- Understand the SVG coordinate system before positioning shapes.
- Remember that the coordinate origin begins at the top-left.
- Remember that positive X values move toward the right.
- Remember that positive Y values move downward.
- Use the <svg> element to define the drawing area.
- Use a meaningful viewBox for responsive SVG graphics.
- Keep the internal coordinate system logical and easy to understand.
- Use width and height when fixed display dimensions are appropriate.
- Use responsive CSS when the graphic must adapt to its container.
- Avoid hard-coded dimensions that cause mobile overflow.
- Use <circle> for circles.
- Use <rect> for rectangles and squares.
- Use <line> for straight lines.
- Use <ellipse> for oval shapes.
- Use <polygon> for closed multi-point shapes.
- Use <text> when text must be part of the SVG graphic.
- Use paths for complex custom shapes when necessary.
- Keep shape coordinates inside the intended drawing area.
- Use fill to control the interior of shapes.
- Use stroke to control shape outlines.
- Use stroke-width to control outline thickness.
- Use CSS classes when many SVG elements share the same styling.
- Avoid repeating unnecessary inline styles.
- Keep SVG markup readable during development.
- Format complex SVG code consistently.
- Group related elements when the graphic becomes complex.
- Use meaningful IDs and class names.
- Avoid unnecessary elements.
- Optimize SVG files exported from design tools.
- Remove unnecessary metadata from production SVG files.
- Remove unused definitions and hidden elements.
- Reduce excessive decimal precision when safe.
- Do not manually edit complex generated paths unless necessary.
- Use SVG optimization tools carefully.
- Test optimized files before deployment.
- Understand SVG drawing order.
- Place background elements before foreground elements.
- Remember that later elements can cover earlier elements.
- Use text-anchor when SVG text needs horizontal alignment.
- Use dominant-baseline carefully when vertical alignment is required.
- Use preserveAspectRatio when scaling behavior requires control.
- Use viewBox to preserve a logical internal coordinate system.
- Test graphics at very small sizes.
- Test graphics at very large sizes.
- Test graphics on high-density displays.
- Test graphics in light mode.
- Test graphics in dark mode when relevant.
- Use currentColor when icons should inherit surrounding text color.
- Use CSS custom properties for themeable SVG graphics.
- Avoid fixed colors when graphics must support multiple themes.
- Use semantic surrounding HTML for important graphics.
- Provide accessible names for meaningful graphics.
- Use appropriate title or description techniques when required.
- Hide purely decorative graphics from assistive technology when appropriate.
- Do not place essential information only inside inaccessible graphics.
- Provide text alternatives for important diagrams.
- Ensure interactive SVG elements are keyboard accessible.
- Use visible focus indicators for interactive graphics.
- Do not rely only on color to communicate meaning.
- Use sufficient contrast.
- Keep interactive targets large enough to use.
- Use JavaScript only when interaction is genuinely required.
- Use CSS for simple hover and transition effects.
- Avoid excessive animation.
- Respect reduced-motion preferences when adding animation.
- Avoid overly complex SVG graphics that create rendering problems.
- Monitor performance for large numbers of SVG elements.
- Consider Canvas for extremely large numbers of frequently changing drawing objects.
- Use SVG when individual graphical elements need DOM interaction.
- Use external SVG files when reuse and caching are important.
- Use inline SVG when direct CSS and JavaScript control is required.
- Understand the difference between inline and external SVG usage.
- Sanitize untrusted SVG content before displaying it.
- Never treat arbitrary SVG markup as automatically safe.
- Avoid injecting untrusted SVG code directly into the page.
- Keep reusable icons consistent in size and coordinate system.
- Use an icon system for large applications.
- Document complex diagrams.
- Keep source files for important graphics.
- Test browser compatibility for advanced SVG features.
- Always balance scalability, accessibility, maintainability, security, and performance.
<svg
viewBox="0 0 200 200"
style="width: 100%; max-width: 400px; height: auto;"
role="img"
aria-label="Blue circle"
>
<circle
cx="100"
cy="100"
r="80"
fill="blue"
/>
</svg>The viewBox defines the internal coordinate system while CSS controls how large the SVG appears on the page.
Frequently Asked Questions
What does SVG stand for?
SVG stands for Scalable Vector Graphics.
What is SVG?
SVG is an XML-based format for creating two-dimensional vector graphics using shapes, coordinates, paths, and other drawing instructions.
Which HTML element creates an SVG drawing area?
The <svg> element creates the drawing area.
Why does SVG remain sharp when enlarged?
SVG graphics are described using mathematical geometry instead of a fixed grid of pixels.
Where does the SVG coordinate system begin?
The default coordinate system begins at the top-left corner at position (0,0).
Which direction does the X-axis move?
Positive X values move toward the right.
Which direction does the Y-axis move?
Positive Y values move downward.
Which SVG element draws a circle?
The <circle> element.
Which attributes position a circle?
The cx and cy attributes position the center of the circle.
Which attribute controls a circle radius?
The r attribute.
Which SVG element draws a rectangle?
The <rect> element.
Which SVG element draws a line?
The <line> element.
Which attributes define a line?
The x1, y1, x2, and y2 attributes define the starting and ending points.
Which SVG element draws an ellipse?
The <ellipse> element.
What is the difference between r, rx, and ry?
The r attribute defines a circle radius, while rx and ry define the horizontal and vertical radii of an ellipse.
Which SVG element draws a polygon?
The <polygon> element.
What does the points attribute do?
It provides coordinate pairs that define the corners of a polygon.
Can SVG contain text?
Yes. The <text> element displays text inside SVG.
What does fill do?
The fill property controls the interior color of a shape.
What does stroke do?
The stroke property controls the outline color of a shape.
What does stroke-width do?
It controls the thickness of the shape outline.
What is viewBox?
The viewBox attribute defines the internal coordinate system and visible region of an SVG.
Why is viewBox important?
It helps SVG graphics scale responsively while preserving their internal coordinate relationships.
Is SVG better than PNG?
It depends on the content. SVG is better for vector-friendly graphics, while PNG is better for many pixel-based images.
Is SVG suitable for photographs?
Usually no. Photographs are normally better stored using raster image formats.
Can SVG be styled with CSS?
Yes. Many SVG properties can be controlled using CSS.
Can SVG use JavaScript?
Yes. Inline SVG elements can be selected and manipulated with JavaScript.
Can SVG be animated?
Yes. SVG graphics can be animated using CSS, JavaScript, and SVG animation techniques.
Can SVG be responsive?
Yes. The viewBox attribute combined with responsive CSS allows SVG graphics to scale effectively.
Why is my SVG shape invisible?
The shape may be outside the visible coordinate area, have no visible fill or stroke, or use incorrect attributes.
Does SVG drawing order matter?
Yes. Elements written later are generally painted above elements written earlier.
Are SVG files always small?
No. Simple graphics can be compact, but extremely complex SVG files can become large.
Can SVG contain security risks?
Yes. Untrusted SVG markup should be treated carefully because SVG can contain active or complex content.
What should I learn after HTML SVG?
The next lesson is HTML Canvas, which introduces script-based pixel drawing.
Key Takeaways
- SVG stands for Scalable Vector Graphics.
- SVG is an XML-based vector graphics format.
- SVG graphics are described using shapes, coordinates, and drawing instructions.
- SVG graphics remain sharp when resized.
- Raster images are made from pixels.
- Raster images can become blurry when enlarged.
- The <svg> element creates the SVG drawing area.
- SVG uses a two-dimensional coordinate system.
- The default origin is the top-left corner.
- The origin coordinate is (0,0).
- Positive X values move toward the right.
- Positive Y values move downward.
- The <circle> element draws circles.
- Circle position uses cx and cy.
- Circle size uses the r attribute.
- The <rect> element draws rectangles.
- Rectangle position uses x and y.
- Rectangle dimensions use width and height.
- The <line> element draws straight lines.
- Lines use starting and ending coordinate pairs.
- The <ellipse> element draws oval shapes.
- Ellipses use rx and ry for horizontal and vertical radii.
- The <polygon> element creates closed multi-point shapes.
- The points attribute defines polygon coordinates.
- The <text> element displays text inside SVG.
- The fill property controls shape interiors.
- The stroke property controls outlines.
- The stroke-width property controls outline thickness.
- Multiple SVG elements can be combined into one graphic.
- SVG drawing order affects which elements appear above others.
- The viewBox attribute defines the internal coordinate system.
- viewBox is important for responsive SVG graphics.
- SVG is ideal for logos and icons.
- SVG is useful for charts and diagrams.
- SVG can be styled using CSS.
- SVG can be controlled using JavaScript.
- SVG supports animation and interaction.
- SVG is not normally the best format for photographs.
- Complex SVG files should be optimized.
- Meaningful graphics should consider accessibility.
- Untrusted SVG content should be handled carefully.
- SVG is one of the most important technologies for scalable web graphics.
Summary
SVG stands for Scalable Vector Graphics and provides a way to create graphics using shapes, coordinates, and mathematical drawing instructions.
Unlike raster images such as JPG and PNG, SVG graphics are not built from a fixed grid of pixels.
Because the browser can recalculate vector geometry at different sizes, SVG graphics can remain sharp when enlarged or reduced.
The <svg> element creates the drawing area, and elements such as <circle>, <rect>, <line>, <ellipse>, <polygon>, and <text> create visual content inside it.
SVG uses a coordinate system that begins at the top-left corner, where positive X values move right and positive Y values move downward.
Different shape elements use different attributes to define their positions and dimensions.
The fill property controls the interior of a shape, while stroke and stroke-width control its outline.
Multiple SVG elements can be combined to create complete illustrations, diagrams, charts, and interface graphics.
The order of SVG elements matters because later elements can be painted above earlier elements.
The viewBox attribute is especially important for creating responsive SVG graphics with a consistent internal coordinate system.
SVG is widely used for company logos, interface icons, charts, maps, diagrams, educational graphics, dashboards, and interactive visualizations.
SVG can be styled with CSS, controlled with JavaScript, and animated to create dynamic visual experiences.
SVG should be used for graphics that benefit from vector scaling, while raster image formats remain more suitable for photographs and highly detailed pixel-based images.
By understanding SVG elements, coordinates, attributes, responsive scaling, accessibility, and optimization, developers can create sharp and flexible graphics for modern websites.
In the next lesson, you will learn about HTML Canvas and how JavaScript can be used to draw pixel-based graphics dynamically.