✨ Creating an SVG File in VS Code from Text
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
Open VS Code
Open the folder you want to store your SVG in.Create a file
File ➜ New File → save asicon.svg
.
VS Code auto-detects.svg
and enables SVG syntax.
2️⃣ Quick Emmet Abbreviation
VS Code ships with Emmet (shorthand → full markup).
svg:root
Type svg:root
and press Tab to expand:
<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
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:
<!--
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.
🔌 Recommended: SVG Viewer
- 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 Lint | Validates SVG + best practices | Avoids errors |
Path Intellisense | Autocompletes file paths | Fewer typos |
Auto Rename Tag | Auto-renames closing tags | Clean markup |
Prettier | Formats SVG nicely | Consistency |
⌨️ Shortcut:
Ctrl+Shift+P
→ Format Document → Prettier.
7️⃣ Common Pitfalls 🪤
❌ Problem | ✅ Fix |
---|---|
XML Parsing Error | Ensure all tags close properly. |
Broken Preview | Add xmlns="http://www.w3.org/2000/svg" . |
No Color Visible | Add fill="currentColor" or a solid fill. |
Doesn’t Scale | Define a viewBox="0 0 w h" . |
8️⃣ Wrap-Up
Creating SVGs by hand in VS Code is easier than you think:
- 📝 Create
*.svg
- ⚡ Use Emmet for skeleton
- ✍️ Add paths
- 👀 Preview & iterate
With a few extensions, your workflow becomes smooth and fun. So grab a ☕, open VS Code, and craft crisp icons by hand.