CSS -moz-image-rect() Function
The -moz-image-rect() function was a Mozilla-specific CSS function used to select a rectangular portion of an image for use as a background.
It allowed developers to display individual sections of a larger image without creating separate image files. This function is deprecated, non-standard, and no longer supported in current Firefox versions.
Syntax
background-image: -moz-image-rect(url("image.png"), top, right, bottom, left);
The function accepts an image URL followed by four values defining the boundaries of the selected rectangular region.
For example:
.example {
background-image: -moz-image-rect(url("image.png"), 0%, 50%, 50%, 0%);
}
This selects the upper-left quarter of the image.
Note: This is legacy syntax. Modern browsers do not support the function, so it should not be used in new projects.
Parameters
The -moz-image-rect() function uses five arguments.
| Parameter | Description |
|---|---|
url() |
Specifies the source image. |
top |
Defines the upper boundary of the selected region. |
right |
Defines the right boundary of the selected region. |
bottom |
Defines the lower boundary of the selected region. |
left |
Defines the left boundary of the selected region. |
The four boundary values accept integers or percentages. Percentages are relative to the dimensions of the source image.
The order is always top, right, bottom, left.
For example:
background-image: -moz-image-rect(url("image.png"), 0%, 100%, 50%, 50%);
This selects the upper-right quarter of the image.
How It Works
The function was designed to extract a rectangular region from a larger source image.
Developers could divide a single image into multiple sections and display those sections as separate backgrounds.
For example, a square image could be divided into four equal regions:
| Region | Top | Right | Bottom | Left |
|---|---|---|---|---|
| Upper-left | 0% | 50% | 50% | 0% |
| Upper-right | 0% | 100% | 50% | 50% |
| Lower-left | 50% | 50% | 100% | 0% |
| Lower-right | 50% | 100% | 100% | 50% |
This technique was useful for image sprites, where multiple graphics were stored in a single image file.
Modern CSS achieves similar results using background-position, background-size, and other widely supported properties.
Example
The following legacy CSS demonstrates how the function selected the upper-left quarter of an image.
HTML:
<div class="legacy-image"></div>
CSS:
.legacy-image {
width: 100px;
height: 100px;
background-image: -moz-image-rect(url("image.png"), 0%, 50%, 50%, 0%);
background-repeat: no-repeat;
}
In older Firefox versions, the selected region of the source image would appear as the element's background.
Browser Result: This example is not rendered because -moz-image-rect() is no longer supported in modern browsers.
The next section demonstrates a modern alternative that produces a similar result.
Modern Alternative
Use background-position and background-size to display a specific region of a larger background image.
For example, a 200 by 200 pixel image can be divided into four 100 by 100 pixel sections.
HTML:
<div class="sprite-grid">
<div class="sprite sprite-one"></div>
<div class="sprite sprite-two"></div>
<div class="sprite sprite-three"></div>
<div class="sprite sprite-four"></div>
</div>
CSS:
.sprite-grid {
display: grid;
grid-template-columns: repeat(2, 100px);
gap: 10px;
}
.sprite {
width: 100px;
height: 100px;
background-image: url("image.png");
background-size: 200px 200px;
background-repeat: no-repeat;
}
.sprite-one {
background-position: 0 0;
}
.sprite-two {
background-position: -100px 0;
}
.sprite-three {
background-position: 0 -100px;
}
.sprite-four {
background-position: -100px -100px;
}
Each element displays a different quarter of the same image.
The following Browser Result uses a CSS gradient as the source image so the technique can be demonstrated without an external image file.
Each square displays one quarter of the larger gradient background.
This technique works in modern browsers and does not require the deprecated -moz-image-rect() function.
For actual image sprites, replace the gradient with a URL pointing to the source image and adjust the background dimensions and positions to match.
Browser Support
The -moz-image-rect() function is deprecated, non-standard, and not part of Baseline.
It was supported in Firefox versions 4 through 119 and was removed in Firefox 120.
Chrome, Edge, Safari, and other major browser engines did not implement this Mozilla-specific function.
Because current browsers do not support it, developers should use standard CSS background positioning or other image techniques instead.
For additional compatibility information, visit Can I Use? and search for the function name or related background-image features.
Specifications
The -moz-image-rect() function was a proprietary Mozilla extension and was never included in an official CSS specification.
It is documented for historical reference and for understanding older CSS code.
For new projects, use standardized CSS background properties rather than this deprecated function.
