Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to set background image in HTML

Understanding the Basics

Before we dive into setting a background image in HTML, let's first understand what HTML is. HTML, short for Hyper Text Markup Language, is the standard markup language used in creating web pages. In simpler terms, it's like the skeleton of a webpage.

Think of HTML like the building blocks of a house. Just as you need bricks, cement, and other materials to build a house, you need HTML to build a webpage. Now, let's say you want to paint a wall of your house with a certain design. Setting a background image in HTML is analogous to that.

Setting Up the HTML Page

Ready to paint that wall? Let's start by setting up a basic HTML page. Here's a simple HTML page structure:

<!DOCTYPE html>
<html>
<head>
    <title>Page Title</title>
</head>
<body>
    <h1>This is a Heading</h1>
    <p>This is a paragraph.</p>
</body>
</html>

The <body> tag is where we will be setting the background image. It's like the wall we want to paint.

Setting a Background Image in HTML

Alright, let's get our paint (image) ready! To set a background image in HTML, we use the CSS background-image property. CSS, short for Cascading Style Sheets, describes how HTML elements are to be displayed on screen. Imagine it as the paintbrush that applies the paint (image) to our wall (HTML page).

Here's how to do it:

<!DOCTYPE html>
<html>
<head>
    <title>Page Title</title>
    <style>
    body {
        background-image: url('image.jpg');
    }
    </style>
</head>
<body>
    <h1>This is a Heading</h1>
    <p>This is a paragraph.</p>
</body>
</html>

In the above code, we have a new <style> tag section within the <head> tag. This is where we write our CSS. We're telling the browser to paint the body of our HTML page with the image 'image.jpg'.

Understanding the Code

In the CSS block, body is the selector. Think of it as us pointing to the specific wall we want to paint. background-image is the property, which is like the type of paint we're using. The url('image.jpg') is the value, which is the specific paint color or design we're applying.

It's important to note that the image file ('image.jpg') should be in the same directory (folder) as your HTML file. If it's in a different directory, you would have to specify the path to the image file, like url('images/image.jpg').

Controlling the Background Image Display

Now, if our image is too small, it might not cover the entire wall, right? Similarly, if the size of the background image is smaller than the size of the HTML page, the image will repeat itself until it fills the entire page. This is the default behavior.

But what if we don't want the image to repeat? Or what if we want to perfectly fit the image to our page? CSS provides properties for that!

No Repeat

To prevent the image from repeating, we can use the background-repeat property and set its value to no-repeat. Here's how:

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

Cover

To make the background image cover the entire page, we can use the background-size property and set its value to cover. Here's how:

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

Conclusion

So, that's how you set a background image in HTML! It's like painting a wall. You choose your wall (the HTML element, in our case, the body), you pick your paint (the image), and then you paint it (apply the CSS).

Remember, practice is key in programming. Try setting different images as the background and play around with the background-repeat and background-size properties to get a feel of how they work.

Happy coding!