An SVG background pattern is a small SVG tile that the browser repeats across an element. You write or generate the tile, point background-image at it (as a file or an inline data URI), and set background-size to the tile's size. For simple dots, stripes and grids you can skip SVG entirely, because CSS gradients draw them directly.
Which method to use comes down to the shape. Use CSS gradients for plain geometry, write the SVG tile by hand when it has to repeat perfectly, and use a generator when you want waves or blobs without editing coordinates.
Why use SVG for background patterns
- Sharp at any size. SVG is vector, so the pattern stays crisp on high-density screens and when the page is zoomed.
- Small. A tile is often a few hundred bytes of text, and text compresses well when your server uses gzip or Brotli.
- Editable. Colors, spacing and stroke widths are plain attributes you can change in any text editor.
- Shapes CSS can't draw easily. Waves, curves and organic blobs are awkward or impossible with gradients alone.
The trade-off: an SVG used as a background image can't read your page's CSS, so you can't restyle it with classes or currentColor. The colors section below shows how to work around that.
Option 1: Draw dots, stripes and grids with CSS gradients
If your pattern is made of dots, straight stripes or grid lines, a CSS background pattern built from gradients is often all you need. There is no extra file and nothing to encode.
Dots:
.dots {
background-color: #f8fafc;
background-image: radial-gradient(#94a3b8 1.5px, transparent 1.5px);
background-size: 20px 20px;
}
Diagonal stripes:
.stripes {
background: repeating-linear-gradient(45deg, #f1f5f9 0 10px, #e2e8f0 10px 20px);
}
Grid lines:
.grid {
background-color: #ffffff;
background-image:
linear-gradient(#e2e8f0 1px, transparent 1px),
linear-gradient(90deg, #e2e8f0 1px, transparent 1px);
background-size: 24px 24px;
}
What gradients can't do well: curves, waves, blobs and anything with more than a few shapes per tile. Hard-edged gradient dots can also look slightly jagged at small sizes. That's when SVG is the better choice.
Option 2: Write a small SVG tile by hand
A pattern tile is just an SVG with a fixed width and height. This one is a 20×20 tile with a single dot in the middle:
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20">
<circle cx="10" cy="10" r="1.5" fill="#94a3b8"/>
</svg>
Save it as dots.svg and use it:
.dots {
background-color: #f8fafc;
background-image: url("dots.svg");
background-size: 20px 20px;
}
Keep the xmlns attribute. Browsers render an SVG without it when it's inline in HTML, but not as an image file or data URI.
Because the dot sits in the middle and never touches an edge, this tile repeats with no seams. Patterns get harder when shapes cross the tile edge (see "Make the tile repeat without visible seams" below).
If you want a motif such as a small icon, you can download icons as SVG from our Icon Library and place the icon's path inside your tile. Our comparison of Material Icons and Font Awesome helps if you're choosing between the two sets.
Option 3: Generate SVG background patterns (waves, dots, stripes, grids, blobs)
When you'd rather move sliders than edit coordinates, a generator is faster. Our free SVG Backgrounds generator makes five pattern types and gives you the result as a file, as SVG code, or as a CSS line.
- Open the SVG Backgrounds page.
- Choose a pattern type: Waves, Dots, Stripes, Grid / Checkerboard or Organic Blobs.
- Adjust the sliders for that type.
- Set Primary Color and Secondary Color.
- Click Generate SVG Background.
- Take the output you need:
- Copy CSS copies a
background-imagedeclaration with the SVG embedded as a data URI. - Download SVG saves a file named
picbolt_svg_background_<type>.svg. - Copy SVG Code copies the raw SVG markup.
- Copy CSS copies a
What to know before you use it:
- It's free and unlimited, with no sign-up and no watermark. Using it doesn't count toward the free plan's 3-downloads-a-day limit.
- The output is SVG only. If you need a PNG (for a slide deck or a design tool that won't take SVG), run the downloaded file through the Image Converter, which accepts SVG input.
- The Pattern Complexity slider currently has no effect on the output. Use the other sliders to change the look.
- The generated patterns are not guaranteed to tile without visible seams. Test them on a large element before you ship (see below).
The Copy CSS output is a background-image declaration, so add the sizing and repeat rules from the next section to suit your layout.
Use the SVG in CSS
You have two ways to reference the pattern.
External file url("pattern.svg") | Data URI url("data:image/svg+xml,...") | |
|---|---|---|
| Extra HTTP request | Yes (cached separately) | No |
| Easy to edit | Yes, it's a normal file | Harder, the markup is encoded inside CSS |
| Best for | Large patterns or ones reused across many pages | Small tiles used in one place |
Data URI encoding rules
Most broken data-URI patterns come from encoding mistakes:
- Start with
data:image/svg+xml,and put the whole thing in double quotes. - Use single quotes inside the SVG so they don't close the CSS string.
- Encode
#as%23. An unencoded#starts a URL fragment and cuts the SVG off, so hex colors like#94a3b8must become%2394a3b8. - Encode
<as%3Cand>as%3E. Browsers usually accept them raw, but encoding them avoids surprises. - Encode any literal
%in the SVG (for examplewidth='100%') as%25. - Remove line breaks.
The hand-written dot tile from Option 2, as a data URI:
.dots {
background-color: #f8fafc;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20'%3E%3Ccircle cx='10' cy='10' r='1.5' fill='%2394a3b8'/%3E%3C/svg%3E");
background-size: 20px 20px;
}
You can also Base64-encode the SVG (data:image/svg+xml;base64,...). It avoids escaping problems, but Base64 turns every 3 bytes into 4 characters, so the result is about a third larger and can't be read or edited by hand.
Repeat, size and position
| Property | What to set | Why |
|---|---|---|
background-size | The tile's size, e.g. 20px 20px | Controls the pattern's scale. Required if the SVG has no width/height |
background-repeat | repeat (the default) | Tiles in both directions. repeat-x or repeat-y for one direction |
background-repeat: round | Optional | Scales the tile slightly so a whole number of tiles fits, so no tile is cut off at the edge |
background-position | e.g. center | Moves where the tiling starts. Useful when cut-off tiles show at one edge |
background-color | A solid color | Fills behind the pattern and shows while the image loads |
To scale a pattern up or down, change background-size rather than editing the SVG. Doubling it to 40px 40px makes everything twice as large.
For a pattern that isn't meant to tile, such as one large wave or blob shape, treat it like a photo:
.hero {
background-image: url("waves.svg");
background-size: cover;
background-repeat: no-repeat;
background-position: center bottom;
}
Make the tile repeat without visible seams
A tile repeats cleanly only when its right edge lines up with its left edge and its bottom edge lines up with its top edge. Any shape that crosses an edge must continue on the opposite edge.
- Shapes inside the tile (a centered dot, a small icon) never cause seams.
- Shapes crossing an edge need a copy on the opposite side. A circle at
cx="0"needs a matching circle atcx="20"in a 20-wide tile. - Waves must start and end at the same height, with the same slope, or you'll see a step every tile.
- Blobs placed randomly almost always get cut at the edge. Use them as a single non-repeating background (
background-size: cover) instead of a tile.
To test, apply the pattern to a large element (the whole body works) and look at it zoomed in. Seams show up as repeating vertical or horizontal lines.
Hairline gaps between tiles usually come from scaling, not the artwork. If background-size works out to a fractional pixel value (from em, % or page zoom), browsers can round each tile differently. Setting the size in whole pixels avoids most of these.
Keep patterns fast
SVG patterns are cheap when they're simple. A few things keep them that way:
- Keep tiles simple. A tile with a handful of shapes is quick to draw. A tile with hundreds of paths gets repeated across the whole element and costs more paint time.
- Avoid SVG filters in tiles. Blur and shadow filters inside a background SVG are expensive to render.
- Use a file for big patterns. A large data URI bloats your CSS, and it downloads again every time the CSS changes. An external file is cached on its own.
- Minify the SVG. Remove editor metadata, comments and unneeded decimal places before encoding.
Colors and contrast
Patterns behind text are a common way to make a page hard to read.
- Keep the two pattern colors close. For a background behind text, the shapes should differ only slightly from the base color (for example
#f1f5f9on#f8fafc). Bold, high-contrast patterns work for empty hero areas and section dividers, not paragraphs. - Check contrast against both colors. Text has to be readable over the pattern's lightest and darkest parts. WCAG AA asks for at least 4.5:1 for normal text and 3:1 for large text.
- Calm a busy pattern with a gradient layer. Background layers stack with the first one on top:
.section {
background-image:
linear-gradient(rgba(255, 255, 255, 0.85), rgba(255, 255, 255, 0.85)),
url("pattern.svg");
background-size: auto, 40px 40px;
}
Change the pattern color from CSS
Since a background SVG can't use currentColor or your CSS variables, you'd normally need one file per color. Using the SVG as a mask avoids that: the shapes decide where color shows, and background-color decides which color.
.section {
position: relative;
isolation: isolate;
}
.section::before {
content: "";
position: absolute;
inset: 0;
z-index: -1;
background-color: var(--pattern-color, #cbd5e1);
mask-image: url("dots.svg");
mask-size: 20px 20px;
pointer-events: none;
}
Now changing --pattern-color recolors the pattern. Add opacity to the ::before rule to fade it further. If you support older browsers, add -webkit-mask-image and -webkit-mask-size with the same values.
Dark mode
Without the mask approach, swap the file in a media query:
.hero { background-image: url("pattern-light.svg"); }
@media (prefers-color-scheme: dark) {
.hero { background-image: url("pattern-dark.svg"); }
}
For gradient backgrounds rather than patterns, see our guides on mesh gradient backgrounds and making a gradient background image.
Which method to use
| Method | Good for | Can't do | Effort |
|---|---|---|---|
| CSS gradients | Dots, straight stripes, grid lines | Curves, waves, blobs | Lowest, no file |
| Hand-written SVG tile | Exact control, icon motifs, guaranteed clean repeats | Slow to write for complex shapes | Medium |
| Generator (SVG Backgrounds) | Quick waves, blobs, checkerboards with slider control | Guaranteed clean tiling, PNG output | Low |
Pick CSS gradients if the pattern is geometric and simple. Write SVG by hand if it has to repeat perfectly or use a specific motif. Use a generator to try out looks quickly, then test the tiling.
If the pattern doesn't show or looks wrong
- Nothing appears (data URI). Usually an unencoded
#in a color, or a double quote inside the SVG closing the CSS string. Encode#as%23and use single quotes inside the SVG. - Nothing appears (file). A
url()in a CSS file resolves relative to the CSS file, not the HTML page. Check the path, and check that the SVG hasxmlns="http://www.w3.org/2000/svg". - One huge shape instead of a pattern. The SVG has no
widthandheight, so the browser doesn't know the tile size. Setbackground-sizeexplicitly. - Lines or steps between tiles. Either the artwork doesn't match at the edges (fix the tile, or use it once with
background-size: cover), orbackground-sizeis a fractional pixel value (switch to whole pixels). - Pattern ignores your CSS color or dark mode. Background SVGs can't read page CSS. Use the mask technique or separate files per theme.
- Pattern is cut off at the edges. Set
background-position: center, or usebackground-repeat: round.
FAQ
Do SVG background patterns affect accessibility?
CSS backgrounds are decorative and screen readers ignore them, which is correct for a pattern. The accessibility risk is text contrast, so check text against the pattern's lightest and darkest colors.
Should I inline the SVG in HTML instead of using CSS?
Only if you need to style or animate the shapes with page CSS. For a repeating decorative background, background-image keeps your HTML clean and handles tiling for you.
Can I use a pattern behind a screenshot instead of on a website?
Yes, and you don't need SVG for that. Our Screenshot Mockup editor has built-in pattern backgrounds on its BG tab, and our guide on adding a background to a screenshot walks through it.
