Languages
[Edit]
EN

CSS - center background image

0 points
Created by:
Aleena
694

In this article, we would like to show you how to center background-image using CSS.

Quick solution:

div {
    background-position: center;
}

 

Practical example

To center the background image, we need to set background-position property value to center.

Runnable example:

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    div {
        height: 100px;
        width: 100px;
        border: 1px solid black;

        /* background properties */
        background-image: url('https://dirask.com/static/bucket/1631898942509-VMYrnXyYZv--image.png');
        background-size: 50px;
        background-repeat: no-repeat;
        background-position: center;  /* center image */
    }

  </style>
</head>
<body>
  <div></div>
</body>
</html>

References

Alternative titles

  1. CSS - position image in middle
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join