Background repeat and CSS sprites

CSS sprites are a great way to improve the loading speed of your pages. One of the problems you might face with sprites is how to deal with cases when the background repeats.

The rule is pretty simple: if you want the background to repeat vertically (top to bottom), place the images in the sprite horizontally (left to right) and make sure the individual images in the sprite have the same height. For the opposite case: when you want to repeat horizontally, sprite vertically.

Example

» Demo

You have three divs and you want to repeat their background vertically, so that the background takes the whole height of the div, according to the content.

Source images:

  1. 1.png
  2. 2.png
  3. 3.png

Singe image sprite:

sprite8.png

CSS definitions to make this work:

    div {
        background-image:url(sprite8.png);
        background-repeat: repeat-y;
        width: 160px;
        padding: 20px;
        margin: 20px;
        float: left;
    }

    #my {
        background-position: 0px;
    }
    #my2 {
        background-position: -200px;
    }
    #my3 {
        background-position: -400px;
    }

Every image is 200px wide. Because of the 2x20px margin left and right, the width is 160px (=200 - 20 - 20)

The tree divs have IDs: my, my2, my3

Result

Once again, the result

Tested on Windows with IE7, FF, O, S.

This entry was posted on Thursday, April 3rd, 2008 and is filed under CSS, performance. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


Get notification for future posts: follow me on Twitter or subscribe to my RSS feed

Somewhat related posts

27 Responses to “Background repeat and CSS sprites”

  1. Brett Zamir Says:

    Nice to see this issue addressed, thanks… I like to use percentage widths though–I guess only something like SVG would allow one to indicate a percentage width/height which would work on both the image as well as the containing element?

  2. CSS Sprites (y II) » Criterion Says:

    [...] comentarios de esta entrada suscribiendote al feed RSS 2.0. Puedes dejar un comentario, o enviar un trackback desde tusitio. [...]

  3. e devlet Says:

    css background examples , Properties , Attribute - - http://css-lessons.ucoz.com/background-css-examples.htm

  4. Andrew Says:

    Thanks for the demo and the special instruction on repeating css sprite images!

  5. James Says:

    What if I have a background image that needs to be repeated x and y?

  6. Background repeat and CSS sprites / phpied.com | CSS Tutorials - CSSHelper.net Says:

    [...] Source [...]

  7. CSS Sprites - Background Repeat Problem | Webdesignblog Says:

    [...] gibt zwar eine Möglichkeit, aber ich halte die nicht für wirklich brauchbar, da man ziemlich eingeschränkt wird und für [...]

  8. The Mystery Of CSS Sprites: Techniques, Tools And Tutorials | CSS | Smashing Magazine Says:

    [...] Background Repeat and CSS Sprites CSS sprites are a great way to improve the loading speed of your pages. One of the problems you might face with sprites is how to deal with cases where the background repeats. The rule is pretty simple: if you want the background to repeat vertically (top to bottom), place the images in the sprite horizontally (left to right) and make sure the individual images in the sprite have the same height. For the opposite case, when you want to repeat horizontally, sprite vertically. [...]

  9. The Mystery Of CSS Sprites: Techniques, Tools And Tutorials 於囧 Says:

    [...] Background Repeat and CSS Sprites CSS sprites are a great way to improve the loading speed of your pages. One of the problems you might face with sprites is how to deal with cases where the background repeats. The rule is pretty simple: if you want the background to repeat vertically (top to bottom), place the images in the sprite horizontally (left to right) and make sure the individual images in the sprite have the same height. For the opposite case, when you want to repeat horizontally, sprite vertically. [...]

  10. The Mystery Of CSS Sprites: Techniques, Tools And Tutorials Says:

    [...] Background Repeat and CSS Sprites CSS sprites are a great way to improve the loading speed of your pages. One of the problems you might face with sprites is how to deal with cases where the background repeats. The rule is pretty simple: if you want the background to repeat vertically (top to bottom), place the images in the sprite horizontally (left to right) and make sure the individual images in the sprite have the same height. For the opposite case, when you want to repeat horizontally, sprite vertically. [...]

  11. The Mystery Of CSS Sprites: Techniques, Tools And Tutorials | Feed Reader Says:

    [...] Background Repeat and CSS Sprites CSS sprites are a great way to improve the loading speed of your pages. One of the problems you might face with sprites is how to deal with cases where the background repeats. The rule is pretty simple: if you want the background to repeat vertically (top to bottom), place the images in the sprite horizontally (left to right) and make sure the individual images in the sprite have the same height. For the opposite case, when you want to repeat horizontally, sprite vertically. [...]

  12. The Mystery Of CSS Sprites: Techniques, Tools And Tutorials | Design Blog Says:

    [...] Background Repeat and CSS Sprites CSS sprites are a great way to improve the loading speed of your pages. One of the problems you might face with sprites is how to deal with cases where the background repeats. The rule is pretty simple: if you want the background to repeat vertically (top to bottom), place the images in the sprite horizontally (left to right) and make sure the individual images in the sprite have the same height. For the opposite case, when you want to repeat horizontally, sprite vertically. [...]

  13. Video | ClickLogin Web Design Says:

    [...] Background Repeat as great as CSS Sprites CSS sprites have been a great approach to urge a loading speed of your pages. One of a problems we competence face with sprites is how to bargain with cases where a credentials repeats. The order is flattering simple: if we wish a credentials to repeat plumb (top to bottom), place a images in a goddess horizontally (left to right) as great as have certain a particular images in a goddess have a same height. For a conflicting case, when we wish to repeat horizontally, goddess vertically. [...]

  14. The Mystery Of CSS Sprites: Techniques, Tools And Tutorials « LocalLab : Foire aux Infos Says:

    [...] Background Repeat and CSS Sprites CSS sprites are a great way to improve the loading speed of your pages. One of the problems you might face with sprites is how to deal with cases where the background repeats. The rule is pretty simple: if you want the background to repeat vertically (top to bottom), place the images in the sprite horizontally (left to right) and make sure the individual images in the sprite have the same height. For the opposite case, when you want to repeat horizontally, sprite vertically. [...]

  15. 我想网 » Blog Archive » 解密CSS Sprites:技巧、工具和教程 Says:

    [...] Background Repeat and CSS Sprites 什么情况下使用CSS Sprites [...]

  16. The Mystery Of CSS Sprites: Techniques, Tools And Tutorials | Designurimagination Blog - Let Your Imagination Fly Says:

    [...] Background Repeat and CSS Sprites [...]

  17. 解密CSS Sprites:技巧、工具和教程| 在这个大都市,我只需要一个角落,用来记录我的生活| 都市de角落 Says:

    [...] Background Repeat and CSS Sprites 什么情况下使用CSS Sprites [...]

  18. 解密CSS Sprites:技巧、工具和教程 | 1px left|左1像素 Says:

    [...] Background Repeat and CSS Sprites 什么情况下使用CSS Sprites [...]

  19. PureCSS » Blog Archive » 解密CSS Sprites:技巧、工具和教程 Says:

    [...] Background Repeat and CSS Sprites 什么情况下使用CSS Sprites [...]

  20. La tecnica CSS Sprite : come ridurre il tempo di caricamento di una pagina web | Pecciola Says:

    [...] infine un esempio pratico : http://www.phpied.com/background-repeat-and-css-sprites/ var heyos_popunder_user = 8812; var heyos_popunder_type = ‘G’; Segnala presso: Articoli che [...]

  21. AMB Album » The Mystery Of CSS Sprites: Techniques, Tools And Tutorials Says:

    [...] Background Repeat and CSS Sprites CSS sprites are a great way to improve the loading speed of your pages. One of the problems you might face with sprites is how to deal with cases where the background repeats. The rule is pretty simple: if you want the background to repeat vertically (top to bottom), place the images in the sprite horizontally (left to right) and make sure the individual images in the sprite have the same height. For the opposite case, when you want to repeat horizontally, sprite vertically. [...]

  22. The Mystery Of CSS Sprites: Techniques, Tools And Tutorials « Ramesh Says:

    [...] Background Repeat and CSS Sprites CSS sprites are a great way to improve the loading speed of your pages. One of the problems you might face with sprites is how to deal with cases where the background repeats. The rule is pretty simple: if you want the background to repeat vertically (top to bottom), place the images in the sprite horizontally (left to right) and make sure the individual images in the sprite have the same height. For the opposite case, when you want to repeat horizontally, sprite vertically. [...]

  23. Sharon Says:

    if you interesting in reading about CSS Sprites in hebrew, you are invited to read this article:
    http://www.ing.co.il/?p=24

  24. CSS Sprites: CSS图片合并技术详解 | 帕兰映像 Says:

    [...] Background Repeat and CSS Sprites 什么情况下使用CSS Sprites [...]

  25. The Mystery Of CSS Sprites: Techniques, Tools And Tutorials | 9Tricks.Com - Tips - Tricks - Tutorials Says:

    [...] Background Repeat and CSS Sprites CSS sprites are a great way to improve the loading speed of your pages. One of the problems you might face with sprites is how to deal with cases where the background repeats. The rule is pretty simple: if you want the background to repeat vertically (top to bottom), place the images in the sprite horizontally (left to right) and make sure the individual images in the sprite have the same height. For the opposite case, when you want to repeat horizontally, sprite vertically. [...]

  26. sunnybear Says:

    There is open source tool - http://sprites.in/ - which can automatically create CSS Sprites of aolmost any complexity. It’s also included into Web Optimizer tool - http://www.web-optimizer.us/

  27. tobias Says:

    dont want to be picky, but its the padding not the margin which gets added to the width of a box

Leave a Reply