How to set background-image for the body element using CSS?

The CSS background-image property is used to set a background image for any HTML element, including the body element. Setting a background image for the body creates a full-page background that enhances the visual appeal of your website.

Syntax

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

Basic Example

Here's a simple example of setting a background image for the body element

<!DOCTYPE html>
<html>
<head>
<style>
    body {
        background-image: url('https://www.tutorialspoint.com/dip/images/einstein.jpg');
        background-repeat: no-repeat;
        background-size: cover;
        background-position: center;
        margin: 0;
        padding: 20px;
        font-family: Arial, sans-serif;
    }
    
    h1 {
        color: white;
        text-align: center;
        background-color: rgba(0, 0, 0, 0.5);
        padding: 20px;
        border-radius: 10px;
    }
</style>
</head>
<body>
    <h1>Welcome to TutorialsPoint</h1>
</body>
</html>
A webpage with Einstein's image as the full background, with a centered white heading that has a semi-transparent black background for better readability.

Common Background Properties

When setting background images, you'll often use these additional properties

Property Description Common Values
background-size Controls the size of the background image cover, contain, 100%
background-repeat Controls image repetition no-repeat, repeat, repeat-x
background-position Sets the position of the image center, top, left, right
background-attachment Controls scrolling behavior fixed, scroll

Example with Multiple Properties

This example demonstrates using multiple background properties together

<!DOCTYPE html>
<html>
<head>
<style>
    body {
        background-image: url('https://www.tutorialspoint.com/dip/images/einstein.jpg');
        background-size: contain;
        background-repeat: no-repeat;
        background-position: top right;
        background-attachment: fixed;
        height: 100vh;
        margin: 0;
        font-family: Arial, sans-serif;
    }
    
    .content {
        background-color: rgba(255, 255, 255, 0.9);
        padding: 30px;
        margin: 50px;
        border-radius: 15px;
    }
</style>
</head>
<body>
    <div class="content">
        <h2>Background Image Demo</h2>
        <p>This content has a semi-transparent white background, making it readable over the background image.</p>
    </div>
</body>
</html>
A webpage with the background image positioned at the top-right corner, not covering the entire page, with readable content in a semi-transparent white box.

Conclusion

The background-image property is an effective way to enhance your website's visual design. Use additional properties like background-size and background-repeat to control how the image appears and ensure your content remains readable.

Updated on: 2026-03-15T16:41:51+05:30

527 Views

Advertisements