How To

How to Center Background Images in Div Tag With CSS

Many times designers face an issue with centering background images using CSS. Here we will discuss how to center background images using CSS.

Read Also: Onhover Effect With Inline CSS and JavaScript

Centering Background Images Using CSS

Many times we face a problem to fit different size images in a fixed size container. In the fixed-size container, it is hard to manage background image; an image may overlap, cut, or may overflow.

To solve that problem, here we are going to learn how to center a background image in a div element with CSS. This method contains the usage of background-size and background-position to center background image in CSS.

Example

<!DOCTYPE html>
<html>
   <head>
      <title>Center Background Images Using CSS - Errorsea</title>
      <style>
         .background{
         height: 200px;
         background-image: url(YOUR_IMAGE_PATH);
         width: 200px;
         display: block;
         background-position: center;
         background-size: cover;
         }
      </style>
   </head>
   <body>
      <div class="background"></div>
   </body>
</html>

Explanation

  • First, we create a fixed 200*200 PX div container with background class to contain the background image.
  • Then, we set a background image with a background-image property.
  • After that, we position the image center with a background-position property.
  • Finally, we set the cover size to best fit the image in a container with the background-size property.

Note: Change image path in background-image property.

Read Also: Bootstrap Vertical Align

Conclusion

I hope you understand how to center an image for fixed-size containers with background property. You can copy the above code and test it your self.

Enjoy Designing 🙂

Nachiket Panchal

Founder & Administrator of `errorsea` Having interest in Programming & Technology.

Recent Posts

5 Important Things To Know About WordPress Before You Use It

There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…

3 years ago

How to Install MySQL on Your PC in 3 Easy Steps

In this tutorial, I'm going to show you how to install MySQL on your computer.…

5 years ago

Download and Install Turbo C++ for Windows 10 (Full Installation Guide)

Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…

5 years ago

PHP .HTACCESS Redirects

We can redirect any webpage to any other or redirect the whole domain or website…

5 years ago

PHP Redirect Pages

There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…

5 years ago

PHP Include & Required

Include files in PHP are used in appending various global or config files. We can…

5 years ago