The Wayback Machine - https://web.archive.org/web/20240717024532/https://www.geeksforgeeks.org/how-to-fit-background-image-to-div-using-css/
Open In App

How to Fit Background Image to Div using CSS ?

Last Updated : 11 Jan, 2024
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

In this article, we will see how to fit background images into HTML div. The main problem that is being solved here is that the dimensions of the background image of a div are not exactly the same as the dimensions of the actual div, so there arises a mismatch. Due to this mismatch, the background image doesn’t fit properly in the div. Now, there are two terms among them one is fill and the other one is fit.

To fit a background image to a div using CSS, you can use the background-size property. Set it to the value cover to make the background image cover the entire div. Additionally, you may want to use background-position: center it to center the background image within the div. This combination ensures that the image scales to cover the div while maintaining its aspect ratio.

Syntax:

div{
background-size: contain;
}

Approach:

  • Create a HTML Structure with div container in your HTML with the appropriate class names for styling.
  • Now use the ‘background’ property in CSS to add the desired image to the div.
  • Define the width, height, and other styling properties for better styling
  • Now add ‘background-repeat: no-repeat;’ to ensure the repeatition of image foes not occur.
  • Apply ‘background-size: contain;’ to make the background image fit within the div.

Example 1: Implementation of the background-size: contain; to fit the background image to a div. To illustrate the effect there is one div and background image with it and then without it.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <title>
        Fit background image to div
    </title>
    <style>
        body {
            font-family: Verdana, Geneva, Tahoma, sans-serif;
        }
 
        .container {
            display: flex;
        }
 
        .container-1 {
            background: url(
            width: 250px;
            height: 300px;
            background-repeat: no-repeat;
            background-size: contain;
            border: 0.25rem solid black;
            margin-left: 10%;
            margin-top: 5%;
            margin-right: 10%;
        }
 
        .container-2 {
            background: url(
            border: 0.25rem solid black;
            width: 250px;
            margin-top: 5%;
            height: 300px;
        }
    </style>
</head>
 
<body>
    <h1 style="color: green;
               margin: 1rem">
        GeeksforGeeks
    </h1>
    <h3 style="margin: 1rem;
               margin-top: -1rem">
        Fit background image to div
    </h3>
    <div class="container">
        <div class="container-1"></div>
        <div class="container-2"></div>
    </div>
</body>
 
</html>


Output:

Example 2: Implementation of difference between the fill and fit of the background image in the div using the background-size: contain; and background-size: contain; properties respectively.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <title>
          Fit background image to div
      </title>
    <style>
        body {
            font-family: Verdana, Geneva, Tahoma, sans-serif;
        }
 
        .container {
            display: flex;
        }
 
        .container-1 {
            background: url(
            width: 250px;
            height: 300px;
 
            background-repeat: no-repeat;
            background-size: contain;
 
            border: 0.25rem solid black;
            margin-left: 10%;
            margin-top: 5%;
            margin-right: 6%;
        }
 
        .container-2 {
            background: url(
            border: 0.25rem solid black;
            width: 250px;
            margin-top: 5%;
            height: 300px;
            background-size: cover;
        }
 
        p {
            margin-left: 10%;
        }
    </style>
</head>
 
<body>
    <h1 style="color: green;
               margin: 1rem">
          GeeksforGeeks
      </h1>
    <h3 style="margin: 1rem;
               margin-top: -1rem">
        Fit background image to div
    </h3>
    <div class="container">
        <div class="container-1"></div>
        <div class="container-2"></div>
    </div>
    <div class="container">
        <p>with
              <code>background-size: contain;</code>
          </p>
        <p>with
              <code>background-size: cover;</code>
          </p>
    </div>
</body>
 
</html>


Output:



Similar Reads

How to auto-resize an image to fit a div container using CSS?
To resize an image or video to fit inside a div container, you can use the object-fit property. This property is used to specify how the content should fit inside the container. With object-fit, you can set the content to maintain its aspect ratio or stretch to fill the entire container. This allows you to control how the content is displayed withi
3 min read
How to Set Div Width to Fit Content using CSS?
The div can be managed to set the whole screen width or the width according to the need. In this article, we are going to discuss how can we set a div width to fit content using CSS. we will discuss different approaches and code examples. Below are the approaches to set div width to fit content using CSS: Table of Content Using Default CSSUsing inl
3 min read
How to overlay one div over another div using CSS
Creating an overlay effect simply means putting two div together at the same place but both the div appear when needed i.e while hovering or while clicking on one of the div to make the second one appear. Overlays are very clean and give the webpage a tidy look. It looks sophisticated and is simple to design. Overlays can create using two simple CS
2 min read
How to Make a Child Div Element Wider than Parent Div using CSS ?
When the child div element has a fixed width that is wider than the parent div element, it will display overflow outside of the parent element. Because the overflow is hidden by default, one approach is to utilize the overflow attribute to enable it on an element. Another approach includes setting the position property of the parent div element to
2 min read
How to stretch div to fit the container ?
A child div inside a container can be made to take the complete width and height of the parent div. There are two methods to stretch the div to fit the container using CSS that are discussed below: Method 1: The first method is to simply assign 100% width and 100% height to the child div so that it will take all available space of the parent div. E
2 min read
How to get a div's background image with jQuery ?
The task is to get a div's background image with jQuery. You can achieve this task by using the CSS() method in jQuery. In simple words, we need to create an empty div and on click event, the empty div will get the image as a background. CSS(): This is an inbuilt method in jQuery to add the CSS property but in this case, we need to provide complete
2 min read
Image inside div has extra space below the image in CSS
Sometimes when you insert an image inside a div element, you might notice that there is extra space below the image. This can happen due to various reasons such as the image having a different aspect ratio than the div, or the div having padding or margin that affects the image placement. In this article, we will discuss different approaches to sol
5 min read
How to copy the content of a div into another div using jQuery ?
Given an HTML document containing some div element and the task is to copy a div content into another div as its child using jQuery. There are two approaches to solve this problem that are discussed below: Approach 1: First, select the div element which needs to be copied into another div element.Select the target element where the div element is c
2 min read
How to force child div to be 100% of parent div's height without specifying parent's height?
When designing a website, you may want to create a layout where a child div element fills up the height of its parent div element. However, you may not know the exact height of the parent div element and you don't want to specify it. In this article, we will explore different approaches to force a child div element to be 100% of its parent div elem
3 min read
How to center a div within another div?
The div tag is used to construct a division or section of an HTML document in which other elements of HTML is placed and that division/section works like a container whose CSS styling can be done as a single unit or javascript can be used to perform various tasks on that container. Syntax: &lt;div&gt; &lt;h3&gt;'This is a div of the web page.&gt;/h
4 min read
Article Tags :
three90RightbarBannerImg