Backgrounds
Backgrounds
background-color — the simplest background
.box {
background-color: #f0f4ff;
}
The plainest background: a solid colour fill behind the element's content and padding. Accepts every colour format from the last chapter (name, hex, rgb, rgba, hsl). Using rgba here gives a see-through tint over whatever is behind. This alone handles most cards, buttons and sections.
background-image — a picture behind content
.hero {
background-image: url("school.jpg");
}
size, position, repeat — controlling the image
.hero {
background-image: url("school.jpg");
background-size: cover; /* fill the whole area, cropping if needed */
background-position: center; /* keep the middle of the image visible */
background-repeat: no-repeat; /* don't tile it */
height: 400px;
}
| Property | Common values | Effect |
|---|---|---|
| background-size | cover / contain / 100px | cover = fill+crop; contain = fit fully inside |
| background-position | center / top / 50% 20% | Which part stays visible |
| background-repeat | no-repeat / repeat-x | Tile or not |
background-size: cover + background-position: center + background-repeat: no-repeat. This is how every website's hero image fills the screen edge-to-edge without stretching or tiling, staying centred on any screen size. cover vs contain: cover fills the box and crops overflow; contain shrinks the image to fit entirely (possibly leaving gaps). Cover for banners, contain for logos.Gradients — colors blending, no image needed
/* LINEAR - colors along a line */
background: linear-gradient(to right, navy, skyblue);
background: linear-gradient(135deg, #667eea, #764ba2);
/* RADIAL - colors from a center outward */
background: radial-gradient(circle, gold, orange);
Gradients are smooth colour transitions generated entirely by CSS — no image to download, infinitely scalable, and everywhere in modern design (buttons, hero sections, cards). linear-gradient blends along a direction (to right, or an angle like 135deg); radial-gradient blends outward from a centre. You can add more colours: linear-gradient(to right, red, yellow, green).
The background Shorthand
/* Instead of 4 separate lines: */
background-color: navy;
background-image: url("bg.jpg");
background-position: center;
background-repeat: no-repeat;
/* Combine into ONE background property: */
background: navy url("bg.jpg") center / cover no-repeat;
│ │ │ │ │
color image position size repeat
background shorthand takes colour, image, position, size (after a /) and repeat in one go. It's cleaner, though beginners often prefer the separate properties until the order feels natural. Both are equally valid.background-color — सबसे सरल background
.box {
background-color: #f0f4ff;
}
सबसे सादा background: element के content और padding के पीछे solid colour fill. पिछले chapter का हर colour format लेता है (name, hex, rgb, rgba, hsl). यहां rgba use करने से पीछे जो हो उस पर see-through tint मिलता है. यह अकेला ज़्यादातर cards, buttons और sections संभाल लेता है.
background-image — content के पीछे तस्वीर
.hero {
background-image: url("school.jpg");
}
size, position, repeat — image control करना
.hero {
background-image: url("school.jpg");
background-size: cover; /* poora area bharo, zaroorat par crop */
background-position: center; /* image ka beech dikhta rahe */
background-repeat: no-repeat; /* tile mat karo */
height: 400px;
}
| Property | Common values | असर |
|---|---|---|
| background-size | cover / contain / 100px | cover = fill+crop; contain = पूरा अंदर fit |
| background-position | center / top / 50% 20% | कौन-सा हिस्सा दिखे |
| background-repeat | no-repeat / repeat-x | Tile या नहीं |
background-size: cover + background-position: center + background-repeat: no-repeat. हर website का hero image ऐसे ही screen को edge-to-edge भरता है बिना खिंचे या tile हुए, हर screen size पर centred. cover vs contain: cover box भरकर overflow crop करता है; contain image को पूरा fit होने के लिए छोटा करता है (शायद gaps छोड़कर). Banners के लिए cover, logos के लिए contain.Gradients — colors का मेल, image की ज़रूरत नहीं
/* LINEAR - line ke saath colors */
background: linear-gradient(to right, navy, skyblue);
background: linear-gradient(135deg, #667eea, #764ba2);
/* RADIAL - center se bahar ki taraf colors */
background: radial-gradient(circle, gold, orange);
Gradients पूरी तरह CSS से बने smooth colour transitions हैं — download को image नहीं, अनंत scalable, और modern design में हर जगह (buttons, hero sections, cards). linear-gradient एक दिशा में blend करता है (to right, या 135deg जैसा angle); radial-gradient center से बाहर. और colours जोड़ सकते हैं: linear-gradient(to right, red, yellow, green).
background Shorthand
/* 4 alag lines ke bajaye: */
background-color: navy;
background-image: url("bg.jpg");
background-position: center;
background-repeat: no-repeat;
/* EK background property me jodo: */
background: navy url("bg.jpg") center / cover no-repeat;
│ │ │ │ │
color image position size repeat
background shorthand colour, image, position, size (/ के बाद) और repeat एक साथ लेता है. साफ है, हालांकि beginners अक्सर separate properties पसंद करते हैं जब तक order स्वाभाविक न लगे. दोनों बराबर valid.💻 Live Code Editor
इस पेज का code यहाँ तैयार है — बदलिए और तुरंत नतीजा देखिए. कुछ install किए बिना.console.log देखने के लिए F12 दबाइए.