The background-size property is used to set the background image size using CSS. Use height and width property to set the size of the background image.
Syntax:
background-size: width height;
Note:
- If the first value is omitted then the image takes its original width.
- If the second value is omitted then the image takes its original height.
Example 1: This example sets the background-size into width and height.
<!DOCTYPE html> <html> <head> <title> Set the size of background image </title> <style> #myDiv { height: 200px; width: 400px; background: background-size: 400px 200px; } </style> </head> <body> <div id= "myDiv"></div> </body> </html> |
chevron_right
filter_none
Output:

Example 2: This example sets the background-size into percentage.
<!DOCTYPE html> <html> <head> <title> Set the size of background image </title> <style> #myDiv { height: 200px; width: 400px; background: /* Set the background-size in percentage */ background-size: 70%; background-repeat: no-repeat; } </style> </head> <body> <div id= "myDiv"></div> </body> </html> |
chevron_right
filter_none
Output:

Recommended Posts:
- CSS | background-image Property
- How to change the background image using jQuery ?
- How to Rotate Container Background Image using CSS ?
- How to Add Image in Text Background using HTML and CSS ?
- How to stretch and scale background image using CSS?
- JQuery | Setting background-image using CSS property
- How to Create a Wave Image for a Background using HTML and CSS ?
- CSS | Combine background image with gradient overlay
- How to give text or an image transparent background using CSS?
- CSS | background-size Property
- How to set the div height to auto-adjust to background size?
- How to get the image size (height & width) using JavaScript ?
- Difference between background and background-color
- Convert an image into grayscale image using HTML/CSS
- How to get the new image URL after refreshing the image using JavaScript ?
- How to set font size based on window size using JavaScript ?
- How to hide “Image not found” icon when source image is not found?
- CSS | Hue Background
- CSS | Background
- How to set the SVG background color ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

