FreeVector

Guide · Vector graphics

How to Edit SVG Files: Tools, Code and Common Pitfalls

You can edit an SVG file in two ways: visually in a vector editor such as Inkscape, Illustrator or Figma, or as code in any text editor, because SVG is an XML text format. Visual editing suits redrawing shapes and changing layout. Code editing suits quick fixes such as changing a color, resizing, removing a hidden layer or cleaning a file before it goes on a website. This guide explains both routes, the few attributes that matter most, and the mistakes that make edited SVGs look wrong.

Designer editing a vector illustration on a laptop at a desk

Key takeaways

  • An SVG is a text file, so a color, size or label change often takes one minute in a code editor.
  • Use a vector editor when you need to move, merge or redraw shapes, and a text editor when you need to change values.
  • The viewBox attribute controls how the drawing scales, so keep it when you resize or optimize a file.
  • How you embed an SVG (inline, img tag or CSS background) decides whether CSS and JavaScript can reach it.
  • Optimize with a tool such as SVGO after editing, then compare the result with the original before you publish.

What is inside an SVG file?

An SVG file is a plain text file written in XML. MDN describes SVG as an XML dialect, and normal SVG files are text files containing SVG markup, usually saved with the .svg extension. You can open one in any text editor and read it, which is the main reason SVG is easier to edit than formats such as PNG or AI.

Inside the root svg element you find shapes (rect, circle, ellipse, line, polygon), path elements for freeform outlines, text, groups (g) and sometimes defs for gradients and reusable parts. Later elements are drawn on top of earlier ones, so the order in the file is the stacking order on screen.

The root element needs the SVG namespace, written as xmlns="http://www.w3.org/2000/svg". MDN states that because SVG is XML, it must always bind the namespaces correctly. A file that loses this attribute can show up as raw text or as a blank image when you open it on its own.

This structure explains most editing tricks. Because every shape is a named element with attributes, you can search for a color value and replace it everywhere in seconds, or delete one element and watch exactly that part of the drawing disappear. Files exported from design tools are usually more cluttered than hand-written ones, with generated IDs, nested groups and metadata, but the same logic applies once you know where to look.

Should you edit an SVG visually or as code?

Pick the route by the kind of change. Changing what the drawing looks like (moving points, combining shapes, redrawing a curve) is easier in a vector editor. Changing a value (a hex color, a width, a title, a class name) is faster in code.

Many people use both: open the file in a vector editor for the drawing work, then open the saved file in a text editor to tidy the output. Our vector graphics software and SVG editors categories list tools for each approach, and the vector vs raster images guide explains why a vector file can be changed without quality loss.

A practical rule is to decide before you open the file what the finished result should look like. If you can describe the change as a value (make it blue, make it 32 pixels wide), go to the code. If you can only describe it as a shape (make the corner rounder, join these two letters), go to the editor.

Mixing the two in the wrong order, for example hand-editing paths and then reopening the file in an editor that rewrites them, is where many accidental changes come from.

  • Use a vector editor to move or reshape objects, trace artwork, combine shapes or draw something new.
  • Use a text editor to swap colors, rename IDs, remove metadata, set a viewBox or add accessibility text.
  • Use code or a script when the same change has to happen across many files.
  • Use an online SVG editor for a one-off edit when you do not want to install anything.

How do you edit an SVG in a vector editor?

Open the file in an editor that reads SVG natively. Inkscape, Illustrator and Affinity Designer open and export it, and Figma imports it as editable layers. Select an object, change its fill, stroke or size, then edit individual anchor points with the node or direct selection tool.

Check what the editor writes back when you save. Some editors add their own metadata, layer names and extra namespaces to the file. If your editor offers a plain SVG or web-optimized export setting, use it for files that will be published, and tidy the result with an optimizer (see below).

Convert text to outlines (paths) before you share a logo or icon that uses a custom font. Live text depends on the font being installed on the viewer's device, so a file that looks right on your machine can fall back to a default font elsewhere. Outlined text keeps its shape but can no longer be edited as words, so keep an editable copy.

How do you edit an SVG in a text editor?

Open the .svg file in a code editor and look for the attribute you want to change. Colors usually live in fill and stroke attributes or in a style block. Sizes live in width, height and viewBox on the root element.

Change the value, save, and refresh the file in a browser to see the result.

Typical edits look like this:

After each change, reload the file in a browser rather than in the editor you wrote it in. Browsers are strict about XML, so they show you problems that a forgiving preview would hide. Make one change at a time, so that when something breaks you know which edit caused it, and use your editor's find-and-replace for repeated values such as a brand color used in many places.

  • Change a color: find fill="#1a73e8" and replace the hex value.
  • Make a shape transparent: use fill="none" for no fill, or fill-opacity for partial transparency.
  • Rename or remove a layer: delete the g element that wraps it, or remove the single element you do not need.
  • Add a title: place a title element as the first child of the svg element.
  • Remove editor leftovers: delete comments, metadata and unused defs blocks.

Keep a backup of the original file before you start. A missing closing tag or a stray quote makes the whole file invalid XML, and browsers may refuse to draw it. If a file stops rendering after an edit, undo the last change first.

What does the viewBox do, and why does it matter?

The viewBox attribute sets the coordinate system of the drawing. MDN explains that it defines the rectangle in user space that is mapped onto the bounds of the SVG element's viewport. It takes four numbers: min-x, min-y, width and height.

A width or height of zero or less disables rendering of the element.

In practice, the viewBox is what lets one file scale to any size. A drawing with viewBox="0 0 100 100" can be shown at 16 pixels or 1,600 pixels wide, and every shape scales with it. If you delete the viewBox and keep fixed width and height values, the picture often stops scaling and can be cropped when you change the size.

The preserveAspectRatio attribute decides what happens when the viewport has a different shape from the viewBox. Its default, xMidYMid meet, scales the drawing uniformly to fit inside the box and centers it. If a logo seems to float in empty space or gets cut off, check the viewBox first and then this attribute.

How do paths work, and when do you edit them by hand?

A path element stores an outline in its d attribute as a list of commands. MDN's path tutorial lists them: M moves to a point, L draws a line, H and V draw horizontal and vertical lines, C and S draw cubic curves, Q and T draw quadratic curves, A draws an arc and Z closes the shape. Uppercase letters use absolute coordinates, and lowercase letters use coordinates relative to the current point.

Most people never write paths by hand, and you should not need to for logo or icon work. Hand edits make sense for small corrections: moving one point by a few units, closing a shape that has a gap, or switching a command from absolute to relative. For anything more complex, use the node tool in a vector editor, which shows the curve handles and prevents arithmetic mistakes.

Arcs have a limit worth knowing. MDN notes that an arc cannot draw a complete circle in a single command because the start and end points coincide. Use a circle or ellipse element instead, or split the shape into two arcs.

How does embedding change what you can edit with CSS?

The way an SVG sits in a page decides how much control you have after publishing. MDN lists several methods: inline in the HTML, an img element, an object element, an iframe, or created with JavaScript and added to the page.

Inline SVG is part of the page's DOM, so page CSS and JavaScript can change its colors and shapes. This is the best choice for icons that need hover effects or dark mode colors. An SVG loaded through an img tag or a CSS background behaves as a self-contained image: the outside page cannot restyle its inner elements, so any color changes must be made in the file itself.

Inline SVG has no alt attribute. MDN's guidance is to add a title element as the first child, optionally a desc element, set role="img" and connect them with aria-labelledby. Decorative graphics should be hidden from assistive technology instead of described.

A simple habit prevents most surprises: decide how the file will be embedded before you finalize its colors. If it will be used inline, you can leave fill values off the shapes and set them with CSS, which makes dark mode and hover states easy. If it will be used as an image, keep every color inside the file, because nothing outside can change it.

How do you optimize an edited SVG?

SVGO is a Node.js command-line tool and library for optimizing SVG files. Its documentation says files from vector editors often contain redundant data, and the tool removes things like editor metadata, comments, hidden elements and default or suboptimal values without changing how the image renders. Install it with npm install -g svgo, then run svgo input.svg -o output.svg, or process a whole folder with the -rf option.

SVGO reads a configuration file (svgo.config.mjs by default) where you can enable the default preset and switch single plugins off. Plugins that clean up IDs or collapse groups can break files that rely on those IDs for CSS or animation, so turn those off for such files. Check the plugin list on the SVGO site before you accept the defaults on a file with a custom viewBox or scripts.

Always compare the optimized file with the original in a browser, side by side, at two or three sizes. Optimization is safe most of the time, but a visual check catches the cases where it is not.

Keep the original, unoptimized file as your source of truth and treat the optimized version as a build output. When you need to edit again, change the source and run the optimizer once more. Editing the minified output by hand is painful, because the readable structure and IDs are gone.

Which mistakes break edited SVG files most often?

Most failures come from a short list of causes. Run through this checklist when an edited file looks wrong.

  • The xmlns attribute is missing from the root svg element, so the file shows as text or not at all.
  • A tag is not closed or an attribute value has no closing quote, which makes the XML invalid.
  • The viewBox was removed or edited so the drawing is cropped or will not scale.
  • A doctype line was added. MDN advises leaving it off because DTD-based validation causes more problems than it solves.
  • Text depends on an installed font and was not converted to outlines.
  • The server sends the wrong content type. MDN says SVG should be served as image/svg+xml, and a misconfigured server is a common reason SVGs fail to load.
  • Optimization removed an ID that a CSS rule or script needed.
  • The file was opened as an img and the CSS written for inline use does nothing.

Treat SVGs from unknown sources with care. An SVG can contain scripts and external references, so open untrusted files in a text editor before you place them inline in a page, and remove script elements you did not expect.

Which tool should you use?

For a quick value change, a plain code editor is enough and costs nothing. For drawing and reshaping, choose a vector editor from our vector graphics software list, and for browser-based edits look at the SVG editors list. If your source is a PNG or JPG rather than an SVG, convert it first with a tracing tool, as described in how to convert an image to vector.

If you need to pick between SVG and other vector formats for delivery, read SVG vs EPS vs AI vs PDF. For logo files in particular, how to get a logo in vector format covers the legal ways to obtain or recreate one.

Frequently asked questions

Can I edit an SVG file in Notepad or TextEdit?

Yes. An SVG is a text file with XML markup, so any text editor can open it. Use a code editor if you can, because it highlights tags and catches unclosed elements.

Save as plain text with the .svg extension and UTF-8 encoding, then open the file in a browser to check the result.

How do I change the color of an SVG?

Find the fill or stroke attribute on the shape and replace its color value. If the SVG is inline in a web page, you can also change the color with CSS. If it loads through an img tag, CSS from the page cannot reach inside it, so edit the file itself or use a version with the color you need.

Why does my SVG look cropped after editing?

The viewBox or the width and height no longer match the artwork. Set the viewBox so it covers the full drawing, for example 0 0 width height of your artboard, and let preserveAspectRatio use its default. Also check that no shape sits outside the visible area because of a transform.

Is it safe to open SVG files from the internet?

Usually, but not always. SVG is a document format that can contain scripts and links to other resources. Browsers limit what an SVG can do when it loads as an image, but inline use gives it more reach.

Inspect unknown files in a text editor and remove script elements before using them in a page.

What is the best free SVG editor?

Inkscape is a widely used free desktop vector editor that works with SVG natively. Browser-based editors suit small jobs, and a code editor handles value changes. The best choice depends on whether you redraw shapes or only adjust values, so compare options in our SVG editors category.

Should I convert text to outlines before exporting?

Yes for logos and icons you hand to others, because live text depends on the font being installed on the viewer's device. Outlined text keeps its exact shape everywhere. Keep a separate editable version with live text, since outlined letters cannot be edited as words later.

Sources and further reading