SVG
Typography per SVG
InkScape (PortableApps)
Save as ...
> Optimized SVG (*.svg)
Minify SVG file : svgo
npx svgo SOURCE.svg # Overwrites source
npx svgo SOURCE.svg -o OUT.svg
HTML/CSS
Self closing tags such as path
, polygon
, etc. do not validate as HTML5; <path ... />
Some modern browsers fix such invalid tags on-the-fly; some don't.
Reading the HTML5 specification, which is the typical IT hellscape of useless verbosity, one would reasonably conclude that self-closing path
tags are valid. It explicitly references the "SVG Namespace" tags as "Foreign Elements" and those listed here that don't validate are also "Void Elements". And those are the two categories specified as valid if self-closing. But no, apparently.
We'll have to wait another half-century before IT specs are formatted as key-value pairs, as engineers figured out centuries ago. (Actual engineers, not "IT engineer"s.)
Convert to valid …
<path ...></path>
viewBox
: Modify Alignment/Position/Size
tl;dr
Normalize to Zero-offest Viewbox (viewBox="0 0 w h"
), assuring dynamic resize without clipping.
Normalize @ Ai …
File > Export > Export for Screens
> Select: All
SVG viewBox
attribute sets the render boundary and placement therein.
Adobe Illustrator (Ai) will add/modify it as needed per processing …
- Crop:
Object > Artboards > Fit to Artwork Bounds
- Transform:
Object > Transform >
Move
, Rotate
, Scale
, …
- Operates on selected object(s) only; use mouse.
While sizing and placement are overridden per CSS, it is useful to scale an SVG to, say, 1000px
or more, for fine-grained control/manipulation (coordinates), and so that any subsequent bitmap image rendering (e.g., PNG) will be of high quality. Though Ai also offers scaled bitmap image conversion/export too.
Style per CSS. For image attributes per se (fill
, height
, width
, …) style the "<svg>
" element itself; perhaps even inlining the dimensions, if static and otherwise apropos, just to be safe. Conversely, style its container regarding CSS positioning, display
, and such box-model properties.
@ viewBox="x-min y-min width height"
Note that viewBox
parameters do not change the graphic (size or proportions) itself, but only that (and positioning) relative to the view; to the bounds (box) of the view.
- Open in Ai
- Scale (set its native size;
~ 1000px
):
Objects > Transform > Scale
(%
is the only option).
- Reset
viewBox
:
Objects > Artboards > Fit to Artwork bounds
.
- Export as SVG:
File > Export > Export for Screens
.
- If
Select: Full Document
, then any relative positioning information is lost. It saves as original filename, or as "Untitled-1.svg
" if new.
- If
Select: All
, then positioning and size relative to viewBox
is preserved and normalized. It saves as "Artboard 1.svg
".
- At this point in the process, either selection is okay since we just reset the
viewBox
, so there is no relative positioning information to preserve.
- Open in text editor, and manually adjust
viewBox
parameters to modify the location and size of the graphic relative to the view box, adjust width
and height
. Adjust the offsets (x-min
, y-min
) to re-center it.
- E.g., if the graphic fills the viewbox "
0 0 500 500
" edge to edge, then "-15 -15 530 530
" inserts 15px
of emptiness around it, keeping it centered.
- Do not modify the Artboard (@ Ai) after doing so, else (all changes to) relative positioning will be lost.
- Open in Ai, and again save:
File > Export > Export for Screens > Select: All
. (Not Full Document
.)
- Saving the Artboard in this manner is a powerful option/tool in that it normalizes all paths such that
viewBox
offsets are reset to zero, while preserving relative scale(s) and position(s) of all object(s) within the viewBox
.
- Delete all cruft from the SVG file. It's okay to delete grouping elements (
g
), and the id
and class
attributes/values too.
SVG-Font Glyph to SVG
SVG-font glyphs do not render in browsers if used directly in an HTML svg
element. They must first be converted from glyph
to path
, and then normalized, all per SVG specs:
- Manually, or by script, map the SVG-font
glyph
to path
, which is as simple as stripping out everything but for the d=...
element, and swapping tags; "<glyph>
" to "<path>
". The "d=...
" values-string is preserved (unchanged). See the example below.
- Crop @ Ai:
Object > Artboards > Fit to Artwork Bounds
- Transform @ Ai:
Object > Transform > Move
, Rotate
, Scale
, …
- Operates on selected object(s) only; use mouse.
- Export @ Ai:
File > Export > Export for Screens > Full Document
- Optionally, additional exports of various (scaled) bitmap image file(s), e.g., PNG, are available here.
- Manually normalize by adding
transform
to path; scale
(vertical flip) and translate
(to re-center). This is because an SVG-font glyph
is upside down relative to an SVG path
.
- Manually delete unnecessary
svg
attributes/values (id
, title
, …).
Example: "웃
"
Unicode glyph 'HANGUL SYLLABLE US'
(U+C6C3
) is plucked from Malgun Gothic (malgun.ttf
), which is a Hangul Symbol font. The TTF is first converted (online) to SVG font (glyph
), and then (per Ai) to SVG (path
).
@ SVG font (glyph
)
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<glyph
glyph-name="uniC6C3"
unicode="웃"
vert-adv-y="2176"
d="M1024 1063q-298 0 -456 92.5t-158 ... -295.5t475.5 -158.5z" />
</svg>
@ SVG (path
)
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1500 1500">
<path
d="M1024 1063q-298 0 -456 92.5t-158 ... -295.5t475.5 -158.5z" />
</svg>
@ Transformed SVG (path
), after cropped to artboard and exported per Ai.
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1790 1886">
<path
transform="scale(1, -1) translate(0, -1886)"
d="M895,1200q-298,0-456... ,206.5-295.5T1651,127Z"/>
</svg>
PNG to SVG
@ Adobe Illustrator (Ai)
Convert from bitmap to vector graphic:
Window > Image Trace
Crop background (Artboard
) to image (Object
):
Object > Artboards > Fit to Selected Art
Save as SVG file:
File > Export > Export for Screens
Unicode Glyph per SVG file
@ fileformat.info
- Find and select the Unicode glyph
- Save the Unicode glyph as SVG file
- The hypertext link @ "Outline (as SVG file)".
@ Adobe Illustrator (Ai)
@ Ai Menu
Crop & add viewport
@ Object
menu:
> Artboards
> Fit to Artwork Bounds
Save as SVG @ File
menu:
> Export > Export for Screens...
> Full Document
> Export Artboard
Note on save as Select: Full Document
vs Select: All
; the latter (All
) preserves positioning information relative to viewBox
, and saves as Artboard 1.svg
.
@ Text Editor, open the SVG file:
- Remove all the cruft;
id
, data-name
, defs
, style
, title
, class
.
- Keep only
xmlns
, viewbox
and path
.
Original
<svg
id="Layer_1"
data-name="Layer 1"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 220 185.1"
>
<defs>
<style>
.cls-1{stroke:#000;}
</style>
</defs>
<title>FNAME</title>
<path class="cls-1" d="..."/>
</svg>
Final
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 220 185.1">
<path d="..."/>
</svg>
@ HTML
Supported @ img
and svg
tags.
<svg class="svgtest">...</svg>
<img class="svgtest" src="images/svg/tfh.svg" alt="TRIGRAM FOR HEAVEN">
@ CSS
SVG file path is relative to css file.
svg,
.svgtest svg {
background-image: url(../images/svg/tfh.svg);
width: 48px;
height: 48px;
}
Use currentColor
, a CSS keyword, to inherit color from parent.
button svg {
fill: currentColor;
}
Useable @ CSS pseudo-class(es)
button svg:hover {
fill: var(--color-anchor-link);
}
Add Dropshadow (properly)
A common method for adding dropshadow to an SVG graphic
is by embedding a graphics file of the desired effect, and applying it as an overlay.
However, the resulting SVG file is huge (100x).
Instead, embed CSS in the SVG to add the effect
as just another vector-graphics object:
<svg version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<style>
.dropshadow-1 {
filter: drop-shadow(7px 7px 6px rgb(0 0 0 / 0.3));
}
</style>
</defs>
<g class="dropshadow-1">
...
</g>
</svg>
Save as ...
> Optimized SVG (*.svg)
svgo
npx svgo SOURCE.svg # Overwrites source
npx svgo SOURCE.svg -o OUT.svg
path
, polygon
, etc. do not validate as HTML5; <path ... />
Reading the HTML5 specification, which is the typical IT hellscape of useless verbosity, one would reasonably conclude that self-closing path
tags are valid. It explicitly references the "SVG Namespace" tags as "Foreign Elements" and those listed here that don't validate are also "Void Elements". And those are the two categories specified as valid if self-closing. But no, apparently.
We'll have to wait another half-century before IT specs are formatted as key-value pairs, as engineers figured out centuries ago. (Actual engineers, not "IT engineer"s.)
<path ...></path>
viewBox
: Modify Alignment/Position/Sizetl;dr
Normalize to Zero-offest Viewbox (viewBox="0 0 w h"
), assuring dynamic resize without clipping.
File > Export > Export for Screens
> Select: All
viewBox
attribute sets the render boundary and placement therein.Object > Artboards > Fit to Artwork Bounds
Object > Transform >
Move
,Rotate
,Scale
, …- Operates on selected object(s) only; use mouse.
1000px
or more, for fine-grained control/manipulation (coordinates), and so that any subsequent bitmap image rendering (e.g., PNG) will be of high quality. Though Ai also offers scaled bitmap image conversion/export too.fill
, height
, width
, …) style the "<svg>
" element itself; perhaps even inlining the dimensions, if static and otherwise apropos, just to be safe. Conversely, style its container regarding CSS positioning, display
, and such box-model properties.viewBox="x-min y-min width height"
Note that viewBox
parameters do not change the graphic (size or proportions) itself, but only that (and positioning) relative to the view; to the bounds (box) of the view.
- Scale (set its native size;
~ 1000px
):
Objects > Transform > Scale
(%
is the only option).
- Reset
viewBox
:Objects > Artboards > Fit to Artwork bounds
.
- Export as SVG:
File > Export > Export for Screens
.- If
Select: Full Document
, then any relative positioning information is lost. It saves as original filename, or as "Untitled-1.svg
" if new. - If
Select: All
, then positioning and size relative toviewBox
is preserved and normalized. It saves as "Artboard 1.svg
". - At this point in the process, either selection is okay since we just reset the
viewBox
, so there is no relative positioning information to preserve.
- If
viewBox
parameters to modify the location and size of the graphic relative to the view box, adjust width
and height
. Adjust the offsets (x-min
, y-min
) to re-center it.- E.g., if the graphic fills the viewbox "
0 0 500 500
" edge to edge, then "-15 -15 530 530
" inserts15px
of emptiness around it, keeping it centered. - Do not modify the Artboard (@ Ai) after doing so, else (all changes to) relative positioning will be lost.
File > Export > Export for Screens > Select: All
. (NotFull Document
.)- Saving the Artboard in this manner is a powerful option/tool in that it normalizes all paths such that
viewBox
offsets are reset to zero, while preserving relative scale(s) and position(s) of all object(s) within theviewBox
.
g
), and the id
and class
attributes/values too.svg
element. They must first be converted from glyph
to path
, and then normalized, all per SVG specs:glyph
to path
, which is as simple as stripping out everything but for the d=...
element, and swapping tags; "<glyph>
" to "<path>
". The "d=...
" values-string is preserved (unchanged). See the example below.Object > Artboards > Fit to Artwork Bounds
Object > Transform > Move
,Rotate
,Scale
, …- Operates on selected object(s) only; use mouse.
File > Export > Export for Screens > Full Document
- Optionally, additional exports of various (scaled) bitmap image file(s), e.g., PNG, are available here.
transform
to path; scale
(vertical flip) and translate
(to re-center). This is because an SVG-font glyph
is upside down relative to an SVG path
.svg
attributes/values (id
, title
, …).웃
"'HANGUL SYLLABLE US'
(U+C6C3
) is plucked from Malgun Gothic (malgun.ttf
), which is a Hangul Symbol font. The TTF is first converted (online) to SVG font (glyph
), and then (per Ai) to SVG (path
).glyph
)<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<glyph
glyph-name="uniC6C3"
unicode="웃"
vert-adv-y="2176"
d="M1024 1063q-298 0 -456 92.5t-158 ... -295.5t475.5 -158.5z" />
</svg>
path
)<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1500 1500">
<path
d="M1024 1063q-298 0 -456 92.5t-158 ... -295.5t475.5 -158.5z" />
</svg>
path
), after cropped to artboard and exported per Ai.<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1790 1886">
<path
transform="scale(1, -1) translate(0, -1886)"
d="M895,1200q-298,0-456... ,206.5-295.5T1651,127Z"/>
</svg>
Convert from bitmap to vector graphic:
Window > Image Trace
Crop background (Artboard
) to image (Object
):
Object > Artboards > Fit to Selected Art
Save as SVG file:
File > Export > Export for Screens
fileformat.info
- The hypertext link @ "Outline (as SVG file)".
@ Ai Menu
Crop & add
viewport
@Object
menu:
> Artboards
> Fit to Artwork Bounds
Save as SVG @
File
menu:
> Export > Export for Screens...
> Full Document
> Export Artboard
Select: Full Document
vs Select: All
; the latter (All
) preserves positioning information relative to viewBox
, and saves as Artboard 1.svg
.id
, data-name
, defs
, style
, title
, class
.xmlns
, viewbox
and path
.<svg
id="Layer_1"
data-name="Layer 1"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 220 185.1"
>
<defs>
<style>
.cls-1{stroke:#000;}
</style>
</defs>
<title>FNAME</title>
<path class="cls-1" d="..."/>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 220 185.1">
<path d="..."/>
</svg>
Supported @ img
and svg
tags.
<svg class="svgtest">...</svg>
<img class="svgtest" src="images/svg/tfh.svg" alt="TRIGRAM FOR HEAVEN">
SVG file path is relative to css file.
svg,
.svgtest svg {
background-image: url(../images/svg/tfh.svg);
width: 48px;
height: 48px;
}
Use currentColor
, a CSS keyword, to inherit color from parent.
button svg {
fill: currentColor;
}
Useable @ CSS pseudo-class(es)
button svg:hover {
fill: var(--color-anchor-link);
}
A common method for adding dropshadow to an SVG graphic is by embedding a graphics file of the desired effect, and applying it as an overlay. However, the resulting SVG file is huge (100x).
as just another vector-graphics object:
<svg version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<style>
.dropshadow-1 {
filter: drop-shadow(7px 7px 6px rgb(0 0 0 / 0.3));
}
</style>
</defs>
<g class="dropshadow-1">
...
</g>
</svg>