CSS/styling
Colour & Background
CSS offers multiple color formats and a rich set of background properties for controlling fills, images, gradients, and positioning behind content.
3 min readcss#css#colour#background
Introduction
CSS offers multiple color formats and a rich set of background properties for controlling fills, images, gradients, and positioning behind content.
Subtopics
- Color formats: keyword, hex,
rgb()/rgba(),hsl()/hsla() background-colorbackground-imageand gradientsbackground-repeat,background-size,background-positionbackground-attachment- Shorthand
background
Syntax
css
.color-formats {
color: tomato; /* keyword */
color: #ff6347; /* hex */
color: rgb(255, 99, 71); /* rgb */
color: rgba(255, 99, 71, 0.6); /* rgb + alpha */
color: hsl(9, 100%, 64%); /* hsl */
}
.bg-basic {
background-color: #f5f5f5;
}
.bg-image {
background-image: url("hero.jpg");
background-size: cover;
background-position: center;
background-repeat: no-repeat;
background-attachment: fixed;
}
.bg-gradient {
background-image: linear-gradient(to right, #ff7e5f, #feb47b);
}
.bg-shorthand {
background: #fff url("pattern.png") no-repeat center / cover;
}
background-color— fills the element's background with a solid color.background-image: url()— sets an image as the background.background-size: cover— scales the image to fully cover the box (may crop);containfits the whole image without cropping.background-position— positions the image (e.g.,center,top left, or coordinates).background-repeat: no-repeat— prevents tiling.background-attachment: fixed— keeps the background fixed during scroll.linear-gradient()/radial-gradient()— generates a gradient image without needing an actual file.
Important Properties
| Property | Purpose | Syntax | Example |
|---|---|---|---|
color | Text color | color: value; | color: #333; |
background-color | Fill color | background-color: value; | background-color: white; |
background-image | Image/gradient fill | background-image: url()/gradient(); | background-image: linear-gradient(red, blue); |
background-size | Scale background image | background-size: value; | background-size: cover; |
background-position | Position image | background-position: value; | background-position: center; |
background-repeat | Tiling behavior | background-repeat: value; | background-repeat: no-repeat; |
background (shorthand) | Combine all bg properties | background: color image repeat position/size; | background: #fff url(x.png) center/cover; |
Common Use Cases
- Beginner: a solid
background-coloron a<div>card. - Practical UI: a hero banner using
background-imagewithcoversizing andcenterpositioning. - Real-world: a full-width parallax section combining
background-attachment: fixedwith a gradient overlay via layeredlinear-gradient()and an image.
Common Errors
- ❌
background-image: hero.jpg;(missingurl()) → ✅background-image: url("hero.jpg");— image paths must be wrapped inurl(). - ❌
background-size: 100%;expecting full crop-fill behavior → ✅ usebackground-size: cover;to fill the box while preserving aspect ratio. - ❌
color: rgb(255, 99, 71, 0.5);mixing rgb with alpha incorrectly → ✅ usergba(255, 99, 71, 0.5)(or modernrgb(255 99 71 / 0.5)) for transparency. - ❌ Forgetting
background-repeat: no-repeat, causing an unintended tiled pattern → ✅ explicitly setno-repeatwhen a single centered image is intended. - ❌
background: red url(bg.png);overriding a previously setbackground-colorunexpectedly → ✅ remember the shorthandbackgroundresets all background sub-properties; use longhand if you only want to change one.
Common Mistakes
- Forgetting quotes or
url()wrapper on image paths. - Confusing
covervscontainforbackground-size. - Using shorthand
backgroundand unintentionally resetting other background properties. - Not testing gradient color stops on different screen widths.
Quick Reference
css
color: hsl(210, 90%, 50%);
background-color: #fafafa;
background-image: linear-gradient(135deg, #667eea, #764ba2);
background: #fff url("bg.jpg") no-repeat center / cover;
LB
Author
Lakshya Bansal