Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add background image in HTML

Getting Started

When you're creating a website, one way to make it visually appealing is to add a background image. In this blog post, we're going to walk through the process of adding an image to the background of a webpage using HTML and CSS - two fundamental technologies used in building web pages.

HTML and CSS: A Quick Recap

HTML (HyperText Markup Language) is the standard language for creating web pages. It provides the structure of a page, like the skeleton in our body. CSS (Cascading Style Sheets), on the other hand, is used for styling the HTML elements - it's like the skin and clothes that give us our appearance.

Adding a Background Image: The Basics

Let's start off with the simplest way to add a background image to your page. You can do this by using the background-image property in CSS. Here's a simple example:

<!DOCTYPE html>
<html>
<head>
<style>
body {
    background-image: url('your-image.jpg');
}
</style>
</head>
<body>
</body>
</html>

In the code above, we've set the background-image property of the body element to an image located at 'your-image.jpg'. Think of the body as the canvas for your webpage. So, setting a background image for the body would be equivalent to painting the entire canvas with your image.

Full Page Background Image

If you want the image to cover the entire webpage, regardless of the device size or screen resolution, you can add a few more CSS properties to your code. Here's what it looks like:

<!DOCTYPE html>
<html>
<head>
<style>
body {
    background-image: url('your-image.jpg');
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
}
</style>
</head>
<body>
</body>
</html>

Let's break this down:

background-position: center; This line centers the image on the page. It's like hanging a picture right in the middle of a wall.

background-repeat: no-repeat; This line ensures that the image doesn’t repeat itself. Without it, the image would tile across the page, like a patterned wallpaper.

background-size: cover; This line makes the image scale and crop to cover the entire page, just like stretching a rubber image to cover the whole wall.

Tips for Choosing a Background Image

When selecting a background image, remember:

The image should not distract from the content of your webpage. It's like choosing a wall paint color that makes your pictures stand out, not overshadow them.

Consider using a low-contrast image. This will make the text easier to read.

Consider the file size of the image. Larger files will take longer to load, which can slow down your website. It's like trying to carry a heavy suitcase up a flight of stairs - the heavier it is, the slower you'll go.

Conclusion

Adding a background image to your webpage can greatly enhance its visual appeal. By using HTML and CSS, you can not only add a background image, but also control its positioning and size. Remember to choose your image wisely to ensure it complements your webpage's content and doesn't slow down your site. Happy coding!