The Wayback Machine - https://web.archive.org/web/20250109042028/https://www.geeksforgeeks.org/css-background-image-property/
Open In App

CSS background-image Property

Last Updated : 15 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The background-image property allows you to set one or more background images for an element. It allows you to specify the image URL and combine it with other background properties to control its position, size, repeat behavior, and more, enhancing the visual design of web pages.

Syntax

element_selector {
    background-image: url('image_url')| none | initial | inherit;
}

Property Values

Values

Description

url(‘url’)

This specifies the URL of the image. To specify the URL of more than one image then separate the URLs using a comma.

none

This is the default case where no image can be displayed.

initial

It is used to set the property to its default value.

inherit

It inherits the property from its parent element.

The background-image property can also be used with the following values:

Values

Descriptions

linear-gradient()

It is used to set the linear-gradient background-image that is defined at least 2 color from top to bottom.

radial-gradient()

It is used to set the radial-gradient background-image that is defined at least 2 color from center to edge.

Basic Example

This example illustrates the background-image property by setting the url value as url.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>background-image property</title>
    <style>
        body {
            background-image: url("https://media.geeksforgeeks.org/wp-content/uploads/rk.png");
            text-align: center;
        }

        h1,
        h3 {
            color: green;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <h3>background-image:url;</h3>
    <div>
        GeeksforGeeks: A computer science portal for geeks
    </div>
</body>

</html>

Output:

background-image-basic-example

Basic Example

CSS background-image Property Examples

Example 1: Background-image example with background position center and no repeat.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Align Items Example</title>
    <style>
        .image-prop {
            background-image: url(
"https://media.geeksforgeeks.org/wp-content/uploads/20240722155354/geeksforgeeks3.png");
            background-position: center;
            background-repeat: no-repeat;
            width: 30rem;
            height: 25rem;
            background-color: bisque;
        }
      h1,h3 {
                color: black;
                text-align: center;
            }
    </style>
</head>

<body>
    <div class="image-prop">
        <h1>GeeksforGeeks</h1>
        <h3>background-image:url;</h3>
    </div>
</body>

</html>

Output:

background-image-property-example-1

CSS background-image Property Example

Example 2: Background-image example with background position center, no repeat and cover.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Align Items Example</title>
    <style>
        .image-prop {
            background-image: url(
"https://media.geeksforgeeks.org/wp-content/uploads/20240722155354/geeksforgeeks3.png");
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            width: 30rem;
            height: 27rem;
            background-color: bisque;
        }

        h1,
        h3 {
            color: black;
            text-align: center;
        }
    </style>
</head>

<body>
    <div class="image-prop">
        <h1>GeeksforGeeks</h1>
        <h3>background-image:url;</h3>
        <h3>background-position: center;</h3>
    </div>
</body>

</html>

Output:

background-image-property-example-2

CSS background-image Property Example

Example 3: Background Image with none, This example illustrates the background-image property by setting the url value as none.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>background-image property</title>
    <style>
        body {
            background-image: url("https://media.geeksforgeeks.org/wp-content/uploads/rk.png") none;
        }

        h1,
        h3 {
            color: green;
        }

        body {
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <h3>background-image:url none;</h3>
    <div>
        GeeksforGeeks: A computer science portal for geeks
    </div>
</body>

</html>

Output:

background-image-property-example-3

CSS background-image Property Example

Example 4: Background-image property by setting the url value as initial.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>CSS background-image property</title>
    <style>
        body {
            background-image: url("https://media.geeksforgeeks.org/wp-content/uploads/rk.png") initial;
        }

        h1,
        h3 {
            color: green;
        }

        body {
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <h3>CSS background-image:url initial;</h3>
</body>

</html>

Output:

background-image-property-example-4

CSS background-image Property Example

Supported Browsers

The browser supported by background-image Property are listed below: 

CSS background-image Property – FAQs

What is the background-image property in CSS?

The background-image property in CSS is used to set one or more background images for an element. These images can be URLs, gradients, or even data URIs.

When to use background image CSS?

If you need to display only a portion of an image, as in the case of CSS sprites, consider using the background-image property in conjunction with the background-size and background-position properties.

What’s the difference between background-image and img elements?

The background-image property is for decorative images and can be layered with other content, while the <img> element is used for images that are part of the content, such as photos or illustrations that convey important information.

Why is my background-image not showing up?

Common reasons include incorrect URL paths, missing files, or the image being hidden behind other content. Also, ensure that the element has a height and width set, as background images won’t display if the container is collapsed.



Next Article

Similar Reads

three90RightbarBannerImg