Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Set the size of background image using CSS ?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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>                    

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>                     

Output:


My Personal Notes arrow_drop_up
Last Updated : 15 Feb, 2019
Like Article
Save Article
Similar Reads
Related Tutorials