Many times designers face an issue with centering background images using CSS. Here we will discuss how to center 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
Note: Change image path in background-image property.
Read Also: Bootstrap Vertical Align
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 🙂
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…