HTML Background Images

HTML background images are images displayed behind the content of a webpage or a specific HTML element. They are commonly used for website banners, hero sections, landing pages, and decorative backgrounds. Unlike regular HTML images created with the <img> tag, background images are usually added using the CSS background-image property.

CSS provides several properties to control how background images appear, including their size, position, repetition, and attachment. These properties help developers create attractive and responsive website designs.

What Is an HTML Background Image?

An HTML background image is an image applied to an HTML element using CSS. It can cover an entire webpage, a section, a header, or a content container.

The background-image property specifies the image that appears behind an element’s content.

HTML Background Image Syntax

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

In this example, the browser uses background.jpg as the background image of the webpage.

HTML Background Image Example

The following example demonstrates how to add a background image to a webpage.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML Background Images</title>

    <style>
        body {
            background-image: url("background.jpg");
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            font-family: Arial, sans-serif;
            color: white;
        }
    </style>
</head>
<body>

    <h1>HTML Background Images</h1>
    <p>This webpage uses a background image.</p>

</body>
</html>

In this example, background-size: cover makes the image cover the available background area. background-position: center centers the image, while background-repeat: no-repeat prevents the image from repeating.

Save the code as index.html and place background.jpg in the same folder.

CSS Properties for Background Images

CSS provides several properties for controlling background images.

PropertyDescription
background-imageSpecifies the background image.
background-sizeControls the size of the background image.
background-positionSets the starting position of the image.
background-repeatControls whether the image repeats.
background-attachmentControls whether the background scrolls with the page or stays fixed relative to the viewport.
background-colorSpecifies a background color behind the image.

Using Background Size

The background-size property controls how an image fits within its background area.

.hero {
    background-image: url("banner.jpg");
    background-size: cover;
}

The cover value fills the entire background area, potentially cropping some parts of the image. Alternatively, contain scales the image so the entire image fits inside the background area, potentially leaving empty space.

Preventing Background Image Repetition

By default, CSS background images repeat when the image is smaller than the background area. You can prevent this behavior using background-repeat: no-repeat.

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

Other values include repeat-x for horizontal repetition and repeat-y for vertical repetition.

Adding a Background Image to a Section

You can apply a background image to a particular section instead of the entire webpage.

<section class="hero">
    <h2>Welcome to Web4learners</h2>
    <p>Explore tutorials and learn web development.</p>
</section>
.hero {
    background-image: url("hero.jpg");
    background-size: cover;
    background-position: center;
    padding: 100px 20px;
    text-align: center;
    color: white;
}

This technique is useful for creating hero sections and promotional banners.

Related HTML Tutorials

Continue learning about HTML images with these tutorials on Web4learners: