CSS

object-position

目标位置

object-position属性确定所选元素在其框内的对齐方式。

/* <position> values */ object-position: 100px 50px; /* Global values */ object-position: inherit; object-position: initial; object-position: unset;

Initial value50% 50%
Applies toreplaced elements
Inheritedyes
Percentagesrefer to width and height of element itself
Mediavisual
Computed valueas specified
Animation typerepeatable list of simple list of length, percentage, or calc
Canonical orderthe unique non-ambiguous order defined by the formal grammar

句法

价值

<position>从一个值到四个值,定义元素的2D位置。可以使用相对或绝对偏移量。请注意,该位置可以设置在元素%27s框之外。

形式语法

<position>where <position> = [[ left | center | right | top | bottom | <length-percentage> ] | [ left | center | right | <length-percentage> ] [ top | center | bottom | <length-percentage> ] | [ center | [ left | right ] <length-percentage>? ] && [ center | [ top | bottom ] <length-percentage>? ]] where <length-percentage> = <length> | <percentage>

HTML内容

<img id="object-position-1" src="https://mdn.mozillademos.org/files/12668/MDN.svg" alt="MDN Logo"/> <img id="object-position-2" src="https://mdn.mozillademos.org/files/12668/MDN.svg" alt="MDN Logo"/>

CSS内容

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%; }

输出量

规格

SpecificationStatusComment
CSS Image Values and Replaced Content Module Level 4The definition of 'object-position' in that specification.Working DraftThe from-image and flip keywords have been added.
CSS Image Values and Replaced Content Module Level 3The definition of 'object-position' in that specification.Candidate RecommendationInitial definition

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support31.01636 (36)No support11.60-o 19.010.0

FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support4.4.436 (36)No support11.5-o 24No support