html - 为什么我的 CSS Sprites 没有被应用?

标签 html css css-sprites

我在我的代码中使用了 CSS Sprite 。以下是我使用的背景图片。

enter image description here

在 HTML 中,我使用 span 根据整数值(计数)动态生成图标,并且应向用户显示与该计数对应的图标。

.molecularmatch-icon-image {
  width: 29px;
  height: 29px;
  line-height: 29px;
  display: block !important;
}

.molecularmatch-icon-image.count0 {
  background-position: -5px -0px;
}

.molecularmatch-icon-image.count1 {
  background-position: -34px -0px;
}

.molecularmatch-icon-image.count2 {
  background-position: -34px -34px;
}

.molecularmatch-icon-image.count3 {
  background-position: -63px -34px;
}

.molecularmatch-icon-image.count4 {
  background-position: -5px -68px;
}

.molecularmatch-icon-image.count5 {
  background-position: -34px -68px;
}

.molecularmatch-icon-image.count6 {
  background-position: -63px -68px;
}

.molecularmatch-icon-image.count7 {
  background-position: -5px -102px;
}

.molecularmatch-icon-image.count8 {
  background-position: -34px -102px;
}

.molecularmatch-icon-image.count9 {
  background-position: -63px -102px;
}

.molecularmatch-icon-image.count10 {
  background-position: -63px -0px;
}

.molecularmatch-icon-image.countexceed-10 {
  background-position: -5px -34px;
}
<span>
  <span class="annotation-module__annotation-item__1DwWp">
  <i class="molecularmatch-icon-image count7" 
  data-test="molecularmatch-icon-image" style="background-image: url(/image/nTR2u.png);">
  </i>
  </span>
</span>

但是,当在浏览器上运行时,只有对应于 molecularmatch-icon-image rule 的 CSS 规则才会应用到图标上。结果,所有图标都显示 Sprite 中的初始图标(计数 0)。当我添加第二条规则时

.molecularmatch-icon-image.count7 {
  background-position: -5px -102px;
}

使用浏览器的开发者工具似乎可以正常工作。谁能告诉我我在这里做错了什么?

最佳答案

您没有在 CSS 中使用图像,并且计数类的使用方式略有错误。请看下面。

.molecularmatch-icon-image {
  width: 29px;
  height: 29px;
  line-height: 29px;
  display: block;
  background-image: url('/image/nTR2u.png');
  background-position: 0 0;
}

.count0 {
  background-position: -5px -0px;
}

.count1 {
  background-position: -34px -0px;
}

.count2 {
  background-position: -34px -34px;
}

.count3 {
  background-position: -63px -34px;
}

.count4 {
  background-position: -5px -68px;
}

.count5 {
  background-position: -34px -68px;
}

.count6 {
  background-position: -63px -68px;
}

count7 {
  background-position: -5px -102px;
}

.count8 {
  background-position: -34px -102px;
}

.count9 {
  background-position: -63px -102px;
}

.molecularmatch-icon-image.count10 {
  background-position: -63px -0px;
}

.molecularmatch-icon-image.countexceed-10 {
  background-position: -5px -34px;
}
<span class="molecularmatch-icon-image count7"></span>
<i class="molecularmatch-icon-image count5"></i>

关于html - 为什么我的 CSS Sprites 没有被应用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45252062/

相关文章:

html - Bootstrap 侧边栏在添加内容时更改大小

html - 全宽导航栏,居中,带内容区域

css - 在 Chrome 上缩放时,图像 Sprite 变得不对齐

html - 高度宽度百分比 css html 一些奇怪的东西

audio - 使用 html5 `<audio>` 标签流式传输音频

javascript - 将 jQuery 导航按钮放在 slider 上

css - HTML 元素向右对齐以将左侧空间填充到另一个动态元素

javascript - 如何将 JQuery 输出格式化为页面顶部并使用 CSS 设置样式

css - 拉伸(stretch)图像 CSS?

css - Symfony2 和 Assetic - 我的 CSS 中引用的图像无法加载