HTML & CSSEasy
What are background properties?
Background properties paint the area behind an element's content and padding: a color, one or more images or gradients, and how those are sized, positioned, repeated, attached and clipped. They can be combined in the background shorthand, and several layers can be stacked with commas.
The properties
background-color: a solid color, painted under all images.background-image:url(),linear-gradient(),radial-gradient(),conic-gradient().background-size:cover(fill, may crop),contain(fit, may letterbox), or explicit sizes.background-position:center,top right,50% 20%.background-repeat:repeat,no-repeat,repeat-x,space,round.background-attachment:scroll,fixed(parallax-like; expensive and ignored on iOS),local.background-origin/background-clip: whether it starts or is painted under the border, padding or content.background-clip: textcreates gradient text.
Shorthand and layers
background: linear-gradient(rgb(0 0 0 / .5), transparent),
url(hero.jpg) center / cover no-repeat, #0b1020;
The first layer listed is on top; the color goes on the last layer.
Best practices
- Use background images only for decoration; content images belong in
<img>withalttext. - Always set a fallback
background-colorso text stays readable while the image loads. - Make sure text over images has enough contrast (add a gradient overlay).
- LCP background images are discovered late; preload them or use
<img>instead.
Code example
html
1<style>2 body { font-family: system-ui; display: grid; gap: 12px; max-width: 420px; }3 .hero {4 height: 140px; color: white; padding: 12px;5 background:6 linear-gradient(to top, rgb(11 16 32 / .8), transparent 70%),7 url(https://picsum.photos/id/1039/840/280) center / cover no-repeat,8 #0b1020;9 display: flex; align-items: end; font-size: 1.4rem; font-weight: 800;10 }11 .dots { height: 60px; background: radial-gradient(#2b4bff 2px, transparent 2.5px) 0 0 / 16px 16px, #fff8ec; border: 2px solid #0b1020; }12 .conic { width: 80px; height: 80px; border-radius: 50%; background: conic-gradient(#2b4bff 0 65%, #e5e7eb 0); }13 .text { font-size: 2rem; font-weight: 900; background: linear-gradient(90deg, #2b4bff, #e11d48); background-clip: text; color: transparent; }14</style>15<div class="hero">Gradient overlay + cover image</div>16<div class="dots"></div>17<div class="conic" role="img" aria-label="65 percent complete"></div>18<div class="text">Gradient text</div>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.