CSS - object-position

概要

属性名 object-position
<position>
値の詳細 <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> ]
]
<length-percentage> = <length> | <percentage>
初期値50% 50%
適用可能要素リプレース要素
継承しない
サポートhttps://caniuse.com/?search=object-position

説明

画像(<img>)やビデオ(<video>)の横幅(width)や高さ(height)と object-fit を指定した場合のオブジェクトの表示位置を指定します。

説明
left左側に表示します。
right右側に表示します。
center横方向または縦方向の中央に表示します。
top上側に表示します。
bottom下側に表示します。
<length-percentage>上記で指定した位置からのオフセット値を指定します。

設定例を下記に示します。

object-position: center top; /* 左右中央、上部 */
object-position: 10px 20px; /* 左端から10px、上端から20pxの場所 */
object-position: right 10px; /* 右端から10pxの場所 */
object-position: right 10px bottom 10px; /* 右端から10px、下端から10pxの場所 */

使用例

CSS
.test {
 display: inline-block;
}
.img1 {
 width: 180px;
 height: 120px;
 border: 1px solid #000;
 object-fit: none;
}
.center-top {
 object-position: center top;
}
.pos-10-20 {
 object-position: 10px 20px;
}
.pos-r10-b10 {
 object-position: right 10px bottom 10px;
}
HTML
<div class="test">
 <div>center top</div>
 <img class="img1 center-top" src="example.gif" alt="test">
</div>
<div class="test">
 <div>10px 20px</div>
 <img class="img1 pos-10-20" src="example.gif" alt="test">
</div>
<div class="test">
 <div>right 10px bottom 10px</div>
 <img class="img1 pos-r10-b10" src="example.gif" alt="test">
</div>
表示
center top
test
10px 20px
test
right 10px bottom 10px
test

リンク


Copyright (C) 2022 杜甫々
初版:2022年8月7日、最終更新:2022年8月7日
https://www.tohoho-web.com/css/prop/object-position.htm

AltStyle によって変換されたページ (->オリジナル) /