LearnContact
Lesson 2310 min read

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.
Create <svg> Drawing Area
Add Shape Elements
Provide Coordinates
Apply Fill and Stroke
Browser Calculates Geometry
Sharp Graphic Appears
SVG is Code-Based Graphics

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.

Simple SVG
<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.

Simple Definition

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.

Graphic Needed
Describe Graphic with Shapes
Browser Calculates Geometry
Scale to Required Size
Graphic Remains Sharp

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 vs Vector
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 & Icons
Think in Instructions

A 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.

General Syntax
<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 with a Shape
<svg width="200" height="200">
    <circle
        cx="100"
        cy="100"
        r="80"
        fill="blue"
    />
</svg>
PartPurpose
<svg>Creates the SVG drawing area
widthSets the displayed width
heightSets the displayed height
Shape ElementsDescribe what should be drawn
CoordinatesControl where shapes appear
Styling AttributesControl colors and borders
Create <svg>
Define Drawing Area
Add Shape Element
Set Coordinates
Apply Appearance
Browser Draws Graphic
The SVG Element is the Canvas

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.

SVG Coordinate System
(0,0) ───────────────────────→ X
  │
  │
  │          ● (100,100)
  │
  │
  │
  ▼
  Y
DirectionCoordinate Change
Move RightX increases
Move LeftX decreases
Move DownY increases
Move UpY decreases
Point at Coordinate 100,100
<svg width="250" height="250">
    <circle
        cx="100"
        cy="100"
        r="5"
        fill="red"
    />
</svg>
Start at (0,0)
Move Right Using X
Move Down Using Y
Reach Target Coordinate
Draw the Shape
Y Moves Downward

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.

ElementPurposeImportant Attributes
<circle>Draws a circlecx, cy, r
<rect>Draws a rectanglex, y, width, height
<line>Draws a straight linex1, y1, x2, y2
<ellipse>Draws an ellipsecx, cy, rx, ry
<polygon>Draws a closed multi-point shapepoints
<text>Draws textx, y
Different Shapes Use Different Coordinates

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.

Circle
<svg width="200" height="200">
    <circle
        cx="100"
        cy="100"
        r="80"
        fill="blue"
    />
</svg>
AttributeMeaningValue
cxHorizontal center position100
cyVertical center position100
rCircle radius80
fillInterior colorblue
Circle Output
Create SVG Area
Locate Center at (100,100)
Use Radius 80
Apply Blue Fill
Browser Draws Circle

Example 2: Drawing a Rectangle

The <rect> element draws a rectangle or square.

Rectangle
<svg width="200" height="100">
    <rect
        x="10"
        y="10"
        width="180"
        height="80"
        fill="green"
    />
</svg>
AttributeMeaningValue
xHorizontal starting position10
yVertical starting position10
widthRectangle width180
heightRectangle height80
fillInterior colorgreen
Rectangle Output
Rectangle Position

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.

Line
<svg width="200" height="100">
    <line
        x1="10"
        y1="10"
        x2="190"
        y2="90"
        stroke="black"
        stroke-width="2"
    />
</svg>
AttributeMeaning
x1X coordinate of the starting point
y1Y coordinate of the starting point
x2X coordinate of the ending point
y2Y coordinate of the ending point
strokeLine color
stroke-widthLine thickness
Line Output
Choose Starting Point
Choose Ending Point
Set Stroke Color
Set Stroke Width
Browser Connects the Points

Example 4: Drawing an Ellipse

The <ellipse> element draws an oval shape.

Unlike a circle, an ellipse has separate horizontal and vertical radii.

Ellipse
<svg width="200" height="100">
    <ellipse
        cx="100"
        cy="50"
        rx="90"
        ry="40"
        fill="purple"
    />
</svg>
AttributeMeaning
cxHorizontal center position
cyVertical center position
rxHorizontal radius
ryVertical radius
fillInterior color
Ellipse Output

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.

Polygon Star
<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.

Polygon Output
Define First Point
Define Additional Points
Browser Connects Points
Final Point Connects to First
Closed Shape Appears
Points Determine the 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 Text
<svg width="200" height="100">
    <text
        x="40"
        y="60"
        fill="red"
        font-size="24"
    >
        Hello SVG
    </text>
</svg>
AttributePurpose
xHorizontal position of the text
yVertical baseline position
fillText color
font-sizeText size
SVG Text Output
SVG Text is Part of the Graphic

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.

Complete SVG
<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>
Complete SVG Output
Create Drawing Area
Draw Background Rectangle
Draw Circle Above Rectangle
Add Text Above Circle
Display Combined Graphic
Drawing Order Matters

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.

AttributePurpose
widthControls the displayed width
heightControls the displayed height
viewBoxDefines the internal coordinate system
fillControls the interior color
strokeControls the outline color
stroke-widthControls outline thickness
xSets a horizontal position
ySets a vertical position
cxSets the horizontal center
cySets the vertical center
rSets a circle radius
rxSets a horizontal radius
rySets a vertical radius
pointsDefines polygon coordinate points
text-anchorControls text alignment around its position
Fill and Stroke
<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.
Learn viewBox

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.

FeatureSVG Vector GraphicsJPG / PNG Raster Images
CompositionShapes and mathematical instructionsGrid of pixels
ScalingRemains sharpCan become blurry
ResolutionResolution independentFixed pixel resolution
Code EditingCan be edited as markupRequires image editing tools
CSS StylingMany properties can be styledLimited to the image as a whole
JavaScript InteractionIndividual elements can be targetedPixels are not normal DOM elements
Best ForLogos, icons, charts, diagramsPhotographs and complex raster artwork
Simple Graphic SizeOften compactDepends on dimensions and compression
Complex PhotosUsually unsuitableIdeal 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 Not Always Better

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.

Read HTML Document
Find <svg> Element
Create SVG Drawing Area
Read Shape Elements
Read Coordinates and Attributes
Calculate Geometry
Paint Shapes in Order
Display Crisp Graphic
SVG Rendering Flow
HTML Document
      │
      ▼
Find <svg>
      │
      ▼
Create Drawing Area
      │
      ▼
Read Shape Elements
      │
      ▼
Read Coordinates
      │
      ▼
Calculate Geometry
      │
      ▼
Apply Fill & Stroke
      │
      ▼
Draw Graphic
      │
      ▼
Display SVG

Raster 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.
The Browser Draws the Result

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.

Design Vector Graphic
Export or Write SVG
Add to Website
Style Responsively
Add Interaction if Needed
Display Across Screen Sizes

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.
Best for Structured Graphics

SVG provides the greatest advantage when the visual can be represented efficiently using shapes, paths, text, and mathematical geometry.

Common Beginner Mistakes

Confusing SVG with PNG

SVG is vector-based, while PNG is a raster format made from pixels.

Using SVG for Photographs

Complex photographs are normally better represented using raster image formats.

Forgetting the Coordinate System

SVG coordinates begin at the top-left and positive Y values move downward.

Placing Shapes Outside the Drawing Area

Coordinates beyond the visible SVG region can make shapes appear missing.

Forgetting the viewBox

Responsive SVG graphics are much easier to manage when an appropriate viewBox is defined.

Confusing fill and stroke

Fill controls the interior, while stroke controls the outline.

Using Incorrect Circle Attributes

A circle uses cx, cy, and r rather than x, y, width, and height.

Using Incorrect Line Attributes

A line requires starting and ending coordinates.

Using Invalid Polygon Points

Polygon points must contain valid coordinate pairs.

Ignoring Drawing Order

Later SVG elements can cover earlier elements.

Writing Huge Unoptimized SVG Files

Exported graphics can contain unnecessary metadata and excessive path data.

Ignoring Accessibility

Meaningful graphics should provide appropriate accessible context.

Shape Outside the Visible Area
<!-- ❌ 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.
Responsive SVG with viewBox
<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>
Use viewBox for Responsive 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.

Next Lesson →

Canvas