【问题标题】:When I try to repeat x an image sprite, it shows the entire image instead当我尝试重复 x 一个图像精灵时,它会显示整个图像
【发布时间】:2019-04-09 03:42:30
【问题描述】:

我正在尝试从这张图片(pic 2)中重复这部分(pic 1 是箭头位)。但是,它不需要重复图像的那个端口。它只显示整个图像。我该如何解决这个问题(并且仍然继续使用图像精灵)。

图片精灵的宽度为134px,高度为44px

body {
    padding: 0;
    margin: 0;
}

.test-box {
    position: relative;
    display: inline-block;
    width: auto;
    height: 14px;
    line-height: 14px;
    font-family: arial;
    font-size: 9px;
    color: #000;
    background: url("https://www.w3schools.com/css/img_navsprites.gif") -84px -18px repeat-x;
}
<div class="test-box">this is a text message</div>

如果我要使用普通图像(不是图像精灵),那么它可以工作。但我想使用图像精灵。例如,当它不是图像精灵时它可以工作

body {
    padding: 0;
    margin: 0;
}

.test-box {
    position: relative;
    display: inline-block;
    width: auto;
    height: 14px;
    line-height: 14px;
    font-family: arial;
    font-size: 9px;
    color: #000;
    background: url("https://i.imgur.com/PhEZLTm.png") repeat-x;
}
<div class="test-box">this is a text message</div>

【问题讨论】:

  • @cjl750 不起作用。在我提出这个问题之前已经看过这篇文章
  • 这个问题的答案的重点是,纯粹用 CSS 做你想做的事是不可能的;您必须以特定的方式设置您的图像精灵才能做到这一点。您在此示例中使用的图像将不起作用,因为它没有设置为允许重复。

标签: html css background repeat


【解决方案1】:

这是因为你的图像太大了。打开查看https://www.w3schools.com/css/img_navsprites.gif

您可以将属性background-size 设置为普通的background-position-xbackground-position-y,并且您的图像会重复

【讨论】:

  • 你能给我看一下代码形式的例子吗?我编辑了我的帖子并输入了图像精灵的大小(以像素为单位)。 Width = 134pxheight = 44px
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-20
  • 2019-02-20
  • 1970-01-01
  • 2017-12-05
  • 2011-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多