CSS background-image Property
CSS background-image Property
CSS background-image
property sets one or more background images on an element.
Example
<!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.
Value | Explanation |
---|
url('URL') | Specifies the url to the image. |
none | Remove 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
<!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
<!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
<!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
<!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
<!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
<!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.
Meet the Author