I was asked recently how to debug inline SVG. Because it is part of the DOM, we can check any inline SVG in DevTools in any browser. Because of this, we were able to scale things and spot any potential issues or opportunities for SVG optimization.
But sometimes, we can't even see our SVG at all. In these cases, I look for six specific aspects when debugging.
When using SVG, viewBox is a common point of confusion. Technically, it's OK to use inline SVG without a viewBox, but we lose one of its most important advantages: scaling with containers. At the same time, if configured improperly, it may be detrimental to us, resulting in unexpected cropping.
When elements are cropped, they are there - they are just part of the coordinate system we can't see. If we open the file in some graphic editing program, it might look like this:
What is the easiest solution? Add overflow="visible"
to SVG, whether in a style sheet, in an inline style attribute, or directly as an SVG to represent properties. However, if we also apply background colors to SVG, or have other elements around it, things may look a little bit wrong. In this case, the best option is to edit the viewBox to show the hidden coordinate system part:
About viewBox, there are some extra things worth discussing:
SVG is an infinite canvas, but we can control what we see and how we see it through the viewport and viewBox.
Viewport is the window frame on the infinite canvas. Its dimensions are defined by the width
and height
properties, or are defined using the corresponding width
and height
properties in CSS. We can specify any unit of length we want, but if we provide unitless numbers, they default to pixels.
viewBox is defined by four values. The first two are the starting points of the upper left corner (x and y values, negative numbers are allowed). I'm editing these to recompose. The last two are the width and height of the coordinate system in the viewport - here we can edit the scale of the grid (we will discuss in the Scaling section).
The following are simplified markers showing the SVG viewBox and <svg></svg>
and width
properties set on height
:
<svg height="700" viewbox="0 0 700 700" width="700"></svg>
So, this:
<svg viewbox="0 0 700 700"></svg>
…map to this:
<svg viewbox="start-x-axis start-y-axis width height"></svg>
The viewport we see begins where 0 on the x-axis and 0 on the y-axis intersect.
Change this content:
<svg viewbox="0 0 700 700"></svg>
…To this point:
<svg viewbox="300 200 700 700"></svg>
…Width and height remain the same (700 units each), but the starting point of the coordinate system is now at 300 points on the x-axis and 200 points on the y-axis.
In the video below, I added a red <circle></circle>
to the SVG, with the center at 300 points on the x-axis and 200 points on the y-axis. Note that changing the viewBox coordinates to the same value will also change the placement of the circle to the upper left corner of the frame, while the rendering size of the SVG remains the same (700×700). All I did was "recompose" with viewBox.
We can change the last two values in the viewBox to zoom in or out of the image. The larger the value, the more SVG units added to the viewport, resulting in smaller images. If we want to maintain a 1:1 ratio, our viewBox width and height must match our viewport width and height values.
Let's see what happens when changing these parameters in Illustrator. The artboard is a viewport, represented by a 700-pixel white square. Everything outside of this area is our unlimited SVG canvas and will be cropped by default.
The following figure 1 shows a blue point located at 900 on the x-axis and 900 on the y-axis. If I change the last two viewBox values from 700 to 900, as shown below:
<svg height="700" viewbox="0 0 700 700" width="700"></svg>
…Then the blue dots are almost completely back to view, as shown in Figure 2 below. Our image is reduced because we increase the viewBox value, but the actual width and height dimensions of the SVG remain the same, and the blue dot returns closer to the uncropped area.
There is a pink square as evidence of how the mesh scales to fit the viewport: the units get smaller, and more mesh lines fit the same viewport area. You can use the same value in the Pen below to see how it works:
Another common question I look at when debugging an inline SVG is whether the width
or height
attributes are included in the tag. In many cases, this is no big deal unless the SVG is located inside a container with absolute positioning or flexible containers (because Safari uses 0px instead of auto to calculate the SVG width value). In these cases, excluding width or height prevents us from seeing the full image, as we saw by opening this CodePen demo and comparing it in Chrome, Safari, and Firefox (clicking on the image to see a larger view).
Solution? Add width or height, whether as a representation property, inline style property, or in CSS. Avoid using heights alone, especially if set to 100%
or auto
. Another solution is to set the right
and left
values.
You can try it with the following Pen and combine different options.
It may also be that we are applying colors to the <svg></svg>
tags, whether inline style or from CSS. This is good, but there may be other color values in the tag or style that conflict with the color set on <svg></svg>
, causing some of the content to be invisible.
This is why I tend to look up the fill
and stroke
properties in the tags of SVG and delete them. The following video shows an SVG that I styled with red fill in CSS. In SVG there are some places where the padding is filled directly in white in the marker, which I removed to show the missing part.
This may seem very obvious, but you'll be surprised to see how often I see it happening. Suppose we made an SVG file in Illustrator and worked very hard to name the layers so we could get a nice matching ID when exporting the file. Suppose we plan to style the SVG in CSS by connecting to these IDs.
This is a good way to do it. However, there are many times when I see the same SVG file exported to the same location the second time and the ID is different, usually when copying/pasting the vector directly. Maybe a new layer was added, or one of the existing layers was renamed, or something else. In any case, the CSS rules no longer match the ID in the SVG tag, causing the SVG to render differently than expected.
We may have difficulty finding these IDs in large SVG files. This is a good time to open DevTools, check for the graph sections that don't work, and see if those IDs still match.
So I would say it's worth opening the export SVG file in the code editor and comparing it to the original file before exchanging the content. Applications like Illustrator, Figma, and Sketch are smart, but that doesn't mean we have no responsibility to review them.
If the SVG is accidentally cropped and the viewBox checks correctly, I usually look at the clip-path
or mask
properties in CSS that may interfere with the image. It's tempting to see inline markers, but it's best to remember that SVG's styles may happen elsewhere.
CSS cropping and masking allow us to "hide" parts of an image or element. In SVG, <clippath></clippath>
is a vector operation that crops parts of the image without intermediate results. <mask></mask>
A label is a pixel operation that allows for transparency, translucent effects and blurred edges.
Here is a small checklist for debugging situations involving cropping and masking:
<clippath></clippath>
or <mask></mask>
are not rendered, you can still use DevTools to check internal code, so use it! <clippath></clippath>
and <mask></mask>
and paste them before closing the tag. Then add padding to these shapes and check the coordinates and dimensions of the SVG. If you still don't see the image, try adding <svg></svg>
to the overflow="hidden"
tag. <clippath></clippath>
or <mask></mask>
The unique ID is used and whether the same ID is applied to the shape or shape group that is being cropped or masked. A mismatched ID can ruin the appearance.
<clippath></clippath>
. <mask></mask>
<clippath></clippath>
, fill
, stroke
, or some other style that is applied to elements within opacity
is useless—the only useful part is the fill area geometry of these elements. That's why if you use <polyline></polyline>
it will behave like <polygon></polygon>
, and if you use <line></line>
you won't see any cropping effect. <mask></mask>
, make sure the mask content is not completely black. The brightness of the mask element determines the opacity of the final figure. You will be able to see through the brighter parts, while the darker parts hide the contents of the image. You can practice using masks and crop elements in this Pen.
Did you know that SVG is an XML-based markup language? Yes, it is! The namespace of SVG is set on the xmlns
property:
<svg height="700" viewbox="0 0 700 700" width="700"></svg>
There is a lot to know about namespaces in XML, and there is a good introduction to the beginning on MDN. It can be said that the namespace provides the browser with context telling it to be tagged with SVG specific. The idea is that when there are multiple types of XML in the same file (such as SVG and XHTML), namespaces help prevent conflicts. This is a less common problem in modern browsers, but can help explain the strict SVG rendering issues for older browsers or browsers like Gecko when defining document types and namespaces.
The SVG 2 specification does not require a namespace when using HTML syntax. But if supporting legacy browsers is a priority, that's crucial—and, adding it doesn't cause any damage. This way, when defining the xmlns
attribute of the element, there will be no conflict in those rare cases.
<svg height="700" viewbox="0 0 700 700" width="700"></svg>
Same is true when using inline SVG in CSS (for example, setting it as a background image). In the following example, after successful verification, the check mark icon appears in the input box. The appearance of CSS is as follows:
<svg viewbox="0 0 700 700"></svg>
When we delete the namespace inside the SVG in the background property, the image disappears:
Another common namespace prefix is xlink:href
. We often use it when referencing other parts of SVG, such as patterns, filters, animations, or gradients. It is recommended to start replacing it with href
, as the other has been deprecated since SVG 2, but may have compatibility issues with older browsers. In this case, we can use both. Remember, if you are still using xlink:href
, include the namespace xmlns:xlink="http://www.w3.org/1999/xlink"
.
If you find yourself troubleshooting an incorrectly rendered inline SVG, I hope these tips can help you save a lot of time. These are just the things I'm looking for. Maybe you have different red flags to pay attention to – if so, let me know in the comments!
Most importantly, it is worthwhile to have a basic understanding of the various ways to use SVG. CodePen Challenges often include SVG and provide good practice. Here are some more resources to improve your level:
I suggest paying attention to some excellent people related to SVG:
The above is the detailed content of 6 Common SVG Fails (and How to Fix Them). For more information, please follow other related articles on the PHP Chinese website!