css 背景图片

45 阅读1分钟

css的background-position默认是相对于左上角进行定位,但是其实可以相对于其它的位置进行定位,下面的demo就是相对于容器的右下角进行定位。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      div {
        width: 200px;
        height: 200px;
        background: pink;
        background-image: url(./1.png);
        background-repeat: no-repeat;
        /* 相对于右边定位 */
        background-position: bottom 10px right 10px;
        background-size: 100px 100px;
      }
    </style>
  </head>
  <body>
    <script></script>
    <div></div>
  </body>
</html>

image.png