How it works
An outline that follows the alpha channel
CSS borders follow the box, not the picture. An SVG filter can read an element's alpha channel instead, grow it, paint it, and put the original back on top: a die-cut sticker edge around any transparent image, icon or line of text.
1. Grow the shape: feMorphology dilate
SourceAlpha is the element's rendering with every pixel turned black and only
its alpha kept: the silhouette. <feMorphology operator="dilate"> replaces
every pixel with the maximum of its neighbourhood, a rectangle radius pixels to each
side. On alpha that grows the silhouette by the radius in every direction; transparent gaps narrower
than twice the radius close up, which is why the loose stars around the rocket merge into one
sticker once the outline gets wide enough.
operator="erode" does the opposite (minimum instead of maximum) and shrinks the shape;
subtracting an eroded silhouette from the original would give an inner line instead.
2. Round the corners: blur and threshold
Because the neighbourhood is a rectangle, a dilated dot becomes a square and every corner of
the outline is square, which looks wrong on round artwork. Roundness blurs the
dilated alpha with <feGaussianBlur> and then thresholds it again at 50 %
with an <feColorMatrix> (alpha × 10 − 4.5, the same trick the gooey effect uses). Along a straight edge
the blurred alpha crosses 50 % exactly where the edge was, so straight parts stay put. At a
convex corner less than half the blur kernel is covered, so the corner is pulled in to a
curve about as wide as the blur; notches fill in the same way.
3. Color, ring and shadow: feFlood, feComposite, feMerge
<feFlood> fills the filter region with one color, and <feComposite operator="in"> keeps it only where the grown alpha is: the
outline. The keyline ring is the same three steps with a radius of outline plus ring width,
drawn underneath. The shadow blurs and offsets the outermost silhouette, so the sticker
casts it as a whole. <feMerge> stacks shadow, ring, outline and finally SourceGraphic, the untouched original, on top.
<svg width="0" height="0" aria-hidden="true">
<filter id="outline" x="0" y="0" width="100%" height="100%"
color-interpolation-filters="sRGB">
<!-- the outline: grow the alpha by 7px -->
<feMorphology in="SourceAlpha" operator="dilate" radius="7"
result="stroke-square" />
<!-- round the square corners: blur, then threshold alpha at 50 % -->
<feGaussianBlur in="stroke-square" stdDeviation="2.1" />
<feColorMatrix type="matrix" result="stroke-alpha"
values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 10 -4.5" />
<feFlood flood-color="#ffffff" />
<feComposite in2="stroke-alpha" operator="in" result="stroke" />
<!-- the keyline ring: grow the alpha by 9.5px -->
<feMorphology in="SourceAlpha" operator="dilate" radius="9.5"
result="ring-square" />
<!-- round the square corners: blur, then threshold alpha at 50 % -->
<feGaussianBlur in="ring-square" stdDeviation="2.85" />
<feColorMatrix type="matrix" result="ring-alpha"
values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 10 -4.5" />
<feFlood flood-color="#120e26" />
<feComposite in2="ring-alpha" operator="in" result="ring" />
<feGaussianBlur in="ring-alpha" stdDeviation="6" />
<feOffset dx="0" dy="6" result="shadow-alpha" />
<feFlood flood-color="#0b0820" flood-opacity="0.4" />
<feComposite in2="shadow-alpha" operator="in" result="shadow" />
<feMerge>
<feMergeNode in="shadow" />
<feMergeNode in="ring" />
<feMergeNode in="stroke" />
<feMergeNode in="SourceGraphic" />
</feMerge>
</filter>
</svg> Room to grow
An outline extends past the element's box, but a filter only paints inside its filter
region. The default region is 10 % larger than the box on each side, which is a lot on a
hero image and almost nothing on a 24px icon. Percentages can't express "box plus 12px", so
the component pads the element by exactly the outline's reach and takes the space back with
a negative margin. The region stays at 0 0 100% 100% of the padded box and the layout
does not move:
.sticker {
/* room for the outline and shadow inside the filter region, layout unchanged */
padding: 36px;
margin: -36px;
filter: url(#outline);
} The filter sees everything the element paints, so its background has to stay transparent; a background color would be outlined as one big rectangle. Text inside keeps working as text: it is still selectable and read by screen readers, only its rendering is filtered.
Browser support and performance
SVG filters on HTML, including feMorphology, have been Baseline widely
available since July 2015 (web-features, svg-filters). Checked in Chromium and
Firefox; Safari is untested.
- Dilate cost grows with the radius, since every output pixel looks at a (2r + 1)² neighbourhood (browsers can split it into a horizontal and a vertical pass, but it still grows). Keep outlines to a few pixels on large elements. With the ring there are two dilations, and roundness adds a blur per ring.
- The radius is in CSS pixels, so the outline keeps its width on high-DPI screens; the browser computes it at device resolution.
- Static content is filtered once and cached. Animating the content, or the outline width, reruns the whole chain every frame.
- Without SVG filter support, a stack of
filter: drop-shadow(0 0 1px color)calls gives a rough, soft-edged outline.