• CSS
  • Your first website: Styling the content
  • Challenge: Biography page
  • Combinators
  • Sizing
  • Styling tables
  • Challenge: Fancy box styles
  • Styling links
  • Introduction
  • CSS grid layout
  • CSS reference
  • Modules
    1. CSS basic user interface
    2. CSS cascading and inheritance
    3. CSS conditional rules
    4. CSS display
    5. CSS fonts
    6. CSS images
    7. CSS masking
    8. CSS namespaces
    9. CSS paged media
    10. CSS ruby layout
    11. CSS scroll-driven animations
    12. CSS shapes
    13. CSS text decoration
    14. CSS view transitions
    15. -moz-float-edge Non-standard Deprecated
    16. -moz-force-broken-image-icon Non-standard Deprecated
    17. -moz-image-region Non-standard
    18. -moz-orient Non-standard
    19. -moz-user-focus Non-standard Deprecated
    20. -moz-user-input Non-standard Deprecated
  • -webkit-*
    1. -webkit-border-before Non-standard
    2. -webkit-box-reflect Non-standard
    3. -webkit-mask-box-image Non-standard
    4. -webkit-mask-composite Non-standard
    5. -webkit-mask-position-x Non-standard
    6. -webkit-mask-position-y Non-standard
    7. -webkit-mask-repeat-x Non-standard
    8. -webkit-mask-repeat-y Non-standard
    9. -webkit-tap-highlight-color Non-standard
    10. -webkit-text-security Non-standard
    11. -webkit-touch-callout Non-standard
  • align-items
  • anchor-name Experimental
  • animation-*
    1. animation-direction
    2. animation-name
    3. animation-range Experimental
    4. animation-range-end Experimental
    5. animation-range-start Experimental
    6. animation-timeline Experimental
    7. backdrop-filter
    8. background-blend-mode
    9. background-origin
    10. background-repeat
    11. border-block
    12. border-block-end-style
    13. border-block-start-style
    14. border-bottom
    15. border-bottom-style
    16. border-end-end-radius
    17. border-image-repeat
    18. border-inline
    19. border-inline-end-style
    20. border-inline-start-style
    21. border-left
    22. border-radius
    23. border-right-width
    24. border-style
    25. border-top-right-radius
    26. bottom
    27. box-*
      1. box-align Non-standard Deprecated
      2. box-direction Non-standard Deprecated
      3. box-flex Non-standard Deprecated
      4. box-flex-group Non-standard Deprecated
      5. box-lines Non-standard Deprecated
      6. box-ordinal-group Non-standard Deprecated
      7. box-orient Non-standard Deprecated
      8. box-pack Non-standard Deprecated
      9. break-before
      10. clear
      11. clip-*
        1. clip Deprecated
        2. color-interpolation
        3. column-fill
        4. column-rule-style
        5. columns
        6. contain-*
          1. contain-intrinsic-inline-size
          2. container-name
          3. counter-increment
          4. cx
          5. display
          6. field-sizing Experimental
          7. fill-*
            1. filter
            2. flex-*
              1. flex-flow
              2. float
              3. font-family
              4. font-optical-sizing
              5. font-smooth Non-standard
              6. font-stretch Deprecated
              7. font-synthesis-position Experimental
              8. font-variant
              9. font-variant-emoji
              10. font-variation-settings
              11. grid
              12. grid-auto-rows
              13. grid-row
              14. grid-template-areas
              15. height
              16. image-orientation
              17. image-resolution Experimental
            3. inset-block
            4. inset-inline-end
            5. interpolate-size Experimental
            6. justify-self
          8. line-break
          9. line-height-step Experimental
        7. list-*
          1. list-style-type
        8. margin-*
          1. margin-block-start
          2. margin-inline-start
          3. margin-trim Experimental
        9. marker-*
          1. marker-start
        10. mask-*
          1. mask-border-outset
          2. mask-border-width
          3. mask-mode
          4. mask-size
          5. math-shift Experimental
          6. max-inline-size
          7. min-inline-size
          8. object-position
          9. offset-*
            1. offset-path
            2. order
            3. outline-offset
            4. overflow-anchor
            5. overflow-wrap
            6. overlay Experimental
            7. overscroll-*
              1. overscroll-behavior-x
              2. padding-block-end
              3. padding-inline-end
              4. padding-top
            8. page-*
              1. page-break-after Deprecated
              2. page-break-before Deprecated
              3. page-break-inside Deprecated
            9. place-content
            10. position
            11. position-anchor Experimental
            12. position-area Experimental
            13. position-try Experimental
            14. position-try-fallbacks Experimental
            15. position-try-order Experimental
            16. position-visibility Experimental
          10. resize
          11. ruby-align
          12. scale
          13. scroll-*
            1. scroll-margin-block-end
            2. scroll-margin-inline-end
            3. scroll-margin-top
            4. scroll-marker-group Experimental
            5. scroll-padding-block-start
            6. scroll-padding-inline-start
            7. scroll-snap-align
            8. scroll-timeline Experimental
            9. scroll-timeline-axis Experimental
            10. scroll-timeline-name Experimental
          14. scrollbar-*
            1. shape-image-threshold
            2. speak-as Experimental
            3. stroke-dasharray
            4. stroke-miterlimit
            5. table-layout
            6. text-*
              1. text-box
              2. text-decoration
              3. text-decoration-skip Experimental
              4. text-emphasis
              5. text-indent
              6. text-rendering
              7. text-size-adjust Experimental
              8. text-spacing-trim Experimental
              9. text-wrap
              10. timeline-scope Experimental
              11. transform-box
              12. transition-behavior
              13. transition-timing-function
            7. user-modify Non-standard Deprecated
            8. view-timeline Experimental
            9. view-timeline-axis Experimental
            10. view-timeline-inset Experimental
            11. view-timeline-name Experimental
            12. white-space-collapse
            13. word-break
            14. y
            15. Attribute selectors
            16. Universal selectors
          15. Combinators
            1. Column combinator Experimental
            2. Selector list
            3. :-moz-broken Non-standard Deprecated
            4. :-moz-drag-over Non-standard
            5. :-moz-first-node Experimental Non-standard
            6. :-moz-handler-blocked Non-standard
            7. :-moz-handler-crashed Non-standard
            8. :-moz-handler-disabled Non-standard
            9. :-moz-last-node Experimental Non-standard
            10. :-moz-loading Non-standard
            11. :-moz-locale-dir(ltr) Non-standard
            12. :-moz-locale-dir(rtl) Non-standard
            13. :-moz-only-whitespace Non-standard
            14. :-moz-submit-invalid Non-standard
            15. :-moz-suppressed Non-standard
            16. :-moz-user-disabled Non-standard
            17. :-moz-window-inactive Non-standard
          16. :blank Experimental
          17. :current Experimental
          18. :disabled
          19. :first-child
          20. :focus-within
        11. :has()
        12. :hover
        13. :is()
        14. :left
        15. :local-link Experimental
        16. :nth-child()
        17. :only-child
        18. :out-of-range
        19. :placeholder-shown
        20. :read-write
        21. :scope
        22. :target
        23. :target-within Experimental
      12. :visited
      13. ::-moz-color-swatch Non-standard
      14. ::-moz-focus-inner Experimental Non-standard
      15. ::-moz-list-bullet Experimental Non-standard
      16. ::-moz-list-number Experimental Non-standard
      17. ::-moz-meter-bar Non-standard
      18. ::-moz-progress-bar Experimental Non-standard
      19. ::-moz-range-progress Non-standard
      20. ::-moz-range-thumb Non-standard
      21. ::-moz-range-track Non-standard
    28. ::-webkit-*
      1. ::-webkit-inner-spin-button Non-standard
      2. ::-webkit-meter-bar Non-standard Deprecated
      3. ::-webkit-meter-even-less-good-value Non-standard
      4. ::-webkit-meter-inner-element Non-standard
      5. ::-webkit-meter-optimum-value Non-standard
      6. ::-webkit-meter-suboptimum-value Non-standard
      7. ::-webkit-progress-bar Non-standard
      8. ::-webkit-progress-inner-element Non-standard
      9. ::-webkit-progress-value Non-standard
      10. ::-webkit-scrollbar Non-standard
      11. ::-webkit-search-cancel-button Non-standard
      12. ::-webkit-search-results-button Non-standard
      13. ::-webkit-slider-runnable-track Non-standard
      14. ::-webkit-slider-thumb Non-standard
    29. ::checkmark Experimental
    30. ::file-selector-button
    31. ::highlight()
    32. ::picker-icon Experimental
    33. ::picker() Experimental
    34. ::scroll-marker-group
  • ::target-text
  • ::view-*
    1. ::view-transition-new
    2. @container
    3. @document Non-standard Deprecated
    4. @import
    5. @namespace
    6. @position-try Experimental
    7. @supports
    8. -moz-image-rect Non-standard Deprecated
    9. anchor-size() Experimental
    10. anchor() Experimental
    11. attr()
    12. calc-size() Experimental
    13. color-mix()
    14. cos()
    15. cubic-bezier()
    16. element() Experimental
    17. fit-content()
    18. hwb()
    19. inset()
    20. lch()
    21. log()
    22. min()
    23. oklch()
    24. palette-mix() Experimental
    25. scale3d()
    26. scroll() Experimental
    27. sin()
    28. steps()
    29. translateZ()
    30. view() Experimental
    31. <angle-percentage>
    32. <blend-mode>
    33. <color-interpolation-method>
    34. <custom-ident>
    35. <display-inside>
    36. <display-outside>
    37. <frequency-percentage>
    38. <hex-color>
    39. <image>
    40. <line-style>
    41. <overflow>
    42. <position-area> Experimental
    43. <resolution>
    44. <shape> Deprecated
    45. <time-percentage>
    46. Guides
    47. Animations
      1. Box alignment in block layout
      2. Introduction to the CSS basic box model
      3. Using relative colors
      4. Basic concepts of multi-column layout
      5. Handling content breaks in multi-column layout
    48. Conditional rules
      1. Using container size and style queries
    49. CSSOM view
      1. Introduction to formatting contexts
      2. Basic concepts of flexbox
      3. Controlling ratios of flex items along the main axis
      4. Variable fonts guide
    50. Grid
      1. Grid template areas
      2. Grids, logical values, and writing modes
      3. Masonry layout Experimental
    51. Images
      1. Basic concepts of logical properties and values
      2. Using CSS math functions
    52. Media queries
      1. Printing
    53. Nesting style rules
      1. Understanding z-index
    54. Scroll snap
      1. Basic shapes with shape-outside
      2. Using CSS transitions
    55. Center an element
    56. List group with badges
    57. Color picker tool
    58. Learn more
    59. See full compatibility
  • The object-fit <video>, should be resized to fit its container.

    Note: The object-fit property has no effect on <fencedframe> elements.

    You can alter the alignment of the replaced element's content object within the element's box using the object-position property.

    Try it

    object-fit: fill;
    
    object-fit: contain;
    
    object-fit: cover;
    
    object-fit: none;
    
    object-fit: scale-down;
    
    <section id="default-example">
      <img
        class="transition-all"
        id="example-element"
        src="/shared-assets/images/examples/plumeria-146x200.jpg" />
    </section>
    
    #example-element {
      height: 100%;
      width: 100%;
      border: 2px dotted #888;
    }
    

    Syntax

    css
    object-fit: contain;
    object-fit: cover;
    object-fit: fill;
    object-fit: none;
    object-fit: scale-down;
    
    /* Global values */
    object-fit: inherit;
    object-fit: initial;
    object-fit: revert;
    object-fit: revert-layer;
    object-fit: unset;
    

    The object-fit property is specified as a single keyword chosen from the list of values below.

    Values

    contain

    The replaced content is scaled to maintain its aspect ratio while fitting within the element's content box. The entire object is made to fill the box, while preserving its aspect ratio, so the object will be "pillarboxed" if its aspect ratio does not match the aspect ratio of the box.

    cover

    The replaced content is sized to maintain its aspect ratio while filling the element's entire content box. If the object's aspect ratio does not match the aspect ratio of its box, then the object will be clipped to fit.

    fill

    The replaced content is sized to fill the element's content box. The entire object will completely fill the box. If the object's aspect ratio does not match the aspect ratio of its box, then the object will be stretched to fit.

    none

    The replaced content is not resized.

    scale-down

    The content is sized as if none or contain were specified, whichever would result in a smaller concrete object size.

    Formal definition

    Formal syntax

    object-fit = 
    fill |
    scale-down

    Examples

    Setting object-fit for an image

    HTML

    html
    <section>
      <h2>object-fit: fill</h2>
      <img class="fill" src="mdn_logo_only_color.png" alt="MDN Logo" />
    
      <img class="fill narrow" src="mdn_logo_only_color.png" alt="MDN Logo" />
    
      <h2>object-fit: contain</h2>
      <img class="contain" src="mdn_logo_only_color.png" alt="MDN Logo" />
    
      <img class="contain narrow" src="mdn_logo_only_color.png" alt="MDN Logo" />
    
      <h2>object-fit: cover</h2>
      <img class="cover" src="mdn_logo_only_color.png" alt="MDN Logo" />
    
      <img class="cover narrow" src="mdn_logo_only_color.png" alt="MDN Logo" />
    
      <h2>object-fit: none</h2>
      <img class="none" src="mdn_logo_only_color.png" alt="MDN Logo" />
    
      <img class="none narrow" src="mdn_logo_only_color.png" alt="MDN Logo" />
    
      <h2>object-fit: scale-down</h2>
      <img class="scale-down" src="mdn_logo_only_color.png" alt="MDN Logo" />
    
      <img class="scale-down narrow" src="mdn_logo_only_color.png" alt="MDN Logo" />
    </section>
    

    CSS

    css
    h2 {
      font-family:
        Courier New,
        monospace;
      font-size: 1em;
      margin: 1em 0 0.3em;
    }
    
    img {
      width: 150px;
      height: 100px;
      border: 1px solid #000;
      margin: 10px 0;
    }
    
    .narrow {
      width: 100px;
      height: 150px;
    }
    
    .fill {
      object-fit: fill;
    }
    
    .contain {
      object-fit: contain;
    }
    
    .cover {
      object-fit: cover;
    }
    
    .none {
      object-fit: none;
    }
    
    .scale-down {
      object-fit: scale-down;
    }
    

    Result

    Specifications

    Specification
    CSS Images Module Level 3
    # the-object-fit

    Browser compatibility

    See also