Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to make a background image in HTML

Understanding HTML Background Images

To grasp the concept of background images in HTML, imagine you're painting a room. The wall paint essentially serves as your background. Similarly, in HTML, the background image is like the paint that covers your webpage. It resides behind all your content, providing a visual context.

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

In this analogy, the 'body' tag is like your wall, and the style attribute with the URL of your image is your chosen paint.

Using CSS for Background Images

HTML alone doesn't offer a lot of flexibility when it comes to background images. This is where Cascading Style Sheets (CSS) come in - they're like your toolkit for web design. CSS allows you to control the position, size, and repeat pattern of your background image.

Setting a Background Image

The CSS property 'background-image' allows you to set an image as the background. The image URL is placed within parentheses and single quotes after the colon.

body {
  background-image: url('image.jpg');
}

Positioning the Background Image

To control the position of your background image, use the 'background-position' property. This property uses keywords (top, bottom, left, right, center) or specific coordinates.

body {
  background-image: url('image.jpg');
  background-position: center top;
}

In this snippet, 'center top' positions the image at the center top of the body.

Repeating the Background Image

By default, a background image will repeat both vertically and horizontally to fill the area. The 'background-repeat' property allows you to control this repeat pattern.

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

In this snippet, 'no-repeat' ensures the image appears only once.

Resizing the Background Image

To resize your background image, use the 'background-size' property. It can take specific sizes in pixels, or keywords like 'cover' and 'contain'.

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

In this snippet, 'cover' scales the image to cover the entire content area.

Shorthand Property for Background

Now, imagine if you could tell a robot to paint your room with just one command. Similarly, CSS offers a shorthand property, 'background', that allows you to set all these properties with one line of code.

body {
  background: url('image.jpg') no-repeat center top / cover;
}

Using Multiple Background Images

CSS also allows you to use multiple background images. Imagine layering different shades of paint on your wall. You can create interesting visual effects by layering different images or patterns.

body {
  background: url('pattern.png') repeat, url('image.jpg') no-repeat center top / cover;
}

In this snippet, 'pattern.png' is the top layer that repeats across the body, and 'image.jpg' is the bottom layer positioned at the center top.

Conclusion

Creating a background image in HTML is like painting a wall, with CSS as your toolkit. You can set, position, repeat, resize, and layer your images to design a captivating webpage. Remember to experiment with different properties and values to create your unique design.