Object-fit not affecting images
Losing a battle against object-fit? Make sure your image has set width and height attributes. The object-fit property can take cover, contain, fill, none, or scale-down as values to suit your desired look. Here's a quick demo:
The 'how-to' guide
Tick the checkbox: container dimensions
object-fit can be tricky and it requires specific conditions to shine. One tricky bit is the parent container: when you're using width and height in percentages, they need a parent with defined dimensions - your img tag needs a mom and dad, essentially.
Mind the little/big obstacles
‘Houston, we have a problem… or maybe more.’ Be aware of the max-width property or some inconspicuous surrounding elements which could interfere with your object-fit progression. CSS is known to have an attitude sometimes.
Battle the 'whitespace' monsters
Keep an eye out for mysterious whitespace that may creep up beneath your images due to their inline-block behavior. Vanquish it with the vertical-align property set to bottom or middle.
Flexbox: Your alignment sidekick
Flexbox is like your best friend - always there when you need to align things up nicely.
Mastering object-fit
Say hello to 'object-position'
With object-position you gain control over the clear visual focus of the image. Think of it as googly eyes for your object-fit clown picture.
Always fashionably responsive
Maintain that style while being responsive. Use the max-width and height: auto properties to ensure your image fits any party it goes to.
Browser compatibility and fallbacks
Like all cool things in life, object-fit is not universally supported. Always check browser compatibility. When in doubt, use a polyfill like object-fit-images as your fallback knight in shining armor.
Was this article helpful?