SA-21/06, C-11, Asapur, Varanasi, UP support@kamaldnp.com +91 8318013442
CSS
1. CSS Tutorial for Beginners — Learn CSS from Scratch 2. CSS Syntax Explained — Selectors, Properties & Values 3. CSS Selectors Tutorial — Class, ID, Attribute & More 4. How to Add CSS to HTML — Inline, Internal, External 5. CSS Colors Tutorial — HEX, RGB, RGBA, HSL Explained 6. CSS Backgrounds Tutorial — Images, Size & Position 7. CSS Borders Tutorial — Style, Width, Radius & Shadow 8. CSS Margin Explained — Shorthand, Auto & Collapse 9. CSS Padding Explained — Shorthand & Margin Difference 10. CSS Height and Width — px, %, em, rem, vw, vh Units 11. CSS Box Model Explained — Content, Padding, Border 12. CSS Text Styling Tutorial — Color, Align & Spacing 13. CSS Fonts Tutorial — font-family, Sizes & Google Fonts 14. CSS Link Styling — Hover, Visited, Active & Buttons 15. CSS Lists Styling — Markers, Counters & Shorthand 16. CSS Tables Styling — Zebra Stripes & Hover Rows 17. CSS Display Property — Block, Inline, None Explained 18. CSS Position Explained — Relative, Absolute, Fixed 19. CSS Overflow Explained — Hidden, Scroll, Auto & More 20. CSS Float and Clear — Layouts & Clearfix Trick 21. CSS Pseudo-classes — Hover, Focus, nth-child & More 22. CSS Pseudo-elements — ::before, ::after & ::first-line 23. CSS Form Styling — Inputs, Labels & Buttons Tutorial 24. CSS Navigation Bar Tutorial — Dropdown Menu Included 25. CSS Website Layout Tutorial — Header, Sidebar, Footer 26. CSS Flexbox Tutorial — Complete Guide with Examples 27. CSS Grid Tutorial — Columns, Rows & Layouts Guide 28. CSS Responsive Design — Media Queries Tutorial 29. CSS Transitions & Animations — No JavaScript Needed 30. CSS Variables Tutorial — :root, var() & Theming 31. CSS Transforms Explained — Rotate, Scale, Translate 32. CSS Filters Tutorial — Blur, Grayscale, Sepia & More 33. CSS Text Effects — Shadows, Wrapping & Overflow 34. CSS Keyframes Tutorial — Bouncing Ball Animation 35. CSS Grid Template Areas — Layout in Words, Not Numbers
Final Test — Certificate ₹10
Try this code live
Lesson 6 of 35

CSS Backgrounds Tutorial — Images, Size & Position

4 min read · Free · No login needed

Every element has a background — by default it is transparent, so you see whatever is behind it. The background properties let you paint that space: a flat colour, a picture, a gradient, or all three together. A good background makes a page feel finished; a bad one makes the text unreadable.

Diagram — see how it fits together
Diagram: CSS Backgrounds

The background properties

PropertyExampleWhat it does
background-color#fff8e1Paints a flat colour behind the element.
background-imageurl("photo.jpg")Places a picture behind the element.
background-repeatno-repeatStops the picture from tiling like bathroom tiles.
background-positioncenterChooses where the picture sits: top, center, right, or exact numbers.
background-sizecovercover fills the whole area; contain shows the whole picture.
background-attachmentfixedfixed keeps the picture still while the page scrolls.

Syntax — a hero banner

.hero {
  background-color: #1e3a8a;
  background-image: url("campus.jpg");
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}

Read it top to bottom: paint the banner dark blue first (so the text is readable even if the picture fails to load), then place the photo on top, do not repeat it, centre it, and stretch it until it covers the whole banner. The colour is the safety net; the picture is the decoration.

The shorthand

All six properties can be written in one line, in this order: colour, image, repeat, attachment, position, then a slash, then size.

.hero {
  background: #1e3a8a url("campus.jpg") no-repeat center / cover;
}

Shorthand is shorter, but it is also easier to get wrong — one word in the wrong place and the whole line is ignored. While learning, write the long form. Use the shorthand only when you are sure of the order.

Note: A background picture is decoration, not content. If the picture carries information — a photo of a person, a chart, a product — use an <img> tag instead, so screen readers can read its alt text. Backgrounds are for patterns, textures, and banners.

Gradients — backgrounds without any picture

A gradient is a smooth blend from one colour to another, and CSS draws it for you — no image file needed. A gradient is a background-image, so it sits in that property.

.sunset {
  background-image: linear-gradient(to right, orange, pink);
}
.ball {
  background-image: radial-gradient(circle, yellow, red);
}

linear-gradient blends in a straight line — to right, to bottom, or an angle like 45deg. radial-gradient blends out from a centre point, like a ball lit from above. You can blend three or more colours by listing them: linear-gradient(red, yellow, green).

Tip: background-size: cover is the one value to remember for full-width banners. It scales the picture until the whole area is covered, cropping the edges if needed. Use it for hero sections, and your banner will look right on a phone and on a big screen.

Common mistakes

  • Forgetting no-repeat — by default a background picture repeats in both directions, so a small photo tiles across the whole element.
  • Writing a wrong image path — the url() is relative to the CSS file, not the HTML file. If the CSS is in a different folder, the path must account for that.
  • No fallback colour — if the picture fails to load, dark text on a transparent background can become unreadable. Always set a background-color first.
  • Using background shorthand in the wrong order — the browser ignores the whole line instead of guessing what you meant.
  • Putting text over a busy picture with no overlay — the words disappear into the photo. A dark gradient or a semi-transparent colour behind the text fixes it.

Remember

  • background-color is the safety net — set it before any picture.
  • background-repeat: no-repeat stops the picture from tiling.
  • background-size: cover fills the area; contain shows the whole picture.
  • Gradients are background-image values — no image file needed.
  • Backgrounds are decoration; real content pictures belong in an <img> tag.

Example explained

  • .hero { background-image: linear-gradient(to right, #f97316, #ec4899); } — a gradient background, orange blending into pink, drawn by CSS with no picture file at all.
  • .hero { color: white; } — white text on the bright gradient, so the words stay readable.
  • .pattern { background-color: #fef3c7; background-image: radial-gradient(circle, #fde68a 20%, transparent 20%); background-size: 40px 40px; } — a pale yellow base with small dots, repeated every 40 pixels like a polka-dot pattern.
  • .plain { background-color: #e0f2fe; } — the simplest background: one flat colour, no picture, no gradient.

Example

Example
<!DOCTYPE html>
<html>
<head>
<title>CSS Backgrounds</title>
<style>
body { font-family: Verdana, sans-serif; margin: 0; }
.hero { background-image: linear-gradient(to right, #f97316, #ec4899); color: white; text-align: center; padding: 40px; }
.pattern { background-color: #fef3c7; background-image: radial-gradient(circle, #fde68a 20%, transparent 20%); background-size: 40px 40px; padding: 30px; margin: 20px; }
.plain { background-color: #e0f2fe; padding: 30px; margin: 20px; }
</style>
</head>
<body>

<div class="hero">
<h1>Chai Festival 2026</h1>
<p>One day, twenty flavours, unlimited kulhads.</p>
</div>
<div class="pattern">
<h2>Pattern box</h2>
<p>This box has a dotted pattern made only with CSS — no picture file at all.</p>
</div>
<div class="plain">
<h2>Plain box</h2>
<p>This box has a simple flat background colour.</p>
</div>

</body>
</html>

Output

Output — how it looks in the browser
Output of CSS Backgrounds Tutorial — Images, Size & Position

Try it yourself — live editor

Change the code below and press Run to see the result instantly. No file to save, no login needed.

Result

Frequently asked questions

The questions learners ask most about this lesson — read the question, then open it to see the answer.

1. How do you stop a background picture from repeating?

With background-repeat: no-repeat. By default a background picture repeats in both directions.

2. What does background-size: cover do?

It makes the background picture fill the whole area, even if some of it gets cropped.

3. Should you set a background colour even with a picture?

Yes. background-color is the safety net — if the picture fails to load, the text stays readable.

Why learn this yourself — and not just ask AI?

AI can write code for you, but it cannot think for you. When you learn HTML yourself, your brain builds the real skill: you understand why a page looks the way it does, you can fix it when it breaks, and you can answer it in an exam or an interview without help. That is the skill that gets you a job — not a copied answer.

Get certified — just ₹10

Finish all 35 lessons, pass the test (80%), pay ₹10 and get your certificate.

Take the test

Prefer watching? Video courses teach the same topics step by step.

Go further with a full course

Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.

Summer Industrial Training in MERN Stack 2026
₹3,000
View
CapCut Mobile Video Editing Course Beginner to Advance
₹99
View
Summer Training in Python Programming 2026
₹3,000
View
Browse all courses
CSS — Lessons
1. CSS Tutorial for Beginners — Learn CSS from Scratch 2. CSS Syntax Explained — Selectors, Properties & Values 3. CSS Selectors Tutorial — Class, ID, Attribute & More 4. How to Add CSS to HTML — Inline, Internal, External 5. CSS Colors Tutorial — HEX, RGB, RGBA, HSL Explained 6. CSS Backgrounds Tutorial — Images, Size & Position 7. CSS Borders Tutorial — Style, Width, Radius & Shadow 8. CSS Margin Explained — Shorthand, Auto & Collapse 9. CSS Padding Explained — Shorthand & Margin Difference 10. CSS Height and Width — px, %, em, rem, vw, vh Units 11. CSS Box Model Explained — Content, Padding, Border 12. CSS Text Styling Tutorial — Color, Align & Spacing 13. CSS Fonts Tutorial — font-family, Sizes & Google Fonts 14. CSS Link Styling — Hover, Visited, Active & Buttons 15. CSS Lists Styling — Markers, Counters & Shorthand 16. CSS Tables Styling — Zebra Stripes & Hover Rows 17. CSS Display Property — Block, Inline, None Explained 18. CSS Position Explained — Relative, Absolute, Fixed 19. CSS Overflow Explained — Hidden, Scroll, Auto & More 20. CSS Float and Clear — Layouts & Clearfix Trick 21. CSS Pseudo-classes — Hover, Focus, nth-child & More 22. CSS Pseudo-elements — ::before, ::after & ::first-line 23. CSS Form Styling — Inputs, Labels & Buttons Tutorial 24. CSS Navigation Bar Tutorial — Dropdown Menu Included 25. CSS Website Layout Tutorial — Header, Sidebar, Footer 26. CSS Flexbox Tutorial — Complete Guide with Examples 27. CSS Grid Tutorial — Columns, Rows & Layouts Guide 28. CSS Responsive Design — Media Queries Tutorial 29. CSS Transitions & Animations — No JavaScript Needed 30. CSS Variables Tutorial — :root, var() & Theming 31. CSS Transforms Explained — Rotate, Scale, Translate 32. CSS Filters Tutorial — Blur, Grayscale, Sepia & More 33. CSS Text Effects — Shadows, Wrapping & Overflow 34. CSS Keyframes Tutorial — Bouncing Ball Animation 35. CSS Grid Template Areas — Layout in Words, Not Numbers
Final Test — Certificate ₹10

🤖 KAMALDNP AI Assistant

Ask about courses, website & more

Hi! I'm KAMALDNP AI Assistant. Ask me about our courses, website features, or anything else! 😊
Please wait 5s...
🤖 KAMALDNP Assistant ✕

Request human callback:

🔐 Login Required
✕

Please log in to download. You will stay on this page after logging in!

New here? Register now →