Performance: Background image
Background images affect page load speed and rendering performance by adding network requests and paint cost.
Jump into concepts and practice - no test required
body { background-image: url('optimized-photo.webp'); background-size: cover; background-repeat: no-repeat; }body { background-image: url('large-photo.jpg'); background-size: cover; }| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Large unoptimized background image | Minimal | 0 | High (large paint area) | [X] Bad |
| Optimized compressed background image | Minimal | 0 | Low (smaller paint area) | [✓] Good |
background-image do on a webpage?background-imagebackground-image.photo.jpg?: to assign values, not an equals sign or parentheses.url('filename') with parentheses and quotes around the filename.<div>?
div {
background-image: url('tree.png');
background-repeat: no-repeat;
background-position: center;
background-size: contain;
width: 200px;
height: 200px;
border: 1px solid black;
}body {
background-image: url(tree.png);
background-repeat: no-repeat
background-position: center;
}