Chapter 2: SVG in HTML

What is SVG in HTML

I’m going to explain it like we’re sitting together on your laptop, step-by-step, very slowly, with simple English, real analogies, lots of copy-paste examples, and clear “why” explanations. By the end you’ll know exactly:

  • What SVG means when we say “in HTML”
  • The 4 main ways to put SVG inside an HTML page
  • Which way is best for different situations
  • How to style and animate it with CSS
  • Common mistakes beginners make

Ready? Let’s open VS Code (or Notepad) and start!

1. Quick Recap: What is SVG? (30-second reminder)

SVG = Scalable Vector Graphics = pictures made with math/code (not pixels) → zoom forever = no blur → small file size for icons/logos → can be styled with CSS, animated, made interactive

Now the real question: How do we actually use SVG inside a normal .html webpage?

There are four popular ways to bring SVG into HTML — and each has its own superpowers and rules.

2. The 4 Ways to Use SVG in HTML (Big Comparison Table)

# Method Code Looks Like Can style with CSS? Can animate with CSS? Can use JavaScript? Best for File size impact
1 Inline SVG <svg>…</svg> directly in HTML Yes – full power Yes Yes – full access Icons you want to animate/color-change Slightly bigger HTML
2 <img> tag <img src=”heart.svg” alt=”like”> Very limited Almost no Very limited Simple static icons Separate file (cached)
3 CSS background-image background: url(‘arrow.svg’) Very limited Limited No Repeating patterns, decorative bg Separate file
4 <object> tag <object data=”map.svg” type=”image/svg+xml”> Some (with tricks) Some Limited Very old browser support + fallback Separate file

Teacher recommendation for 2025–2026: Use #1 (Inline SVG) most of the time — especially if you want color changes, hover effects, or animations. Use #2 (<img>) only when the SVG is purely decorative/static and you want it cached separately.

3. Method #1 – Inline SVG (The Most Powerful & Most Popular)

You literally copy-paste the <svg>…</svg> code inside your HTML.

Live Example – Simple Heart Icon

HTML

What’s happening here?

  • The <svg> is inside the HTML → browser treats it like any other element
  • We gave it a class .heart → normal CSS works!
  • :hover → scale bigger + change fill color
  • viewBox → makes sure it scales perfectly no matter what width/height we set

Magic benefit: One single SVG code → unlimited color variations with CSS only (no need for 10 different PNG files).

4. Method #2 – Using <img> (Simplest – but weakest)

HTML

Good points

  • Very easy
  • Browser caches the file → fast on repeat visits
  • Works with srcset for responsive images

Bad points

  • Cannot change color with CSS (except trick with filter)
  • Cannot animate internal parts
  • Cannot add click events to inner shapes

When to use: Static icons that never change color (example: company logo in header)

5. Quick Bonus: Change Color of <img> SVG with CSS Filter (Trick)

CSS

→ turns blue icon purple, etc. But it’s not perfect — inline is still better for real control.

6. Method #3 – CSS Background (Mostly for Decorative Stuff)

CSS

Good for subtle repeating backgrounds, not for important icons.

7. Real-World Examples You See Every Day (2026 Websites)

Website / App How they use SVG in HTML Why?
YouTube Inline SVG for play button, like button Animate on hover, change color dark mode
TailwindCSS docs Inline SVG icons everywhere Easy to change size/color with classes
Google Maps SVG for pins, routes (vector maps!) Zoom forever without pixelation
Most React apps Inline SVG or component <HeartIcon /> Full control + reusability
Twitter/X Inline SVG for bird logo, hearts, retweets Theme changes instantly

8. Common Beginner Mistakes (Arman – Don’t Do These!)

  1. Forgetting viewBox → icon becomes stretched/weird when resized
  2. Using <img> when you need hover color change → then frustrated why CSS doesn’t work
  3. Putting huge 5000×5000 SVG code inline → page becomes slow (optimize first!)
  4. Not adding role=”img” + aria-label → bad for screen readers

Good habit:

HTML

9. Your 5-Minute Practice Task

  1. Copy the heart example above
  2. Change the hover color to #00ff88 (bright green)
  3. Make it 120px big
  4. Add another class .pulse with animation:
CSS

Apply both .heart.pulse — watch it beat!

Paste your final code here if you want feedback — I’ll correct/review like a proud sir 😄

Summary – One-line Takeaway

“SVG in HTML” mostly means inline <svg>…</svg> code so you can use CSS and JavaScript to control colors, size, animation, and interaction — something impossible with normal PNG/JPG images.

Got any doubt right now? “Sir, viewBox aur width mein kya fark hai?” “Animation kaise karte hain SVG mein?” “Game mein SVG use kar sakte hain kya?”

Ask anything — we’ll go as deep as you want next!

Class rocking… ab aap bolo! 🎨🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *