CSS background-size Property

You are Here:

CSS background-size Property

CSS background-size property sets the size of the element's background image.

Example

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

Syntax

Using CSS

element{ background-size: cover; }

Using Javascript

object.style.backgroundSize="cover";

Animatable

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

Default Value

Default value for CSS background-size property is auto.

Property Value

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

ValueExplanation
autoSets the background image in the corresponding direction such that its intrinsic proportions are maintained.
lengthSets the width and height of the background image. Negative values are not allowed.
percentageSets the width and height of the background image in percentage of the background positioning area. Negative values are not allowed.
coverSets the image as large as possible without stretching the image.
containSets the image as large as possible without cropping or stretching the image.

Using Length

In the following example, we will use pixel to adjust the size of the background image.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> body{ background-image: url("ocean.jpg"); background-repeat: no-repeat; background-size: 500px 200px; color: #ffffff; } </style> </head> <body> <h1>CSS background-size Property</h1> <p>CSS background-size to length</p> </body> </html>

Using percentage

In the following example, we will use percentage to adjust the size of the background image.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> body{ background-image: url("ocean.jpg"); background-repeat: no-repeat; background-size: 100% 150%; color: #ffffff; } </style> </head> <body> <h1>CSS background-size Property</h1> <p>CSS background-size to percentage</p> </body> </html>

Using cover

In the following example, we use background-size:cover to the image as large as possible without stretching the image.

Example

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

Using contain

In the following example, we use background-size:contain to set the image as large as possible without cropping or stretching the image.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> body{ height: 260px; background-image: url("ocean.jpg"); background-repeat: no-repeat; background-size: contain; color: #ffffff; } </style> </head> <body> <h1>CSS background-size Property</h1> <p>CSS background-size to contain</p> </body> </html>

Using JavaScript

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

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> body{ height: 260px; background-image: url("ocean.jpg"); background-repeat: no-repeat; background-size: contain; color: #ffffff; } </style> </head> <body> <h1>CSS background-size Property</h1> <button onclick="myFunction()">Click Me</button> <script> var x = document.getElementsByTagName("body")[0]; function myFunction(){ x.style.backgroundSize = "cover"; } </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