Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to put a background image in HTML

The Concept of Background Image in HTML

When you visit a website, one of the first things that catch your eye is the background. It could be a solid color, a gradient, or most visually engaging, an image. This sets the tone for the entire website. As someone who's learning programming, you might wonder how you can also add a background image to your own HTML documents. This article will walk you through the process in a simple, jargon-free way.

Understanding CSS: The Stylist of the Web

Before we dive into the code, let's get our feet wet with a bit of theory. Cascading Style Sheets (CSS) is like a stylist for your HTML document. It decides how your webpage should look, from the color of the text to the size of the images, and yes, the background of your webpage.

Think of HTML as the bones and CSS as the skin of your webpage. HTML provides the structure, whereas CSS provides the style.

Now that you understand the role of CSS, let's see how we can use it to add a background image in HTML.

Adding a Background Image: The CSS Way

Here's a basic HTML document:

<!DOCTYPE html>
<html>
  <head>
    <title>My Webpage</title>
  </head>
  <body>
    <h1>Hello, world!</h1>
  </body>
</html>

To add a background image, we'll need to add some CSS. Let's add a style element inside our head element. We'll target the body element, which represents the content of our webpage, and use the background-image property to set our image.

<!DOCTYPE html>
<html>
  <head>
    <title>My Webpage</title>
    <style>
      body {
        background-image: url('my-image.jpg');
      }
    </style>
  </head>
  <body>
    <h1>Hello, world!</h1>
  </body>
</html>

In the above code, url('my-image.jpg') tells the browser to look for an image named 'my-image.jpg' in the same folder as the HTML file. If the image is in another folder, you would need to specify the path to that folder, like url('images/my-image.jpg').

Resizing and Positioning the Background Image

In the real world, when you put up a wallpaper, you ensure it covers the entire wall evenly. The same goes for background images. You don't want your image to repeat or to be too small or too large. This is where the background-size and background-position CSS properties come in handy.

The background-size property allows you to control the size of your background image. The value cover ensures your image covers the entire body of your webpage, resizing the image as needed.

The background-position property allows you to control the position of your background image. The value center ensures your image is centered on the page.

Here's how to use both properties:

<!DOCTYPE html>
<html>
  <head>
    <title>My Webpage</title>
    <style>
      body {
        background-image: url('my-image.jpg');
        background-size: cover;
        background-position: center;
      }
    </style>
  </head>
  <body>
    <h1>Hello, world!</h1>
  </body>
</html>

In the above code, background-size: cover; ensures the image covers the entire body and background-position: center; ensures the image is centered.

Ensuring the Image Doesn't Repeat

By default, if your image is smaller than your page, it will repeat horizontally and vertically to fill the page. This could look unappealing. To prevent this, use the background-repeat property and set its value to no-repeat.

<!DOCTYPE html>
<html>
  <head>
    <title>My Webpage</title>
    <style>
      body {
        background-image: url('my-image.jpg');
        background-size: cover;
        background-position: center;
        background-repeat: no-repeat;
      }
    </style>
  </head>
  <body>
    <h1>Hello, world!</h1>
  </body>
</html>

Conclusion

Adding a background image in HTML is like putting up wallpaper in the real world. You want it to cover the entire wall, be positioned correctly, and not repeat awkwardly. With the CSS properties background-image, background-size, background-position, and background-repeat, you can achieve just that.

Remember, practice makes perfect. The more you experiment with these properties, the more comfortable you'll get with them. So go on, pick an image and start decorating your webpage!