css-如何使用图像的一部分作为背景

问题描述 投票:1回答:1

我正在尝试将图像的一部分用作div的背景。

它适用于我的固定宽度的屏幕。

desired output

但是当我调整屏幕宽度的大小时,它将失去正确的位置,显示图像的其他部分。

behavior while resizing

有人可以帮忙吗?

HTML

<div class="game">
  <div class="slice top_estrada"></div>
</div>

CSS

.game{
  display:block;
  width:100%;
  .slice{
    display:block;
    width:100%;
    background-image:url('/assets/maps/tilemaps/mymap.png');
    background-repeat: no-repeat;
    background-size: 100% auto;
    &.top_estrada{
      background-position:0 -125px;      // <========= HERE
      height:88px;                       // <========= AND HERE
    }
  }
}

.game {
  display: block;
  width: 100%;
}

.game .slice {
  display: block;
  width: 100%;
  background-image: url('https://i.imgur.com/rdx8FHt.png');
  background-repeat: no-repeat;
  background-size: 100% auto;
}

.game .slice.top_estrada {
  background-position: 0 -125px; // <========= HERE
  height: 88px; // <========= AND HERE
}
<div class="game">
  <div class="slice top_estrada">
    top_estrada
  </div>
</div>
html css
1个回答
0
投票

background-size: 100% auto;替换background-size: center center;以实现该目的。

© www.soinside.com 2019 - 2024. All rights reserved.