illustration for Styling
Course

Styling

  1. 2
    Place Images with object-position
    2m 31s

Place Images with object-position

Michael Chan
InstructorMichael Chan
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

object-position controls the placement of img and video media inside containing boxes. It's a partner property to object-fit. Using the two properties together gives full control over image scaling, placement, and cropping.

Instructor: [00:00] Object-position is a CSS property that affects which parts of a cropped image are seen. Here are two different images with two aspect ratios. One of them is 2 x 3, 200px x 300px, and the other is 3 x 2, the inverse.

[00:16] Let's start by making both of them square using this prepared class "square-image". This class sets the height and width to a fixed 200 pixels and uses the object-fit property with the value of cover to crop the images instead of squishing down to fit the available space. We can see how bad and squished they look without the rule.

[00:44] By default, object-fit: cover gives you a center center crop where the top and bottom are cropped off of this first image and the left and right are cropped off of this second image. Use the object-position property to place the image in the available space. I'll do this inline so we can control it independently for each image.

[01:09] Object-position takes x-axis and y-axis values in that order. We can show the top of this vertical image by changing the value to center top. Conversely, we can shift it down using center bottom.

[01:23] For the horizontal image, we do the same thing, but we're going to change the first value, which is the x-axis value, to be left to get the left side of the image or right to get the right side of the image. To get even tighter control, we can use a percentage. These values can be negative values as well.

[01:42] Here you'll see that it's impossible to move an image too far and not hit the edge of your container. Let's change this back to a positive number. When you know the exact image size, you can use pixels as well, which also support negative values. This is true for any supported fixed measurement value.

When we use object-fit: [02:03] Cover, these images are going to fit the available space. Even though I'm specifying bottom here, it doesn't matter because the height of this image is being set to match the size of the box. If we change this to object-fit: none, which does no scaling, make the box smaller than the image, we can have control over both axes. The same is true for our vertical image.

[02:29] That's it. That's object-position.

egghead
egghead
~ 44 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today