Apply CSS3 Transformations to Background Images

5:28:00 PM |

Scaling, skewing and rotating any element is possible with the CSS3 transform property. It’s supported in all modern browsers (with vendor prefixes) and degrades gracefully, e.g.
  1. #myelement  
  2. {  
  3.     -webkit-transform: rotate(30deg);  
  4.     -moz-transform: rotate(30deg);  
  5.     -ms-transform: rotate(30deg);  
  6.     -o-transform: rotate(30deg);  
  7.     transform: rotate(30deg);  
  8. }  
Great stuff. However, this rotates the whole element — it’s content, border and background image. What if you only want to rotate the background image? Or what if you want the background to remain fixed while the element is rotated?
Currently, there’s no W3C proposal for background-image transformations. It would be incredibly useful so I suspect one will appear eventually, but that doesn’t help developers who want to use similar effects today.
Fortunately, there is a solution. In essence, it’s a hack which applies the background image to a before or after pseudo element rather than the parent container. The pseudo element can then be transformed independently.

Transforming the Background Only

The container element can have any styles applied but it must be set to position: relative since our pseudo element will be positioned within it. You should also set overflow: hidden unless you’re happy for the background to spill out beyond the container.
  1. #myelement  
  2. {  
  3.     position: relative;  
  4.     overflow: hidden;  
  5. }  
We can now create an absolutely-positioned pseudo element with a transformed background. The z-index is set -1 to ensure it appears below the container’s content.
  1. #myelement:before  
  2. {  
  3.     content: "";  
  4.     position: absolute;  
  5.     width: 200%;  
  6.     height: 200%;  
  7.     top: -50%;  
  8.     left: -50%;  
  9.     z-index: -1;  
  10.     background: url(background.png) 0 0 repeat;  
  11.     -webkit-transform: rotate(30deg);  
  12.     -moz-transform: rotate(30deg);  
  13.     -ms-transform: rotate(30deg);  
  14.     -o-transform: rotate(30deg);  
  15.     transform: rotate(30deg);  
  16. }  
Note you may need to adjust the pseudo element’s width, height and position. For example, if you’re using a repeated image, a rotated area must be larger than its container to fully cover the background:
background transform

Fixing the Background on a Transformed Element

All transforms on the parent container are applied to pseudo elements. Therefore, we need to undo that transformation, e.g. if the container is rotated by 30 degrees, the background must be rotated -30 degrees to return to a fixed position:
  1. #myelement  
  2. {  
  3.     position: relative;  
  4.     overflow: hidden;  
  5.     -webkit-transform: rotate(30deg);  
  6.     -moz-transform: rotate(30deg);  
  7.     -ms-transform: rotate(30deg);  
  8.     -o-transform: rotate(30deg);  
  9.     transform: rotate(30deg);  
  10. }  
  11. #myelement:before  
  12. {  
  13.     content: "";  
  14.     position: absolute;  
  15.     width: 200%;  
  16.     height: 200%;  
  17.     top: -50%;  
  18.     left: -50%;  
  19.     z-index: -1;  
  20.     background: url(background.png) 0 0 repeat;  
  21.     -webkit-transform: rotate(-30deg);  
  22.     -moz-transform: rotate(-30deg);  
  23.     -ms-transform: rotate(-30deg);  
  24.     -o-transform: rotate(-30deg);  
  25.     transform: rotate(-30deg);  
  26. }  
Again, you will need to adjust the size and position to ensure the background covers the parent container adequately.
The effects work in IE9, Firefox, Chrome, Safari and Opera. IE8 will not show any transformations but the background appears.
IE6 and 7 do not support pseudo elements so the background disappears. However, if you want to support those browsers, you could apply a background image to the container then set it to “none” using an advanced selector or conditional CSS.