-moz-image-rect() CSS function

Non-standard: This feature is not standardized. We do not recommend using non-standard features in production, as they have limited browser support, and may change or be removed. However, they can be a suitable alternative in specific cases where no standard option exists.

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

The -moz-image-rect value for CSS background-image lets you use a portion of a larger image as a background.

Syntax

css
-moz-image-rect(url("my-url"), top, right, bottom, left);

Values

<url>

The URI of the image from which to take the sub-image.

top

The top edge, specified as an <integer> or <percentage>, of the sub-image within the specified image.

The right edge, specified as an <integer> or <percentage>, of the sub-image within the specified image.

bottom

The bottom edge, specified as an <integer> or <percentage>, of the sub-image within the specified image.

left

The left edge, specified as an <integer> or <percentage>, of the sub-image within the specified image.

Description

This property allows you to, for example, use different parts of one larger image as backgrounds in different parts of your content.

The syntax for the rectangle is similar to the rect() function generating a <shape> CSS type. All four values are relative to the upper left corner of the image.

Examples

This example loads an image and uses it in four segments to draw the Firefox logo in four <div> blocks. Clicking on their container causes the four segments to rotate around by swapping the background-image property values among the four <div> blocks.

CSS

The CSS defines one container style, then the styles for the four boxes that comprise the complete image.

The container looks like this:

css
#container {
  width: 267px;
  height: 272px;
  top: 100px;
  left: 100px;
  position: absolute;
  font-size: 16px;
  text-shadow: white 0px 0px 6px;
  text-align: center;
}

Then the four boxes defining the segments of the image are defined. Let's look at them one at a time.

css
#box1 {
  background-image: -moz-image-rect(url("firefox.png"), 0%, 50%, 50%, 0%);
  width: 133px;
  height: 136px;
  left: 0px;
  top: 0px;
  position: absolute;
}

This is the top-left corner of the image. It defines a rectangle containing the top-left quarter of the image in the file firefox.jpg.

css
#box2 {
  background-image: -moz-image-rect(url("firefox.png"), 0%, 100%, 50%, 50%);
  width: 133px;
  height: 136px;
  left: 133px;
  top: 0px;
  position: absolute;
}

This defines the top-right corner of the image.

The other corners follow a similar pattern:

css
#box3 {
  background-image: -moz-image-rect(url("firefox.png"), 50%, 50%, 100%, 0%);
  width: 133px;
  height: 136px;
  left: 0px;
  top: 136px;
  position: absolute;
}
#box4 {
  background-image: -moz-image-rect(url("firefox.png"), 50%, 100%, 100%, 50%);
  width: 133px;
  height: 136px;
  left: 133px;
  top: 136px;
  position: absolute;
}

HTML

We include a container with four boxes:

html
<div id="container">
  <div id="box1">Top left</div>
  <div id="box2">Top right</div>
  <div id="box3">Bottom left</div>
  <div id="box4">Bottom right</div>
</div>

This places the four segments of our image in a two-by-two box grid. These four segments are all contained within a larger <div> block whose primary purpose is to receive click events and dispatch them to our JavaScript code.

JavaScript

This code handles the click event when the container receives a mouse click.

js
function rotate() {
  let prevStyle = window
    .getComputedStyle(document.getElementById("box4"), null)
    .getPropertyValue("background-image");

  // Now that we've saved the last one, start rotating
  for (let i = 1; i <= 4; i++) {
    const curId = `box${i}`;

    // Shift the background images
    const curStyle = window
      .getComputedStyle(document.getElementById(curId), null)
      .getPropertyValue("background-image");
    document.getElementById(curId).style.backgroundImage = prevStyle;
    prevStyle = curStyle;
  }
}

document.getElementById("container").addEventListener("click", rotate);

This uses window.getComputedStyle() to fetch the style of each element, shifting it to the following element. Notice that before it begins doing so it saves a copy of the last box's style since it will be overwritten by the third element's style. By copying the values of the background-image property from one element to the next with each mouse click, we achieve the desired effect.

What it looks like

Specifications

Not part of any standard.

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
-moz-image-rect()
Deprecated Non-standard
Chrome – No support
Chrome
footnote No support
Edge – No support
Edge
footnote No support
Firefox – No support
Firefox 4 – 119 (Release date: 2011-03-22)
footnote Removed in 120 and later
Opera – No support
Opera
footnote No support
Safari – No support
Safari
footnote
footnote See bug 32177
Chrome Android – No support
Chrome Android
footnote No support
Firefox for Android – No support
Firefox for Android 4 – 119 (Release date: 2011-03-29)
footnote Removed in 120 and later
Opera Android – No support
Opera Android
footnote No support
Safari on iOS – No support
Safari on iOS
footnote
footnote See bug 32177
Samsung Internet – No support
Samsung Internet
footnote No support
WebView Android – No support
WebView Android
footnote No support
WebView on iOS – No support
WebView on iOS
footnote
footnote See bug 32177

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Non-standard. Check cross-browser support before using.
Deprecated. Not for use in new websites.
See implementation notes.

See also