Skip to content

✨ Creating an SVG File in VS Code from Text



plant

SVGs are tiny, scalable, and editable — perfect for icons and illustrations. But many devs think: *“I can’t write XML, it’s too finicky.”* Good news: **VS Code makes it painless.**

This guide walks you through the whole process — from blank file ➜ preview ➜ helpful extensions — until you’re happily hand-crafting icons.

💡 Why Start with Text?

  • 🎯 Precision – Full control over every attribute.
  • 📜 Version-control friendly – Clean diffs.
  • 🛠 No tooling required – Just VS Code.
  • 🌍 Cross-platform – Works anywhere XML is supported.

📝 If you can write HTML, you can write SVG.

1️⃣ Create a New SVG File

  1. Open VS Code
    Open the folder you want to store your SVG in.

  2. Create a file
    File ➜ New File → save as icon.svg.
    VS Code auto-detects .svg and enables SVG syntax.

2️⃣ Quick Emmet Abbreviation

VS Code ships with Emmet (shorthand → full markup).

svg
svg:root

Type svg:root and press Tab to expand:

svg
<svg
  width="100%"
  height="100%"
  viewBox="0 0 24 24"
  xmlns="http://www.w3.org/2000/svg"
  fill="none"
  stroke="currentColor"
  stroke-width="2"
  stroke-linecap="round"
  stroke-linejoin="round"
>
  <path d="" />
</svg>

3️⃣ Write Your First SVG

Let’s make a ❤️ heart icon:

svg
<svg
  width="48"
  height="48"
  viewBox="0 0 24 24"
  xmlns="http://www.w3.org/2000/svg"
  fill="none"
  stroke="red"
  stroke-width="2"
  stroke-linecap="round"
  stroke-linejoin="round"
>
  <path 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 3
           c1.74 0 3.41 0.81 4.5 2.09
           C13.09 3.81 14.76 3 16.5 3
           19.58 3 22 5.42 22 8.5
           c0 3.78-3.4 6.86-8.55 11.54L12 21.35z" />
</svg>

💡 Pro Tip: Add a metadata comment to keep track:

svg
<!--
  icon-heart.svg
  Size: 48×48
  Author: Your Name
-->

4️⃣ Preview in VS Code

👀 Built-in Webview

  • Open icon.svg.
  • Press Ctrl+Shift+V / Cmd+Shift+V.
  • A preview pane opens beside your code.
  • Install via Extensions → SVG Viewer.
  • Right-click file → Open in Preview.
  • Saves auto-reload instantly.

5️⃣ Optional: Live Server

Want to test in a real browser?

6️⃣ Handy Extensions & Settings

🌟 Extension💼 What It Does🚀 Why It Helps
SVG LintValidates SVG + best practicesAvoids errors
Path IntellisenseAutocompletes file pathsFewer typos
Auto Rename TagAuto-renames closing tagsClean markup
PrettierFormats SVG nicelyConsistency

⌨️ Shortcut: Ctrl+Shift+PFormat Document → Prettier.

7️⃣ Common Pitfalls 🪤

❌ Problem✅ Fix
XML Parsing ErrorEnsure all tags close properly.
Broken PreviewAdd xmlns="http://www.w3.org/2000/svg".
No Color VisibleAdd fill="currentColor" or a solid fill.
Doesn’t ScaleDefine a viewBox="0 0 w h".

8️⃣ Wrap-Up

Creating SVGs by hand in VS Code is easier than you think:

  1. 📝 Create *.svg
  2. ⚡ Use Emmet for skeleton
  3. ✍️ Add paths
  4. 👀 Preview & iterate

With a few extensions, your workflow becomes smooth and fun. So grab a ☕, open VS Code, and craft crisp icons by hand.