Responsive Full-Screen Background Image (Simple Example)

Welcome to a tutorial on how to create a responsive full-screen background image with HTML and CSS. Need to add a background image that will look nice on both big and small screens?

A simple way to create a responsive full-screen background image is to set a background image that covers the entire window – body { width: 100vw; min-height: 100vh; background: url("IMAGE"); background-size: cover; }.

That covers the quick basics, but read on for a few more examples!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

TLDR – QUICK SLIDES

Fullscreen Mode – Click Here

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

RESPONSIVE BACKGROUND DEMO

CITY LIFE

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

 

 

RESPONSIVE BACKGROUND IMAGE

All right, let us now get into the examples of how to create responsive background images.

 

PART 1) THE HTML

1-background.html
<!DOCTYPE html>
<html>
  <head>
    <title>Responsive Background Image</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="2-background.css">
  </head>
  <body>
    <main>
      <h1>CITY LIFE</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </main>
  </body>
</html>

Yes, that’s all it takes to create a responsive full-page background image.

  • We will be applying the background image onto <body> itself.
  • Then, <main> will serve as the “foreground content”.

 

 

PART 2) THE CSS

2-background.css
/* (A) FILL ENTIRE WINDOW */
body, main { width: 100vw; min-height: 100vh; }
 
/* (B) <BODY> - BACKGROUND */
body {
  /* (B1) REMOVE DEFAULT PAGE SPACING */
  margin: 0; padding: 0;
 
  /* (B2) BACKGROUND IMAGE */
  background-image: url("city.webp");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}
 
/* (C) <MAIN> - FOREGROUND */
main {
  padding: 20px;
  color: #fff;
  background: rgba(0,0,0,0.5);
}
 
/* (X) NOT REALLY IMPORTANT */
* {
  font-family: Arial, Helvetica, sans-serif;
  box-sizing: border-box;
}

Just what is happening here?

  1. We set both <body><main> to width: 100vw and min-height: 100vh. This will make the page fill up the entire window by default.
  2. Remember that <body> will serve as the background?
    • (B1) First, use margin: 0; padding: 0; to remove the annoying default page spacing.
    • (B2) background-image: url("city.webp") The background image.
    • (B2) background-size: cover The background image will cover the entire page. You can also set this to contain, specify a percentage or even exact pixels.
    • (B2) background-position: center Center the background image on the page.
    • (B2) background-repeat: no-repeat We do not want the background image to repeat itself but stretch to fill the page. Remove this if you are using a repeating pattern.
  3. Finally, <main> 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.

 

 

EXTRA BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

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

 

TUTORIAL VIDEO

 

INFOGRAPHIC CHEAT SHEET

Fullscreen Responsive Background Image (Click To Enlarge)

 

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!

1 thought on “Responsive Full-Screen Background Image (Simple Example)”

Leave a Comment

Your email address will not be published. Required fields are marked *