Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add a background image in HTML

Understanding HTML and Background Images

Before we dive into the world of HTML and background images, it's important to note that HTML, or HyperText Markup Language, is a standard language for creating web pages and web applications. And when we talk about a background image, we refer to a picture or graphic that appears behind the content on a website.

You can think of HTML as the skeleton of a webpage and the background image as the wallpaper of a room. The skeleton provides the structure, while the wallpaper gives the room its aesthetic appeal.

Now, let's get started with adding a background image to a webpage.

Setting Up Your HTML File

Before adding a background image, you need to have an HTML file. If you haven't already, create a new file in your text editor and save it as index.html. Your file should look something like this:

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

This is a very basic HTML file that displays a heading saying, "Hello, World!".

Adding a Background Image Using CSS

While HTML provides the structure of a webpage, CSS, or Cascading Style Sheets, is used to style the webpage. It's like the paint and furniture in our room analogy. To add a background image, we'll use CSS.

First, we need to add a style tag within the head element. This is where we'll write our CSS. It should look something like this:

<head>
    <title>My First Webpage</title>
    <style>

    </style>
</head>

Next, we need to select the body element because we want the background image to cover the entire page. We'll do this by typing body inside the style tag.

<style>
    body {

    }
</style>

Now, we can add the background image itself. We'll use the background-image property and the url() function in CSS. The url() function allows us to specify the location of the image.

<style>
    body {
        background-image: url('path-to-your-image.jpg');
    }
</style>

Replace 'path-to-your-image.jpg' with the actual path to the image you want to use. If your image is in the same directory as your HTML file, you just need to include the image's filename and extension. If it's in a different directory, you'll need to include the path to that directory.

Modifying the Background

Now that we have our background image, we might notice that it doesn't look quite right. It might be repeating, or it might not cover the entire page. We can fix these issues with some additional CSS properties.

To prevent the image from repeating, we can use the background-repeat property and set it to no-repeat.

<style>
    body {
        background-image: url('path-to-your-image.jpg');
        background-repeat: no-repeat;
    }
</style>

To make the image cover the entire page, we can use the background-size property and set it to cover.

<style>
    body {
        background-image: url('path-to-your-image.jpg');
        background-repeat: no-repeat;
        background-size: cover;
    }
</style>

The cover value scales the image as large as possible without stretching it or leaving any whitespace. It's like choosing a piece of wallpaper that's just the right size for our room.

Conclusion

And that's it! You now know how to add a background image to a webpage using HTML and CSS. Remember, while it's important to make your website look good, it's equally important to ensure that the content is readable and accessible to all users. So, choose your background images wisely.

In upcoming blogs, we'll dive deeper into CSS and explore more ways to style your webpage. Stay tuned!