1. Home
  2. Coding Hub
  3. Coding Notes
  4. Html
  5. Inline Svg
45% OFFBLACK FRIDAY SALE
Sale ends in
03Days
11Hrs
37Mins
08Secs
Claim
  1. Home
  2. Coding Hub
  3. Coding Notes
  4. Html
  5. Inline Svg

Limited time offer

45% OFF

BLACK FRIDAY SALE

Sale ends in

03Days
11Hrs
37Mins
08Secs
Claim
HTMLCSSJavaScriptPythonJavaC++React.jsSQLNext.jsNode.js
Underrated CoderUnderrated Coder
Underrated CoderUnderrated Coder
Resources
  • Coding Hub
  • Roadmap
  • DSA Sheet
  • Coding Notes
  • Coding Quiz
  • Code Compiler
  • Coder AI
Interview Preparation
  • 1:1 Mentorship
  • Interview Q&A
Community
  • Blog
  • Coder Community
Help & Support
  • Contact Us
  • Troubleshooting
  • Report a Bug
HTMLCSSJavaScriptPythonJavaC++React.jsSQLNext.jsNode.js
Outline
HTML IntroductionDOM ParsingDocument StructureMetadata & <head>Viewport SetupLinks & FaviconsScript Loading
Heading HierarchyText SemanticsBlock vs InlineHyperlinks & AnchorsHTML ListsSemantic LayoutSectioning Elements
Image OptimizationResponsive <picture>Audio & VideoMedia <track>Inline SVG<iframe> Sandboxing
Table BasicsTable SemanticsSpanning & Captions
Form ArchitectureText InputsSelection ControlsSpecialized InputsLabels & Fieldsets<datalist> AutocompleteNative ValidationForm Submission
<details> Accordions<dialog> ModalsPopover APIInvoker Commands<template> Tag
data-* AttributesWeb AccessibilityARIA EssentialsOpen Graph SEOResource Hints
HTML

Inline SVG

Overview

Scalable Vector Graphics (SVG) are mathematical formulas defining shapes, rather than grids of pixels (like JPG or PNG). Because they are math-based, they scale infinitely without ever getting pixelated or blurry, making them perfect for logos and UI icons. More importantly, because SVG files are literally just XML/HTML text, you can paste the code directly inline into your HTML document. This eliminates the HTTP request required to download an image file and allows you to manipulate the SVG shapes directly using standard CSS and JavaScript.

Syntax

html
<!-- 
  Pasting SVG code directly into the HTML!
  We can use standard CSS classes to style it.
-->
<svg 
  xmlns="http://www.w3.org/2000/svg" 
  viewBox="0 0 24 24" 
  width="48" 
  height="48"
  class="icon-heart"
>
  <!-- We can change the color dynamically using the 'fill' attribute -->
  <path 
    fill="currentColor" 
    d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"
  />
</svg>

Common Pitfalls

  • Missing the viewBox attribute. Without the viewBox (which defines the internal coordinate system of the drawing), the SVG will not scale correctly when you attempt to change its width and height via CSS.
  • Pasting massive, unoptimized SVGs exported directly from Figma or Adobe Illustrator. These files contain thousands of lines of useless metadata and editor junk. Always run them through an optimizer like SVGOMG before pasting them into your HTML.

Interview Questions

Why is fill="currentColor" a powerful trick for Inline SVGs?

It commands the SVG to inherit its fill color from the parent element's CSS color property. This allows you to easily change the icon's color on hover or in Dark Mode just by changing text color on the parent button or div.

Real-World Example

A highly dynamic icon component in React utilizing Inline SVGs.

example
html
// Because it's inline, React can control the SVG perfectly!
const CheckIcon = ({ className }) => (
  <svg 
    className={`w-6 h-6 ${className}`} 
    fill="none" 
    stroke="currentColor" 
    viewBox="0 0 24 24"
  >
    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
  </svg>
);

Check Your Knowledge

Test your understanding of Inline SVG with these quick questions.

Question 1

What is the primary performance benefit of using an Inline SVG over an `<img>` tag linking to an SVG file?

Take HTML Quiz
Outline
HTML IntroductionDOM ParsingDocument StructureMetadata & <head>Viewport SetupLinks & FaviconsScript Loading
Heading HierarchyText SemanticsBlock vs InlineHyperlinks & AnchorsHTML ListsSemantic LayoutSectioning Elements
Image OptimizationResponsive <picture>Audio & VideoMedia <track>Inline SVG<iframe> Sandboxing
Table BasicsTable SemanticsSpanning & Captions
Form ArchitectureText InputsSelection ControlsSpecialized InputsLabels & Fieldsets<datalist> AutocompleteNative ValidationForm Submission
<details> Accordions<dialog> ModalsPopover APIInvoker Commands<template> Tag
data-* AttributesWeb AccessibilityARIA EssentialsOpen Graph SEOResource Hints
HTML

Inline SVG

Overview

Scalable Vector Graphics (SVG) are mathematical formulas defining shapes, rather than grids of pixels (like JPG or PNG). Because they are math-based, they scale infinitely without ever getting pixelated or blurry, making them perfect for logos and UI icons. More importantly, because SVG files are literally just XML/HTML text, you can paste the code directly inline into your HTML document. This eliminates the HTTP request required to download an image file and allows you to manipulate the SVG shapes directly using standard CSS and JavaScript.

Syntax

html
<!-- 
  Pasting SVG code directly into the HTML!
  We can use standard CSS classes to style it.
-->
<svg 
  xmlns="http://www.w3.org/2000/svg" 
  viewBox="0 0 24 24" 
  width="48" 
  height="48"
  class="icon-heart"
>
  <!-- We can change the color dynamically using the 'fill' attribute -->
  <path 
    fill="currentColor" 
    d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"
  />
</svg>

Common Pitfalls

  • Missing the viewBox attribute. Without the viewBox (which defines the internal coordinate system of the drawing), the SVG will not scale correctly when you attempt to change its width and height via CSS.
  • Pasting massive, unoptimized SVGs exported directly from Figma or Adobe Illustrator. These files contain thousands of lines of useless metadata and editor junk. Always run them through an optimizer like SVGOMG before pasting them into your HTML.

Interview Questions

Why is fill="currentColor" a powerful trick for Inline SVGs?

It commands the SVG to inherit its fill color from the parent element's CSS color property. This allows you to easily change the icon's color on hover or in Dark Mode just by changing text color on the parent button or div.

Real-World Example

A highly dynamic icon component in React utilizing Inline SVGs.

example
html
// Because it's inline, React can control the SVG perfectly!
const CheckIcon = ({ className }) => (
  <svg 
    className={`w-6 h-6 ${className}`} 
    fill="none" 
    stroke="currentColor" 
    viewBox="0 0 24 24"
  >
    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
  </svg>
);

Check Your Knowledge

Test your understanding of Inline SVG with these quick questions.

Question 1

What is the primary performance benefit of using an Inline SVG over an `<img>` tag linking to an SVG file?

Take HTML Quiz

HTML coding notes for Indian students — 41 topics with syntax, pitfalls, interview Q&A, and inline quizzes.

Topics in HTML

  1. HTML Introduction
  2. DOM Parsing
  3. Document Structure
  4. Metadata & <head>
  5. Viewport Setup
  6. Links & Favicons
  7. Script Loading
  8. Heading Hierarchy
  9. Text Semantics
  10. Block vs Inline
  11. Hyperlinks & Anchors
  12. HTML Lists
  13. Semantic Layout
  14. Sectioning Elements
  15. Image Optimization
  16. Responsive <picture>
  17. Audio & Video
  18. Media <track>
  19. Inline SVG
  20. <iframe> Sandboxing
  21. Table Basics
  22. Table Semantics
  23. Spanning & Captions
  24. Form Architecture
  25. Text Inputs
  26. Selection Controls
  27. Specialized Inputs
  28. Labels & Fieldsets
  29. <datalist> Autocomplete
  30. Native Validation
  31. Form Submission
  32. <details> Accordions
  33. <dialog> Modals
  34. Popover API
  35. Invoker Commands
  36. <template> Tag
  37. data-* Attributes
  38. Web Accessibility
  39. ARIA Essentials
  40. Open Graph SEO
  41. Resource Hints

Study next

  • All coding notes
  • HTML notes home
  • HTML certification quiz
  • Online compiler
  • Interview Q&A

HTML coding notes for Indian students — 41 topics with syntax, pitfalls, interview Q&A, and inline quizzes.

Topics in HTML

  1. HTML Introduction
  2. DOM Parsing
  3. Document Structure
  4. Metadata & <head>
  5. Viewport Setup
  6. Links & Favicons
  7. Script Loading
  8. Heading Hierarchy
  9. Text Semantics
  10. Block vs Inline
  11. Hyperlinks & Anchors
  12. HTML Lists
  13. Semantic Layout
  14. Sectioning Elements
  15. Image Optimization
  16. Responsive <picture>
  17. Audio & Video
  18. Media <track>
  19. Inline SVG
  20. <iframe> Sandboxing
  21. Table Basics
  22. Table Semantics
  23. Spanning & Captions
  24. Form Architecture
  25. Text Inputs
  26. Selection Controls
  27. Specialized Inputs
  28. Labels & Fieldsets
  29. <datalist> Autocomplete
  30. Native Validation
  31. Form Submission
  32. <details> Accordions
  33. <dialog> Modals
  34. Popover API
  35. Invoker Commands
  36. <template> Tag
  37. data-* Attributes
  38. Web Accessibility
  39. ARIA Essentials
  40. Open Graph SEO
  41. Resource Hints

Study next

  • All coding notes
  • HTML notes home
  • HTML certification quiz
  • Online compiler
  • Interview Q&A