Scaling SVG clip paths.
Aug 28, 2023
2 minutes
I had to code a design that was like half a pie chart with text in the slices and the center of the chart was cut out in a circle like a donut. You can see it here on Squad by Mamamia. The solution I went with was to use SVGs as a clip path to make the background shapes.
I had to make a couple of modifications to the SVG code that I embedded.
- Wrap the
<path>
in a<clipPath>
element and give it an#id
so it can referenced from the CSSclip-path: url(#id)
- Add the attribute
clipPathUnits="objectBoundingBox"
- Add a
transform="scale(1/svg-width, 1/svg-height)"
attribute to the<path>
. - Set the
width
andheight
attribute of the SVG to0
Next It’s time to do the CSS part of this.
- Add the CSS declarations to make the
clipPath
visible. - On the parent container add a fluid typography
font-size
- The code below has a fallback for browsers that don’t support
clamp()
- The code below has a fallback for browsers that don’t support
- Use
em
to size and position the shapes so that it can scale.
Here’s what the finished example looks like. If you open the CodePen or resize your browser the green shape will scale.