CSS Backgrounds & Borders
Backgrounds element ke visual surface ko style karte hain, aur borders uski boundary define karte hain. Is chapter me colors, images, gradients, background sizing, border styles aur rounded corners practical examples ke saath samjhenge.
background-color
background-color sets the background color behind an element's content and padding area.
.card {
background-color: #f8fafc;
color: #0f172a;
}Background color element ke peeche ka surface color set karta hai. Text color ke saath enough contrast maintain karna readability ke liye important hai.
background-image
A background image is decorative CSS content painted behind the element. It does not replace meaningful HTML images.
.hero {
background-image: url("hero.jpg");
}<img> with appropriate alt text use karo.background-repeat
Small background images repeat by default. Use background-repeat when you want to control tiling.
.hero {
background-image: url("hero.jpg");
background-repeat: no-repeat;
}
.pattern {
background-repeat: repeat;
}repeat— repeat horizontally and vertically.no-repeat— show one copy only.repeat-x— repeat horizontally.repeat-y— repeat vertically.
background-position
background-position decides where the background image is placed inside the element.
.hero {
background-position: center center;
}
.avatar-bg {
background-position: top right;
}center image ko available background area ke center me place karta hai. top right image ko upper-right side me align karta hai.
background-size
background-size controls how large a background image is rendered.
.hero-cover {
background-size: cover;
}
.logo-panel {
background-size: contain;
}cover— fills the area while preserving aspect ratio; some image parts may be cropped.contain— shows the whole image while preserving aspect ratio; empty space may remain.
cover full-bleed hero sections me common hai. contain tab useful hai jab entire image visible rehni chahiye.CSS gradients
Gradients are generated images created directly in CSS. A linear gradient blends colors along a line.
.banner {
background-image: linear-gradient(
135deg,
#0b1f3a,
#0b7cff
);
color: white;
}You can also use radial gradients:
.badge-area {
background-image: radial-gradient(
circle,
#dbeafe,
#eff6ff
);
}Gradient overlay on an image
Multiple backgrounds can layer a gradient over an image.
.hero {
background-image:
linear-gradient(rgb(15 23 42 / 0.7), rgb(15 23 42 / 0.7)),
url("hero.jpg");
background-size: cover;
background-position: center;
}Image ke upar dark translucent gradient text ko readable bana sakta hai. Lekin contrast actual image ke saath test karna zaroori hai.
background shorthand
The background shorthand can combine several background properties. It is compact, but can reset omitted background sub-properties.
.hero {
background:
#0f172a
url("hero.jpg")
center / cover
no-repeat;
}Border basics
A border has width, style and color. The border style must be visible, such as solid, for the border to render.
.card {
border-width: 1px;
border-style: solid;
border-color: #dbe4ee;
}The common shorthand is:
.card {
border: 1px solid #dbe4ee;
}Common border styles
.solid { border: 2px solid #0b7cff; }
.dashed { border: 2px dashed #0b7cff; }
.dotted { border: 2px dotted #0b7cff; }
.double { border: 4px double #0b7cff; }Use decorative border styles carefully; a simple solid border is usually easiest to scan in application interfaces.
Individual border sides
You can style one side independently.
.note {
border-left: 4px solid #0b7cff;
padding-left: 1rem;
}
.table-row {
border-bottom: 1px solid #e2e8f0;
}Callout note ke left side accent border ya table rows ke beech bottom border clean visual separation de sakta hai.
border-radius
border-radius rounds the corners of the border box.
.card {
border-radius: 12px;
}
.button {
border-radius: 8px;
}
.pill {
border-radius: 999px;
}A very large radius is commonly used to create pill-shaped buttons or badges.
Make a circle
An equal-width, equal-height square can become a circle with border-radius: 50%.
.avatar {
width: 96px;
height: 96px;
border-radius: 50%;
object-fit: cover;
}50% ellipse bana sakta hai, perfect circle nahi.Border vs outline
Unlike a border, an outline is drawn outside the border edge and normally does not take up layout space. It is useful for keyboard focus indicators.
button:focus-visible {
outline: 3px solid #60a5fa;
outline-offset: 3px;
}Backgrounds, borders & accessibility
- Text ke peeche background par readable contrast maintain karo.
- Busy background images ke upar text rakhte waqt overlay ya solid surface consider karo.
- Important information sirf decorative background image me mat rakho.
- Error/success state ko sirf border color se communicate mat karo.
- Keyboard focus outline ko clearly visible rakho.
- Very thin low-contrast borders ko only separation cue mat banao when stronger grouping is needed.
Best practices
- Decorative backgrounds ko purposeful rakho.
- Large images ko optimize karo before using them as backgrounds.
- Repeated cards ke liye consistent border color and radius scale use karo.
- Background image ke saath fallback background color consider karo.
- Shorthands tab use karo jab unka reset behavior samajh ho.
- Rounded corners ka overuse avoid karo; design system me consistent values rakho.
Common beginner mistakes
background-size: coverko image cropping ke bina full image display samajhna.- Background image ko meaningful content ke replacement ke roop me use karna.
- Text contrast test na karna.
- Border width/color set karke
border-stylebhool jana. - Every component par different random radius use karna.
- Focus outline remove karke replacement na dena.
- Background shorthand use karke existing sub-properties accidentally reset karna.
Practice Task
Ek “Course Promo Card” banao aur backgrounds + borders practice karo.
- Card ko light background color do.
- 1px solid border aur 16px border-radius add karo.
- Card ke top banner me linear gradient use karo.
- Ek second version me background image with
cover,centeraurno-repeattest karo. - Image version me readable text ke liye gradient overlay add karo.
- Badge ko pill shape dene ke liye large border-radius use karo.
- Ek callout me left border accent add karo.
- Button ke
:focus-visiblestate ke liye clear outline banao. - Browser DevTools me computed background and border properties inspect karo.