CSS propertyEffects
object-position
Sets the position of an image or video within its box.
- Initial value
50% 50%- Inherited
- No
- Animatable
- yes, as a length or percentage
- Applies to
- replaced elements, e.g. img, video
- JavaScript
element.style.objectPosition = "center top"
Example
#CSS
.team img {
width: 100%;
aspect-ratio: 3 / 4;
object-fit: cover;
object-position: center top;
}Example HTML
<img src="/collections/covers/programowanie-dla-dzieci.webp" alt="" style="width: 150px; aspect-ratio: 3 / 4; object-fit: cover; object-position: 60% top; border-radius: 12px;">Preview
Definition and usage
#The object-position property specifies which part of an image is visible when object-fit crops it to the box, or where the image sits when it is smaller than the box. It works like background-position, but for <img> and <video> elements.
By default the image is centred (50% 50%). For portraits center top often works better so the head isn’t cut off, and for landscapes center bottom.
It accepts keywords, percentages and lengths, first for the horizontal axis, then for the vertical one.
Syntax
#Syntax
object-position: <x-position> <y-position>?;Values
#Values in practice
#center
The centre of the image is visible.
.demo { object-position: center; }left
The left part of the image is visible.
.demo { object-position: left; }right
The right part of the image is visible.
.demo { object-position: right; }30% 20%
A precise choice of the visible portion.
.demo { object-position: 30% 20%; }Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- For photos of people set
object-position: center topso the crop doesn’t cut off faces. - Without an
object-fitother thanfill, the image fills the box entirely and the position doesn’t matter.
Related entries
#Spotted a mistake or missing an example? Write to us.