Welcome to a tutorial on how to create a responsive fullscreen background image in HTML CSS. Need to add a background image that will look nice on both big and small screens?
Creating a responsive fullscreen background image is as easy as setting a background image to cover the entire window – body { width: 100vw; min-height: 100vh; background: url("IMAGE") center no-repeat; background-size: cover; }
.
That covers the quick basics, but read on for the example!
TABLE OF CONTENTS
DOWNLOAD & DEMO
Here is the download link to the example code, so you don’t have to copy-paste everything.
EXAMPLE CODE DOWNLOAD
The example code is released under the MIT license, so feel free to build on top of it or use it in your own project.
SORRY FOR THE ADS…
But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a “paid scripts” business, and I don’t “block people with Adblock”. Every little bit of support helps.
Buy Me A Coffee Code Boxx eBooks
RESPONSIVE BACKGROUND DEMO
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
RESPONSIVE FULLSCREEN BACKGROUND IMAGE
All right, let us now get into the examples of how to create responsive background images.
TUTORIAL VIDEO
PART 1) THE HTML
background.html
Responsive Background Image
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Yes, that’s all it takes to create a responsive full-page background image.
- We will be applying the background image onto
.
- Then,
will serve as the “foreground content”.
PART 2) THE CSS
background.css
/* (A) FILL ENTIRE WINDOW */
body, main {
width: 100vw;
min-height: 100vh;
}
/* (B) - BACKGROUND */
body {
/* (B1) REMOVE DEFAULT PAGE SPACING */
margin: 0; padding: 0;
/* (B2) BACKGROUND IMAGE */
background: url("city.webp") center no-repeat;
background-size: cover;
}
/* (C) - FOREGROUND */
main {
padding: 20px;
color: #fff;
background: rgba(0,0,0,0.5);
}
Just what is happening here?
- We set both
to
width: 100vw
andmin-height: 100vh
. This will make the page fill up the entire window by default. - Remember that
will serve as the background?
- (B1) First, use
margin: 0; padding: 0;
to remove the annoying default page spacing. - (B2)
background: url("city.webp") center no-repeat
Set the background image, centered on the page. - (B2)
background-size: cover
The background image will cover the entire page. You can also set this tocontain
, but it will not fill the entire page.
- (B1) First, use
- Finally,
will serve as foreground content. All we are doing here is setting the font and color “as usual”. Give it a transparent background so that the text can show up a little better.
LINKS & SUMMARY
Property | Description | Reference |
background-image
|
Set background image. | Link |
background-size
|
Set the size of the background image. | Link |
background-position
|
Set the position of the background image. | Link |
background-repeat
|
Repeat the background image. | Link |
INFOGRAPHIC CHEAT SHEET

THE END
Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!