Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to set an image as a background in HTML

Setting The Scene

Imagine you're moving into a new house and you're considering how to decorate your living room. You've got your furniture arranged, but now you need to decide on the paint or wallpaper. In a similar way, HTML allows you to set the 'decor' of your web page. The background-image property in CSS (Cascading Style Sheets) is the 'paint' or 'wallpaper' for your webpage. CSS is a language used to describe how HTML elements (the furniture) should be displayed.

The CSS Background-Image Property

Now, let's look at how we can use the background-image property. At its simplest, the background-image property is used like this:

<body style="background-image: url('image.jpg');">

In this code, we're using inline CSS within the HTML body tag to set the background image. The url('image.jpg') is the location of the image file we want to use. This file can be in the same directory as your HTML file, or it can be a web URL.

Adding The Image File

For the background-image property to work, the image file needs to be in the correct location. If the image file is not in the same directory as your HTML file, you need to provide the path to it. For example:

<body style="background-image: url('images/image.jpg');">

In this example, the image file is located in a folder named images which is in the same directory as the HTML file. If you are using an image from the web, you would use the image's URL, like so:

<body style="background-image: url('https://example.com/image.jpg');">

Customize Your Background Image

Just setting an image as background might not give the desired look. The image might be too small, too large, or not positioned as you want. This is where we can use other CSS properties to customize our background image.

The Background-Repeat Property

Think of background-repeat like a tile floor. If your image is smaller than the area you're trying to cover, CSS will automatically 'tile' it to fill the space. If you don't want this, you can set background-repeat: no-repeat;:

<body style="background-image: url('image.jpg'); background-repeat: no-repeat;">

The Background-Size Property

background-size is like choosing the size of your tiles. The cover value will scale your image to be as large as possible so that the background area is completely covered by the image. Some parts of the image may not be in view within the background positioning area:

<body style="background-image: url('image.jpg'); background-repeat: no-repeat; background-size: cover;">

The Background-Position Property

background-position is like deciding where on the wall you want your picture to hang. It sets the initial position for the background image. By default, it's positioned at the top left of the block. You can change it to whatever suits your design:

<body style="background-image: url('image.jpg'); background-repeat: no-repeat; background-size: cover; background-position: center center;">

Putting It All Together

Now that we've covered the basics, let's put everything together. We'll use the body tag to set the background image, and we'll use various CSS properties to adjust the image to our liking.

<body style="background-image: url('images/background.jpg'); background-repeat: no-repeat; background-size: cover; background-position: center center;">

This code sets background.jpg as the background image, ensures the image does not repeat, scales the image to cover the entire background, and centers the image both vertically and horizontally.

By using these properties, you can control how images are displayed as your webpage's background, allowing you to create visually appealing designs that enhance your content and engage your audience.

Conclusion

Setting an image as a background in HTML is a straightforward process but it's also a powerful tool. By understanding and using the different CSS properties associated with background images, you can create webpages that are visually engaging and unique. Remember, HTML and CSS are like decorating a house. The HTML is the structure and the furniture, while the CSS is the decor. Take the time to experiment with different images and CSS properties to find the design that's right for you.