object-position
CSS
property specifies the alignment of the selected
replaced element
's contents within the element's box.
Areas of the box which aren't covered by the replaced element's object will show the element's background.
You can adjust how the replaced element's object's intrinsic size (that is, its natural size) is adjusted to fit within the element's box using the
object-fit
特性。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
/* <position> values */ object-position: center top; object-position: 100px 50px; /* Global values */ object-position: inherit; object-position: initial; object-position: unset;
<position>
From one to four values that define the 2D position of the element. Relative or absolute offsets can be used.
注意: The position can be set so that the replaced element is drawn outside its box.
| 初始值 |
50% 50%
|
|---|---|
| 适用于 | replaced elements |
| 继承 | yes |
| 百分比 | refer to width and height of element itself |
| 计算值 | 如指定 |
| 动画类型 | repeatable list of simple list of length, percentage, or calc |
<position>where
<position> = [ [ left | center | right ] || [ top | center | bottom ] | [ left | center | right | <length-percentage> ] [ top | center | bottom | <length-percentage> ]? | [ [ left | right ] <length-percentage> ] && [ [ top | bottom ] <length-percentage> ] ]where
<length-percentage> = <length> | <percentage>
Here we see HTML that includes two
<img>
elements, each displaying the MDN logo.
<img id="object-position-1" src="https://yari-demos.prod.mdn.mozit.cloud/files/12668/MDN.svg" alt="MDN Logo"/> <img id="object-position-2" src="https://yari-demos.prod.mdn.mozit.cloud/files/12668/MDN.svg" alt="MDN Logo"/>
The CSS includes default styling for the
<img>
element itself, as well as separate styles for each of the two images.
img {
width: 300px;
height: 250px;
border: 1px solid black;
background-color: silver;
margin-right: 1em;
object-fit: none;
}
#object-position-1 {
object-position: 10px;
}
#object-position-2 {
object-position: 100% 10%;
}
The first image is positioned with its left edge inset 10 pixels from the left edge of the element's box. The second image is positioned with its right edge flush against the right edge of the element's box and is located 10% of the way down the height of the element's box.
| 规范 | 状态 | 注释 |
|---|---|---|
|
CSS Images Module Level 3
The definition of 'object-position' in that specification. |
候选推荐 | 初始定义。 |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
object-position
|
Chrome 完整支持 31 | Edge 完整支持 16 | Firefox 完整支持 36 | IE 不支持 No |
Opera
完整支持
19
|
Safari 完整支持 10 | WebView Android 完整支持 4.4.3 | Chrome Android 完整支持 31 | Firefox Android 完整支持 36 |
Opera Android
完整支持
19
|
Safari iOS 完整支持 10 | Samsung Internet Android 完整支持 2.0 |
| Support for three-value syntax of position | Chrome 不支持 31 — 68 | Edge 不支持 16 — 79 | Firefox 不支持 36 — 70 | IE 不支持 No |
Opera
不支持
19 — 55
|
Safari 完整支持 10 | WebView Android 不支持 4.4.3 — 68 | Chrome Android 不支持 31 — 68 | Firefox Android 完整支持 36 |
Opera Android
不支持
19 — 48
|
Safari iOS 完整支持 10 | Samsung Internet Android 不支持 2.0 — 10.0 |
完整支持
不支持
要求使用供应商前缀或不同名称。
object-fit
,
image-orientation
,
image-rendering
,
image-resolution
.
<gradient>
<image>
image-orientation
image-rendering
object-fit
object-position