More CSS answers

by Danny
Published 2 min read

A CSS background image usually fails because the path is wrong, the element has no height, or another rule overrides it. Here is how to find which one.

The short answer

#

Check three things in order. First, the path: a relative url() in a stylesheet is resolved from the CSS file's location, not the HTML page. Second, the size: an empty element has zero height, so there is nothing to paint the image on. Third, overrides: a later background shorthand resets background-image.


_10
.hero {
_10
/* path relative to this CSS file, e.g. /css/site.css */
_10
background-image: url("../images/hero.jpg");
_10
background-size: cover;
_10
background-position: center;
_10
min-height: 24rem; /* give the element a size */
_10
}

Why it happens

#

A background image is decoration on an element's box. It does not create space the way an img element does, and it fails silently. The browser does not show a broken image icon; you just see nothing. That makes it easy to miss the real cause.

The most common causes are:

  • Wrong path. In a file at /css/site.css, url("images/hero.jpg") points to /css/images/hero.jpg. The same path in an inline style attribute is resolved from the page URL instead.
  • No size. A div with no content and no height is 0px tall. The image is there, but in a box with no area.
  • An override. background: #fff; written after background-image resets the image to none, because the shorthand sets every background property.
  • Case and file names. Hero.JPG and hero.jpg are the same file on macOS and Windows but different files on most Linux servers, so it works locally and breaks in production.
  • Bundler paths. Frameworks rewrite or serve assets differently. A path that works in a plain HTML file may not work inside a component.

How to fix it

#

Check the Network panel first

Open DevTools, go to the Network panel, filter by Img and reload. If the image is listed with a 404, the path is wrong; hover the request to see the full URL the browser tried. If it is not listed at all, the rule is not applying or the element is not on the page.

Use a root-relative or correct relative path

A path that starts with / is resolved from the site root, which removes the guesswork about where the CSS file lives.


_10
.hero {
_10
background-image: url("/images/hero.jpg");
_10
}

In Vite and Next.js, files in the public folder are served from the root, so public/images/hero.jpg is /images/hero.jpg. Files imported from src are processed by the bundler instead, and in CSS you refer to them with a path relative to the CSS file.

Give the element a size

Add a height, a minimum height, padding or an aspect ratio. aspect-ratio is a good choice for a banner that should keep its shape.


_10
.banner {
_10
background: url("/images/banner.webp") center / cover no-repeat;
_10
aspect-ratio: 16 / 9;
_10
}

Inspect the computed value

Select the element in the Elements panel and open the Computed tab. Search for background-image. If it says none, click the arrow next to it to see which rule won. A crossed-out declaration in the Styles pane means something else overrode it.

Order shorthand before longhand

If you mix the shorthand and individual properties, put the shorthand first.


_10
.panel {
_10
background: #f5f5f5;
_10
background-image: url("/images/texture.png"); /* not reset */
_10
}

Common pitfalls

#
  • Writing the path relative to the HTML file when the CSS lives in a subfolder.
  • Leaving out quotes around a URL that contains spaces or brackets. Quote every url() to be safe.
  • Using a background image for content that matters, such as a product photo. Use an img element with alt text instead, so screen readers and search engines can see it.
  • A Content Security Policy that blocks images from another domain. The Console shows a CSP error when this happens.
  • A dark overlay or gradient layered above the image with no transparency, which hides it completely.
#

Was this resource helpful?