Languages
[Edit]
EN

CSS - center background image

0 points
Created by:
Aleena
334

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

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