This is something that I have had trouble with in the past and it's on my mind so I made a simple example sprite image to test and hopefully get some answers with.

这是我过去遇到过麻烦的事情,这是我的想法,所以我做了一个简单的sprite图像测试,希望得到一些答案。

If you view my code and demo here http://dabblet.com/gist/2263037

如果您在http://dabblet.com/gist/2263037查看我的代码和演示

You will see that I have a simple div that uses a background image

你会看到我有一个使用背景图像的简单div

Below that DIV I have the same div but this time I attempt to use a CSS Sprite image instead

在DIV下面我有相同的div,但这次我尝试使用CSS Sprite图像

So my question, is it possible to replicate the look of the first DIV using this sprite image or are changes needed on the sprite image?

所以我的问题是,是否可以使用此精灵图像复制第一个DIV的外观,或者是精灵图像需要更改?

Without Sprite image

没有Sprite图像

/* Notice wrapper with Single Image */
.notice-wrap {
    margin-top: 10px; padding: 0 .7em;
    -moz-border-radius: 4px;
    -webkit-border-radius: 4px;
    border-radius: 4px;
    border: 1px solid #CD0A0A;
    background: #B81900 url(http://www.getklok.com/css/ui-lightness/images/ui-bg_diagonals-thick_18_b81900_40x40.png) 50% 50% repeat;
}

With Sprite image

使用Sprite图像

/* Notice wrapper with SPRITE Image */
.notice-wrap-sprite {
    margin-top: 10px; padding: 0 .7em;
    -moz-border-radius: 4px;
    -webkit-border-radius: 4px;
    border-radius: 4px;
    border: 1px solid #CD0A0A;
    background: #fff url(http://f.cl.ly/items/2P1u2S1a132R0B3d2s08/test-sprite.png) repeat;
    background-position: 0 -52px;
}

HTML

<div class="notice-wrap"> 
       <p><strong>NOTICE:</strong> This is just a test notice, no reason to be alarmed</p> 
</div>

<BR><BR><BR>

<div class="notice-wrap-sprite"> 
       <p><strong>NOTICE:</strong> This is just a test notice, no reason to be alarmed</p> 
</div>

2 个解决方案

#1


15

Setting up the sprite

You can use an image sprite to do what you want. They can only repeat along one axis, i.e. repeat-x, but in your case that's all you need. Also your image within the sprite must run the entire width, this is how the browser knows to tile it.

您可以使用图像精灵来执行您想要的操作。它们只能沿一个轴重复,即repeat-x,但在你的情况下,这就是你所需要的。此外,精灵中的图像必须运行整个宽度,这是浏览器知道如何平铺它。

The trick is your repeated background must extend across the FULL WIDTH of your sprite image. This is crucial. Here is your image, modified to meet that criterion:

诀窍是你的重复背景必须延伸到精灵图像的全宽度。这很关键。这是您的图片,经过修改以符合该标准:

Setting up the CSS

Now we just reference it as usual, and it will work fine:

现在我们像往常一样引用它,它会正常工作:

/* Notice wrapper with SPRITE Image */
.notice-wrap-sprite {
  margin-top: 10px; padding: 0 .7em;
  -moz-border-radius: 4px;
  -webkit-border-radius: 4px;
  border-radius: 4px;
  border: 1px solid #CD0A0A;
  background: #fff url(http://f.cl.ly/items/2P1u2S1a132R0B3d2s08/test-sprite3.png) repeat-x;
  background-position: 0 -52px;
}

更多相关文章

  1. 使用img src加载图像会在Mozilla / Chrome中产生不同的大小
  2. PHP选择具有特定宽度的图像并构建网格
  3. 如何在应用程序中嵌入图像并在HTML控件中使用它们?
  4. 有没有办法从指定的网站找到某种颜色的图像?
  5. 如何递归将特色图像应用于Wordpress中的子页面?
  6. 如何在没有任何扩展名的php中保存图像
  7. php调整图像大小
  8. 从JSON字符串/数组中提取第一个图像
  9. 如何调整图像覆盖滑块

随机推荐

  1. c语言除法如何运算
  2. c语言二进制输出的方法
  3. c语言简单小游戏-扫雷游戏
  4. c语言程序的调试方法
  5. c语言求x的n次方的函数是什么
  6. c语言中合法的字符常量如何定义?
  7. c语言逗号表达式的运算规则
  8. executenonquery 的用法详解
  9. c语言允许函数的递归调用吗
  10. c语言常量定义规则