Background Image

Bootstrap 5 Background Image

Adding a background image can be troublesome. Learn how to seamlessly set a Background image in Bootstrap along with advanced tricks that will allow you to create amazing projects with unconventional design.

Also, try these ready-to-use templates to see how you can implement the background image in a real project.


Basic example

This is a basic example of a full-page background image. If you need a background image with different settings have a look at other sections of this docs.

  1. Add background-image via inline CSS.
  2. Define the background height. In the example below we use vh units, which stands for "viewport height" (height: 100vh means 100% of available height.)
  3. Add .bg-image class to scale the image properly and to enable responsiveness
Demo

How it works

You can easily set the background image in each HTML element by adding a single line of CSS:

style="background-image: url('');

Inside of the url('') we need to provide a link to our image.

If you want to use the image on your computer, the path should look like this:

Path Description
img src="picture.jpg" The "picture.jpg" file is located in the same folder as the current page
img src="images/picture.jpg" The "picture.jpg" file is located in the images folder in the current folder
img src="/images/picture.jpg" The "picture.jpg" file is located in the images folder at the root of the current web
img src="../picture.jpg" The "picture.jpg" file is located in the folder one level up from the current folder

You can also use an absolute path and add a link to the image directly from the Internet.

Then we just need to add this CSS line to the HTML element.

However, despite this, our picture does not appear. Take a look at the demo:

Demo

That's because we need to provide a height to this HTML element. Let's add height: 400px; to set a height.

Now it works ... partially. We see the picture, but it is cut and unsightly.

Click on the images to launch the live demo.

Bootstrap Background Image

Fortunately, there is a class in MDBootstrap that fixes this issue. Just add .bg-image to the class and you will see the problem magically disappear.

Bootstrap Background Image

Full page background image

Now we can easily make this background image to cover the full available space and make it a full-page background image.

Just replace height: 400px;withheight: 100vh;

vh stands for viewport height.

height: 100vh; means 100% of available height.

Demo

Note: If you want to stretch the image to the full available height and width remember to use the image with enough high resolution. However, be careful not to overdo it. Heigh-resolution images weigh a lot and can slow down your website.

Demo

Half page background image

You can also stretch the background image half page (or any other percentage). Just replace height: 100vh" withheight: 50vh"

Demo

This is a very useful design that is often used in many landing pages.

Demo

Jumbotron with background image

In bootstrap 5 there is no dedicated jumbotron component, but it's not a problem at all. You can easily create your own component by using available classes.

Bootstrap Jumbotron Bacckground Image

Jumbotron

Lorem ipsum dolor, sit amet consectetur adipisicing elit. Repellendus praesentium labore accusamus sequi, voluptate debitis tenetur in deleniti possimus modi voluptatum neque maiores dolorem unde? Aut dolorum quod excepturi fugit.


Cards with background image

Using the same technique as with the jumbotron, we can easily create cards with an image in the background.

Bootstrap Cards with Background Images
Card title

Some quick example text to build on the card title and make up the bulk of the card's content.

Button

Centering content

An important part of using components with background images is content alignment. In most cases, we need to center the content vertically and horizontally. The best way to do this is to use flexbox.

Add.d-flex to .bg-image to enable flexbox. Then add .justify-content-center to align content horizontally and.align-items-center to align them vertically.

To learn more about flexbox have a look at our Flexbox docs. You can also use our Flexbox generator to set up the desired flexbox settings.

Center Bootstrap Background Image Content

Page title


Masks

Rarely does the background image alone provide sufficient contrast for the content to be clearly visible. That's why we use masks.

Masks alter the visibility of an element by either partially or fully hiding it. Masks are used to make content more visible by providing a proper contrast.

To learn more about masks have a look at our Masks docs. You can also use our Masks generator to create the desired mask.

Bootstrap Image Mask

How it works:

  • Masks require .bg-image wrapper which sets a position to relative, overflow to hidden, and properly center the image.
  • The inside .bg-image wrapper as the first child we place an img element with the source link
  • Below is the actual mask. We set a color and opacity via rgba code and inline CSS.
  • If you want to put a text on the image you have to place it within the .mask wrapper. To center it you have to use flex utilities .
Bootstrap Mask

Page title

By manipulating RGBA code you can change the color and opacity of the mask.

Bootstrap 5 Mask

Page title


Gradients

You can also use our Gradient generator to apply stunning gradients to the background image.

Bootstrap Gradient Mask

Page title


Instagram

I suppose you've heard of the famous Instagram filters. Thanks to our Instagram filter generator you can apply them to your background image.

Bootstrap Instagram Mask

SVG

Are you looking for something even more extravagant? Use our SVG wave generator and apply vector shapes to the background image.

SVG Mask Bootstrap 5

Tools and resources

A list of useful resources and tools.