#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
CSS3 Fundamentals

CSS3 Backgrounds

Background properties paint the area behind an element's content and padding. With them you can set a solid colour, place one or more images, control how images repeat and scale, and decide whether they scroll with the page. They are used for hero sections, card textures, decorative patterns and icons beside text.

The individual properties

  • background-color fills the box with a colour and sits underneath any image.
  • background-image takes url(...) or a gradient.
  • background-repeat chooses repeat, no-repeat, repeat-x, repeat-y, space or round.
  • background-position places the image, using keywords like center or values like right 20px top 10px.
  • background-size sets the image size; cover and contain are the two most used keywords.
  • background-attachment decides whether the image scrolls with the element or stays fixed.
  • background-origin and background-clip decide whether painting starts at the border, padding or content edge.

Cover versus contain

cover scales the image until it fills the whole box, cropping whatever spills over. It is right for decorative photos where filling the area matters more than seeing every pixel. contain scales the image so all of it fits, which may leave empty bands at the sides. Use it for logos or diagrams where cropping would lose meaning.

Layering several backgrounds

CSS3 allows a comma-separated list of backgrounds on one element. The first layer in the list is painted on top. A common trick is placing a semi-transparent gradient over a photo so white text stays readable.

.hero {
  min-height: 60vh;
  color: white;
  background:
    linear-gradient(rgba(0, 0, 0, 0.55), rgba(0, 0, 0, 0.2)),
    url("images/city-night.jpg") center / cover no-repeat;
  background-color: #1d2533; /* shown while the image loads */
}

.search-input {
  padding-left: 36px;
  background: url("icons/search.svg") no-repeat 10px center / 18px 18px;
}

Notice the shorthand order: position, then a slash, then size. The slash is required when you include a size in the background shorthand.

For high-resolution screens, the image-set() function lets the browser choose between versions of a background image, such as a normal and a double-density file. That keeps photos sharp on phones without forcing every visitor to download the largest file.

Shorthand resets and other gotchas

The background shorthand resets every background property you do not mention. If you set background-color first and then write background: url(...) later in the same rule, the colour returns to transparent. Either put all values in the shorthand or use only the individual properties.

Relative image paths are resolved from the stylesheet's location, not from the HTML page. A path that works in an inline style may break once moved into a CSS file in another folder.

Also, a background image is decoration only. Screen readers do not announce it and it has no alt text, so meaningful pictures such as product photos belong in an img element. Finally, background-attachment: fixed is ignored or janky on many mobile browsers, so do not rely on it for important effects.

Keep in mind

Always pair a background image with a fallback colour that keeps text readable, use cover for photos and contain for logos, and remember that the first layer listed is the one on top.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course