That's a good thing! There are a number of possible values for object-fit, but the most-used of them all is object-fit: cover;. All comments are held for moderation. This asks the browser to fill the rectangle with the image by cropping it, rather than stretching it. or "Tricks".

Use fallbacks and lightweight shims where necessary – but let’s not ruin things for the good browsers! your-favorite-image { object-fit : contain; font-family : 'object-fit: contain;' ; } or, if you also need object … . squeezed to fit the container of 200x400 pixels, and its Used by itself, object-fit lets us crop an inline image by giving us fine-grained control over how it squishes and stretches inside its box. But in Internet Explorer, it looks a little funny…. Using the right markup to describe your document is paramount; but there’s no reason for things to look awful. Be helpful and kind and yours will be published no problem. Can we apply object-fit on background images? I still haven’t hopped on the “IE’s okay, now!” train, so I don’t know if it really IS legit, but it does seem worth giving a shot. CSS-Tricks* is created, written by, and maintained by Chris Same here. Alternatively, only fix the images you want, once: You can call objectFitImages() on the same elements more than once without issues, for example to manually request an update of the object-fit value. This property tells the content to fill the container in a variety of ways; such as So let’s use Modernizr for feature detection and fix this with some JS: This will copy the src of our image to the background image source of the container. The tech stack for this site is fairly boring. The object-position property is used in conjunction with object-fit property and defines how an element such as a video or image is positioned with X/Y coordinates inside its content-box. height:200px; You can get the layout benefits of background-image with images without sacrificing the data that you need outside of your side to display your post properly. overflow:hidden; Great post :) I Love object fit! The butchery that took place before object-fit became available caused images to be removed ifrom the document flow. Maybe someone will propose srcset for background images in the css spec instead though. Object-fit CSS property has a decent support in modern browsers, except in all IEs and even Edge (update 2017–10–18: Edge 16 now support object-fit). https://github.com/aFarkas/lazysizes and supports object-position too! No mention that you can use same notation as background-position Thank you so much for this (object-fit: contain). Constance, your polyfill is fantastic. If nothing happens, download the GitHub extension for Visual Studio and try again. If we use object-fit: cover; it will cut off the sides of the image, The default value for object-position is 50% 50% when using the object-fit property on an image, so that by default all images are positioned in the center of their content box, like so: Beneath are a few examples as to how we can manipulate the object-position of an image with the object-fit property set to none. I also

We use essential cookies to perform essential website functions, e.g. The whole point of the polyfill is to make IE/Edge be nice. In one of those cases, use the watchMQ option: We use optional third-party analytics cookies to understand how you use GitHub.com so we can build better products. This is complimented by the second part, which is to replace the original image source with a transparent image.
It is altogether 10 lines of JS and 10 lines of CSS code. Save my name, email, and website in this browser for the next time I comment. CSS defining object-fit and a special font-family property to allow IE to read the correct value . * for videos: https://github.com/jonathantneal/fitie, Just in case someone needs a IE9, IE10, Edge hack solution, I have a workaround, that produces not actually a cropping, but a IMHO acceptable fallback for IE9+: The object-position property is used in conjunction with object-fit property and defines how an element such as a video or image is positioned with X/Y coordinates inside its content-box. Your email address will not be published. text-indent: -100%; Old question, but in my case i found using position:fixed solved it for me. ShopTalk is a podcast all about front-end web design and development. "preserve that aspect ratio" or "stretch up and take up as much space as }, The browser support is wrong for Safari, which doesn’t support object-position at all. element.innerHTML = “what the hell?”; It’s worth noting that by default the image is centered within its content box but this can be altered with the object-position property. You may write comments in Markdown thanks to Jetpack Markdown.