How to Fit Background Image to Div using CSS? Last Updated : 10 Jan, 2025 Comments Improve Suggest changes Like Article Like Report To fit a background image to a div using CSS, you can utilize the background-size property. Here are some approaches:1. Using background-size: cover;This method scales the background image to cover the entire div, maintaining the image's aspect ratio. HTML <html> <head> <style> .background-cover { width: 300px; height: 200px; background-image: url('image.jpg'); background-size: cover; background-position: center; background-repeat: no-repeat; } </style> </head> <body> <div class="background-cover"></div> </body> </html> In this Example;The background-size: cover; property ensures the image covers the entire div, potentially cropping parts of the image to maintain aspect ratio.The background-position: center; centers the image within the div.2. Using background-size: contain;This method scales the background image to be fully visible within the div, maintaining the image's aspect ratio. HTML <html> <head> <style> .background-contain { width: 300px; height: 200px; background-image: url('image.jpg'); background-size: contain; background-position: center; background-repeat: no-repeat; } </style> </head> <body> <div class="background-contain"></div> </body> </html> In this Example;The background-size: contain; property ensures the entire image fits within the div, which may result in empty space if the aspect ratios differ.The background-repeat: no-repeat; prevents the image from repeating if it's smaller than the div.3. Using background-size with Specific DimensionsThis method sets the background image to specific dimensions within the div. HTML <html > <head> <style> .background-size { width: 300px; height: 200px; background-image: url('image.jpg'); background-size: 150px 100px; background-position: center; background-repeat: no-repeat; } </style> </head> <body> <div class="background-size"></div> </body> </html> In this Example;The background-size: 150px 100px; sets the background image to 150 pixels wide and 100 pixels tall.The background-position: center; centers the image within the div. Comment More infoAdvertise with us Next Article How to Fit Background Image to Div using CSS? T triashabiswas Follow Improve Article Tags : Web Technologies CSS Similar Reads How to Add filter to the background image using CSS? Adding filters to background images using CSS allows you to apply visual effects like blur, grayscale, brightness adjustment, and more without modifying the actual image file. CSS provides a set of filter functions that can be applied to elements with background images. This approach enhances design 3 min read How to transform background image using CSS3 ? In this article we will learn how to transform a background image using CSS3, The transform property in CSS is used to transform the background image. In Approach: The CSS transform property is used to apply the two-dimensional or three-dimensional transformation to an element. The transform propert 1 min read How to blur background image using CSS ? CSS (Cascading Style Sheets) is a language used to describe the appearance and formatting of a document written in HTML. In this article, we will learn how to blur background images using CSS, along with basic implementation examples. Blurring Background Images with CSSTo blur a background image usi 3 min read How to set multiple background images using CSS? The multiple background images for an element can be put in the HTML page using CSS. Use CSS background property to add multiple background images for an element in any pattern and use other CSS property to set the height and width of the images. The used background property are listed below: backgr 2 min read How to Set background Image using jQuery css() Method ? This article will show you how to set the background image using jQuery. To set the background image, we are using the jQuery css() method. jQuery css() MethodThis method sets/returns one or more style properties for the specified elements. Syntax: Return a CSS property:$(selector).css("propertyname 2 min read Like