How to Set a Bootstrap Background Image in Bootstrap 5: 3 Methods Explained

Bootstrap 5 provides a .bg-image utility class, defined in scss/_utilities.scss at line 695, that enables background images through inline styles, CSS custom properties, or utility combinations.

Bootstrap 5 includes a flexible background image utility system that lets you apply CSS background-image properties without writing custom CSS rules. According to the twbs/bootstrap source code, this utility is generated from the core utilities map and supports multiple implementation patterns for different use cases.

Understanding the Bootstrap Background Image Utility

The background image functionality originates in the Bootstrap utilities API. In scss/_utilities.scss at line 695, Bootstrap registers the background-image property in the utilities map. When the Sass is compiled, this generates the .bg-image class.

The .bg-image class itself does not set a specific image URL. Instead, it prepares the element to receive a background-image value through inline styles, CSS variables, or additional utility classes. This approach keeps background styling within Bootstrap's utility system, allowing you to toggle images on and off with a single class.

Method 1: Inline Style Approach

The simplest way to set a Bootstrap background image is to combine the .bg-image class with an inline style attribute.

<div class="bg-image" style="background-image:url('https://example.com/hero.jpg');">
  <!-- content -->
</div>

This method works immediately without custom CSS files. The .bg-image class ensures the background property is set, while the inline style provides the specific image URL.

Method 2: CSS Custom Property Approach

For reusable, maintainable code, define a custom class that sets the --bs-bg-image CSS variable. Bootstrap's background utilities read this variable when generating the final CSS.

First, create a custom class in your SCSS or CSS:

/* custom.scss */
.custom-hero {
  --bs-bg-image: url('https://example.com/hero.jpg');
}

Then apply both classes to your HTML element:

<div class="bg-image custom-hero">
  <!-- content -->
</div>

This approach separates content from presentation, making it easier to update image paths across your entire site by modifying a single CSS rule.

Method 3: Combining with Background Gradients

Bootstrap 5 allows you to layer background images with gradients using the .bg-gradient utility. According to scss/_variables.scss at line 398, the gradient utility leverages the --bs-gradient CSS variable.

To create an image with a gradient overlay:

<div class="bg-image bg-gradient" style="background-image:url('https://example.com/hero.jpg');">
  <!-- content -->
</div>

The .bg-gradient class applies the gradient defined in --bs-gradient over your background image, creating visual effects like overlays without additional CSS.

Responsive Background Images

To implement responsive background images using Bootstrap's standard breakpoints (768px, 992px), combine the .bg-image utility with custom CSS that swaps the image at different viewports.

<div class="bg-image responsive-hero">
  <!-- content -->
</div>
.responsive-hero {
  background-image: url('small.jpg');
}

@media (min-width: 768px) {
  .responsive-hero {
    background-image: url('medium.jpg');
  }
}

@media (min-width: 992px) {
  .responsive-hero {
    background-image: url('large.jpg');
  }
}

Alternatively, use SCSS with CSS custom properties to swap the --bs-bg-image variable value at different breakpoints.

Summary

  • The .bg-image utility is defined in scss/_utilities.scss (line 695) and generates the background-image property through Bootstrap's utilities API.
  • Three implementation methods exist: inline styles, CSS custom properties (--bs-bg-image), and utility combinations (pairing with .bg-gradient).
  • The .bg-gradient utility (referenced in scss/_variables.scss) uses --bs-gradient to add gradient overlays to background images.
  • For detailed documentation, refer to site/src/content/docs/utilities/background.mdx in the twbs/bootstrap repository.

Frequently Asked Questions

What is the Bootstrap 5 background image utility?

The Bootstrap 5 background image utility is a CSS class named .bg-image generated from the utilities map in scss/_utilities.scss. It registers the background-image property in Bootstrap's utility API, allowing developers to apply background images through inline styles or CSS variables while maintaining consistency with Bootstrap's design system.

How do I add a gradient overlay to a background image in Bootstrap?

Combine the .bg-image class with .bg-gradient. The .bg-gradient utility applies the --bs-gradient CSS variable (defined in scss/_variables.scss) over your background image. Use it like this: <div class="bg-image bg-gradient" style="background-image:url('image.jpg');"></div>.

Can I use the Bootstrap background image utility with responsive images?

Yes. While the .bg-image class sets the base property, you implement responsive behavior through CSS media queries that change the background-image URL or the --bs-bg-image variable at different breakpoints. Define responsive rules in custom CSS classes using Bootstrap's standard breakpoints (768px, 992px, 1200px).

Where is the background image utility defined in Bootstrap's source code?

The background image utility is defined in scss/_utilities.scss at line 695, where Bootstrap registers background-image in the utilities map. The related gradient functionality is documented in scss/_variables.scss (line 398), and official usage documentation appears in site/src/content/docs/utilities/background.mdx.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s https://instagit.com/install.md

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client