CSS background Property

You are Here:

CSS background Property

CSS background property is a shorthand property of the following CSS properties

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> body{ background: lightgreen url("apple.png") no-repeat fixed center; height: 800px; } </style> </head> <body> <h1>CSS background Property</h1> </body> </html>

Syntax

Using CSS

element{ /* background: background-color background-image background-repeat background-attachment background-position; */ background: lightgreen url("apple.png") no-repeat fixed center; }

Using Javascript

object.style.background="lightgreen url('apple.png') no-repeat fixed center";

Animatable

Yes, background property is animatable. CSS Animatable Properties Reference.

Default Value

Default value for CSS background property is white none 0% 0% auto repeat padding-box border-box scroll.

Property Value

ValueTypeExplanation
background-colorRequiredSpecifies the background color to be used.
background-imageOptionalSpecifies the background image color to be used
background-positionOptionalSpecifies the position of the background images.
background-sizeOptionalSpecifies the size of the background images.
background-repeatOptionalSpecifies how to repeat the background images.
background-originOptionalSpecifies the position of the background images.
background-clipOptionalSpecifies how to clip the background-image.
background-attachmentOptionalSpecifies whether the image's position is fixed within the viewport, or scrolls with its containing block.

Using JavaScript

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

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> body{ background: lightgreen url("apple.png") no-repeat fixed center; height: 800px; } </style> </head> <body> <h1>CSS background Property</h1> <button onclick="myFunction()">Click Me</button> <script> var x = document.getElementsByTagName("body")[0]; function myFunction(){ x.style.background = "DodgerBlue url('orange.png') no-repeat fixed center"; } </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