object-fit

Controls how replaced elements (img, video) fit within their container. Determines whether to crop while maintaining aspect ratio, or show the entire content.

Class List

ClassCSSDescription
ofcobject-fit: coverfills container; maintains ratio, overflow cropped
ofctobject-fit: containshows entirely; maintains ratio, letterboxing possible

Visual Comparison

Compares the difference between cover and contain when placing the same image in a fixed-size container.

object-fit: cover — ofc

cover demo

Fills the container completely. Maintains aspect ratio, overflowing parts are cropped

object-fit: contain — ofct

contain demo

Shows entirely within the container. Maintains aspect ratio, letterboxing may occur

ofc (cover)ofct (contain)
fill methodFills container with no gapsFits entirely within container
aspect ratio preservedPreservedPreserved
croppingOverflowing parts croppedNot cropped
letterboxingNoneMargins appear for ratio difference (letterbox)
primary use casesHero, avatar, thumbnail, background imagesProduct images, logos, icons

When to Use What?

SituationRecommendedReason
Hero image, bannerofcFill completely for visual impact
User avatarofcSquare + br50p for circular avatar
Card thumbnailofcDisplay images of various ratios uniformly
Product imageofctFull product must be visible, prevent cropping
Logo displayofctLogo must not be partially cropped
Gallery/portfolioofc일관된 그리드 크기 Preserved

Class Details

ofcobject-fit: cover

Fills the container completely while maintaining the image aspect ratio. Overflowing parts are cropped. The most commonly used pattern for hero images, avatars, and thumbnails.

<!-- Hero image -->
<div class="w100p h30rem oh br8px">
  <img src="hero.jpg" alt="hero" class="w100p h100p ofc" />
</div>

<!-- Circular avatar -->
<img src="avatar.jpg" alt="avatar" class="w6rem h6rem ofc br50p" />

<!-- Card thumbnail -->
<div class="dg gtcr3-1fr gap2rem sm-gtc1fr">
  <div class="oh br8px">
    <img src="thumb1.jpg" alt="" class="w100p h20rem ofc" />
    <div class="p16px">Card Title</div>
  </div>
</div>

Common Combinations

w100p h20rem ofcresponsive image container (most basic)
w6rem h6rem ofc br50pcircular avatar
w100p h30rem ofc br8pxrounded-corner hero image
ar16-9 w100p ofc16:9 ratio fixed image

ofctobject-fit: contain

Fits the entire image within the container. Maintains aspect ratio; if the container and image ratios differ, letterboxing occurs. Used when the full image must be shown without cropping, such as product images and logos.

<!-- Product image -->
<div class="w100p h24rem bg18181B br8px">
  <img src="product.jpg" alt="product" class="w100p h100p ofct" />
</div>

<!-- Logo display -->
<div class="w12rem h6rem bg0F0F17 br8px p8px">
  <img src="logo.svg" alt="logo" class="w100p h100p ofct" />
</div>

Common Combinations

w100p h24rem ofctproduct image (show full)
w12rem h6rem ofct p8pxlogo container (with padding)
ar4-3 w100p ofct bg18181B4:3 ratio + background color for letterbox

Common Patterns

The basic pattern for responsive image containers is w100p h20rem ofc.

<!-- Responsive image container (default pattern) -->
<img src="photo.jpg" alt="" class="w100p h20rem ofc" />

<!-- Card grid + uniform thumbnails -->
<div class="dg gtcr3-1fr gap2rem sm-gtc1fr">
  <div class="oh br8px bg18181B">
    <img src="1.jpg" alt="" class="w100p h20rem ofc" />
    <div class="p16px">
      <h3>Title</h3>
      <p>Description</p>
    </div>
  </div>
  <div class="oh br8px bg18181B">
    <img src="2.jpg" alt="" class="w100p h20rem ofc" />
    <div class="p16px">
      <h3>Title</h3>
      <p>Description</p>
    </div>
  </div>
</div>

<!-- Avatar list -->
<div class="df gap8px">
  <img src="u1.jpg" alt="" class="w4rem h4rem ofc br50p" />
  <img src="u2.jpg" alt="" class="w4rem h4rem ofc br50p" />
  <img src="u3.jpg" alt="" class="w4rem h4rem ofc br50p" />
</div>

Tips & Notes

Replaced elements only

ofc and ofct only work on replaced elements such as img, video, and canvas. They have no effect on regular div elements.

Creating circular avatars

Combine ofc with br50p to crop any aspect ratio image into a circle. Example: w6rem h6rem ofc br50p

width/height required

object-fit requires explicit dimensions on the element. Always set the container size with w100p h20rem etc.