coding help
Quick Code


Copy to clipboard
<div style="background:__;background-size:_px _px;">CONTENT</div>
Two before and after examples of background size changes. When background size is reduced, the background tiles.

Background size is a CSS attribute that controls how large a background is. It can be handy when you'd like a repeating pattern as a background.

<div style="background: ____; background-size: ___;">CONTENT</div>


There are different ways to express background size, including size in px, percentages, and words like "cover."

General

It looks like this:

<div style="background: ____; background-size: ___;">CONTENT</div>

Inside the background size, you can specify different things:

There are also other values you can enter for background-size, like:

However, you're less likely to use these on wikis. For example, "cover" and "contain" are generally used with background images. Background images don't work properly on wikis (though we have invented partial work-arounds).

Pixel sizes

To use background-size, add the following:

<div style="background:__;background-size:_px _px;">CONTENT</div>

The first space is for a background. For example, perhaps you'd like to put a gradient there.

Now, fill in the 2 _px spaces with sizes for your background. The first one controls width and the second controls height.

Examples

Here's a radial gradient:

Here's a repeating radial gradient.
Each tile is 50px wide and 50px high.
It's a bit visually distracting.

The code for this:

<div style="background-color: LightCyan; color: black; padding: 10px; background:radial-gradient(LightCyan, PaleTurquoise); background-size:50px 50px;">
Here's a repeating radial gradient.<br/>
Each tile is 50px wide and 50px high.<br/>
It's a bit visually distracting.
</div>

Here are more example CSS styles:

background: linear-gradient(45deg, LavenderBlush, pink); background-size:50px 100px;"
background: repeating-conic-gradient(FloralWhite 0% 25%, AntiqueWhite 25% 50%); background-size: 100px 100px;

Percentage sizes

Percentage sizes let you set widths and heights according to the box size. For example, maybe you want a pattern to repeat 4 times horizontally, no matter how wide the box is. Then, setting the width to 25% is your best bet.

<div style="background:__;background-size:_% _%;">CONTENT</div>

Like in the px version, the first number controls width and the second controls height.

Examples

Let's try some diagonal tiles. If you're able, resize the width of the window and notice how the pattern shifts.

Here's a repeating linear gradient.
Each tile is exactly 20% wide and 50% tall.
Count them: 5 per row, 2 per column.


The code for this:

<div style="background-color: LavenderBlush; color: black; padding: 10px; background:linear-gradient(45deg, Ivory, Bisque); background-size:20% 50%;">
Here's a repeating linear gradient.<br />
Each tile is exactly 20% wide and 50% tall.<br />
Count them: 5 per row, 2 per column.
</div>

Some example CSS styles:

background: radial-gradient(ivory, bisque); background-size: 25% 50%;"
background: repeating-conic-gradient(GhostWhite 0% 25%, MistyRose 25% 50%); background-size: 50% 100%;

Best practices

See also: Accessibility

Remember that repeating backgrounds can look complex, creating visual clutter. This can make it hard to read any text that goes on top of it. You have 2 options:

Here's an example of the latter:

The text goes in a white container, while the repeating background is outside it. This lets us read the text easily while still taking in the background.

And here's the code:

<div style="background-color: LightCyan; background: repeating-conic-gradient(LightCyan 0% 25%, PaleTurquoise 25% 50%); background-size: 50px 50px; padding: 25px; width: 400px;">
<div style="width: 300px; background-color: GhostWhite; border: 2px solid Turquoise; color: black; padding: 10px; margin: 0 auto;">
The text goes in a white container, while the repeating background is outside it. This lets us read the text easily while still taking in the background.
</div>
</div>

See also

External links