CSS background-image Property

You are Here:

CSS background-image Property

CSS background-image property sets one or more background images on an element.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> body{ background-image: url("ocean.jpg"); color: #ffffff; } </style> </head> <body> <h1>CSS background-image Property</h1> <p>CSS background-image is repeated by default.</p> </body> </html>

Syntax

Using CSS

element{ background-image: url("ocean.jpg"); }

Using Javascript

object.style.backgroundImage="url('ocean.jpg')";

Animatable

No, background-image property is not animatable. CSS Animatable Properties Reference.

Default Value

Default value for CSS background-image property is none.

Property Value

The following table provides a list of values for CSS background-image property.

ValueExplanation
url('URL')Specifies the url to the image.
noneRemove or no background image.
linear-gradient()Specifies a linear gradient as the background image. Define at least two colors (top to bottom).
radial-gradient()Specifies a radial gradient as the background image. Define at least two colors (center to edges).
repeating-linear-gradient()Repeats a linear gradient.
repeating-radial-gradient()Repeats a radial gradient.

Using Multiple Background Images

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> body{ background-image: url("ocean.jpg"), url("apple.png"); background-repeat: no-repeat, repeat; color: #ffffff; } </style> </head> <body> <h1>CSS background-image Property</h1> <p>CSS supports multiple background-image</p> </body> </html>

Using Linear Gradient

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> body{ background-image: linear-gradient(gold, black); color: #ffffff; } </style> </head> <body> <h1>CSS background-image Property</h1> <p>CSS background-image to linear-gradient.</p> </body> </html>

Using Repeating Linear Gradient

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> body{ background-image: repeating-linear-gradient(90deg, red, green 7%, blue 10%); color: #ffffff; } </style> </head> <body> <h1>CSS background-image Property</h1> <p>CSS background-image to repeating-linear-gradient.</p> </body> </html>

Using Radial Gradient

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> body{ background-image: radial-gradient(gold, black); color: #ffffff; } </style> </head> <body> <h1>CSS background-image Property</h1> <p>CSS background-image to radial-gradient.</p> </body> </html>

Using Repeating Radial Gradient

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> body{ background-image: repeating-radial-gradient(circle, yellow 10%, green 15%); color: #ffffff; } </style> </head> <body> <h1>CSS background-image Property</h1> <p>CSS background-image to repeating-radial-gradient.</p> </body> </html>

Using JavaScript

In the following example, we will demonstrate how to remove the CSS background-image property of an element using JavaScript.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> body{ background-image: url("ocean.jpg"); color: #fff; } </style> </head> <body> <h1>CSS background-image Property</h1> <button onclick="myFunction()">Click Me</button> <script> var x = document.getElementsByTagName("body")[0]; function myFunction(){ x.style.backgroundImage = "none"; x.style.color = "black"; } </script> </body> </html>

Reminder

Hi Developers, we almost covered 98.7% of CSS Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in CSS.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author