CSS object-position
The CSS object-fit
property determines the alignment of the replaced element inside its box.
The object-position
property accepts any position value type. These are the same values that background-position
accepts.
You can use object-position
property in conjunction with the object-fit
property, to specify how the element fits, and is aligned, within its box.
What is a Replaced Element?
According to the CSS specification, a replaced element is an element whose content is outside the scope of the CSS formatting model, such as an image, embedded document, or applet.
According to the HTML specification the following elements are replaced elements:
Syntax
Where <position>
is a valid position value type, namely:
Possible Values
- percentage
-
Specifies the image's position as a percentage. This represents an offset of the top left corner of the background image from the top left corner of the background positioning area.
- length
-
Specifies the image's position as a length. This represents an offset of the top left corner of the background image from the top left corner of the background positioning area.
top
- Computes to
0%
for the vertical position if one or two values are given, otherwise specifies the top edge as the origin for the next offset. right
- Computes to
100%
for the horizontal position if one or two values are given, otherwise specifies the right edge as the origin for the next offset. bottom
- Computes to
100%
for the vertical position if one or two values are given, otherwise specifies the bottom edge as the origin for the next offset. left
- Computes to
0%
for the horizontal position if one or two values are given, otherwise specifies the left edge as the origin for the next offset. center
- Computes to
50%
(left 50%
) for the horizontal position if the horizontal position is not otherwise specified, or50%
(top 50%
) for the vertical position if it is.
If only one value is specified, the second value is assumed to be center
. If two values are given, a length or percentage as the first value represents the horizontal position (or offset) and a length or percentage as the second value represents the vertical position (or offset).
If three or four values are given, then each percentage or length represents an offset and must be preceded by a keyword, which specifies from which edge the offset is given. If three values are given, the missing offset is assumed to be zero.
Negative values are allowed. These represent an offset outward from the edge of the background positioning area.
For more information on valid values, see the position data type.
In addition, all CSS properties also accept the following CSS-wide keyword values as the sole component of their property value:
initial
- Represents the value specified as the property's initial value.
inherit
- Represents the computed value of the property on the element's parent.
unset
- This value acts as either
inherit
orinitial
, depending on whether the property is inherited or not. In other words, it sets all properties to their parent value if they are inheritable or to their initial value if not inheritable.
Example Code
Here are some code examples to demonstrate the various ways of specifying an object's position.
Basic Property Information
- Initial Value
50% 50%
- Applies To
- Replaced elements
- Inherited?
- No
- Animatable
- As for
background-position
(see example)
CSS Specifications
The object-position
property is defined in CSS Images Module Level 3 (W3C Candidate Recommendation, 10 October 2019)
Browser Support
The following table provided by Caniuse.com shows the level of browser support for this feature.
Vendor Prefixes
For maximum browser compatibility many web developers add browser-specific properties by using extensions such as -webkit-
for Safari, Google Chrome, and Opera (newer versions), -ms-
for Internet Explorer, -moz-
for Firefox, -o-
for older versions of Opera etc. As with any CSS property, if a browser doesn't support a proprietary extension, it will simply ignore it.
This practice is not recommended by the W3C, however in many cases, the only way you can test a property is to include the CSS extension that is compatible with your browser.
The major browser manufacturers generally strive to adhere to the W3C specifications, and when they support a non-prefixed property, they typically remove the prefixed version. Also, W3C advises vendors to remove their prefixes for properties that reach Candidate Recommendation status.
Many developers use Autoprefixer, which is a postprocessor for CSS. Autoprefixer automatically adds vendor prefixes to your CSS so that you don't need to. It also removes old, unnecessary prefixes from your CSS.
You can also use Autoprefixer with preprocessors such as Less and Sass.