How to Use an SVG Shape as a Background Image in CSS
Use an SVG shape as a background image in CSS. Covers sizing, stretching, placement, data URIs, changing color with masks and fixes for common problems.
MintConvert Team
"A curve under a header or a soft shape behind a heading can lift a plain layout. With SVG, it stays sharp at any width. The tricky part is getting it to sit and stretch the way you intended.
Step 1: Prepare the shape
Save this as curve.svg:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 20" preserveAspectRatio="none">
<path d="M0 10 C25 20 75 0 100 10 V20 H0 Z" fill="#10b981" />
</svg>You can also create a starting shape with the SVG maker, or pick a dedicated blob tool or wave tool. Our guide on how to make an SVG file covers other methods.
Step 2: Add it with CSS
.hero {
min-height: 24rem;
padding-bottom: 5rem;
background-image: url("/shapes/curve.svg");
background-repeat: no-repeat;
background-position: center bottom;
background-size: 100% 5rem;
}The element needs its own size, from content, padding or a height. A background image doesn't make an empty box grow.
Step 3: Choose how it scales
| Goal | background-size | Result |
|---|---|---|
| Show the whole shape | contain | Keeps proportions; may leave space |
| Fill the area | cover | Keeps proportions; may crop |
| Stretch a divider across | 100% 5rem | Fixed height, full width |
| Repeat a small shape | 40px 40px | Fixed tile size |
To stretch a shape to different proportions, include preserveAspectRatio="none" in the SVG file. MDN explains the sizing rules for SVG backgrounds. Stretching suits curves, but it will distort circles and logos.
For a repeating design, the edges of the tile need to match. A pattern tool can build tiles for you.
Layer shapes over a color
.card {
background-color: #f0fdf4;
background-image: url("/shapes/corner.svg"), url("/shapes/curve.svg");
background-repeat: no-repeat, no-repeat;
background-position: top right, bottom center;
background-size: 8rem auto, 100% 4rem;
}Replace corner.svg with your second shape. The first image in the list is the top layer.
Inline the shape with a data URI
A very small shape can live in your stylesheet, which avoids a separate request:
.dot {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10'%3E%3Ccircle cx='5' cy='5' r='4' fill='%2310b981'/%3E%3C/svg%3E");
}Keep xmlns, and write # in colors as %23. An SVG to Base64 tool can prepare the encoded string. Separate files are easier to edit and can be cached on their own.
Change the color from CSS
Page styles such as fill don't reach inside a background SVG. For a one-color shape, use it as a mask:
.shape {
height: 5rem;
background-color: rebeccapurple;
-webkit-mask: url("/shapes/curve.svg") center / 100% 100% no-repeat;
mask: url("/shapes/curve.svg") center / 100% 100% no-repeat;
}A mask hides parts of the whole element, including its text. Use it on an empty decorative element or pseudo-element, and test in the browsers you support.
Troubleshooting
| Problem | What to check |
|---|---|
| Nothing appears | The file path, and that the element has a width and height |
| Shape won't stretch | preserveAspectRatio="none" in the SVG |
| Data URI is blank | Missing xmlns, an unencoded # or clashing quotes |
| External picture or font is missing | SVG used as an image can't load external resources |
| Thin gap below a divider | Check spacing and the bottom edge of the path |
Key takeaways
Give the box a size, set repeat, position and size deliberately, and add preserveAspectRatio="none" only when distortion is acceptable. Use a mask when CSS needs to control a one-color shape.
Frequently asked questions
More articles
SVG Documents Explained: Structure, Elements and Examples
How SVG documents are structured: the root element, namespace, viewBox, groups, definitions, reuse, drawing order and the errors that stop a file rendering.
SVG Files for Cricut: Free Downloads, Licenses and Uploading
Using SVG files for Cricut? Learn where to get free downloads, how to check the license, how to upload to Design Space and how to fix files that look wrong.
SVG Image Element: How to Add Pictures Inside an SVG
Use the SVG image element to place PNG, JPG or SVG pictures inside a graphic. Covers href, sizing, cropping, shaped clips, embedding and missing images.