Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!
HTML5 Zone is brought to you in partnership with:

I'm a writer, programmer, web developer, and entrepreneur. Preona is my current startup that began its life as the team developing Twitulater. Our goal is to create a set of applications for the emerging Synaptic Web, which would rank real-time information streams in near real time, all along reading its user behaviour and understanding how to intelligently react to it. Swizec is a DZone MVB and is not an employee of DZone and has posted 17 posts at DZone. You can read more from them at their website. View Full User Profile

Firefox’s “funny” CSS3 Image Scaling Quirk

10.20.2011
Email
Views: 3097
  • submit to reddit
The HTML5 Microzone is presented by DZone and Microsoft to bring you the most interesting and relevant content on emerging web standards.  Experience all that the HTML5 Microzone has to offer on our homepage and check out the cutting edge web development tutorials on Script Junkie, Build My Pinned Site, and the HTML5 DevCenter.

Last night as I was rewriting the CSS for postme.me from scratch and I was cursing and shouting obscenities under my breath and trying to find a way to punch all of Mozilla in the face over the internet … I discovered something silly.


A quirk

Scaling images is something every developer on the web has had to come to grips with. Maybe you were implementing an image gallery and wanted to display thumbnails, maybe you’ve reimplemented all the scaling algorithms several times.

It’s surprising how complex something as simple as making sure no dimension is beyond a certain size while maintaining aspect ratio can be. Which is exactly why I wanted to avoid the whole thing this time ’round and let the browser take care of it all.

Should be pretty simple with CSS right?

Turns out it is, here’s everything you have to do:

img {
   max-width: 300px;
   max-height: 200px;
}

Casting aside concerns such as load times and bandwidth for a moment. This is an elegant solution to a hairy problem. Browsers as it turns out do exactly the right thing – make sure neither dimension is too big and carefully adjusting the other dimension to maintain aspect ratio.

But try putting that image into a flexbox div and strange things start happening in firefox.

Flexbox and image scaling in FF

What’s stranger still is that wrapping that same image into a vanilla div with zero styling not only fixes the image resizing issue, but also maintains centering and other flexbox properties you were originally going for.

And then it works

You can check it out in action here.

Not sure exactly what’s going on here, but  I’m glad I managed to find a fix for one strange thing among many. Now I have a website full of very very nonsemantic HTML simply  because of javascript’s quirks.

This brings up memories of times before I decided I don’t care about IE support anymore … granted, these features are far from standardized and it’s therefore to be expected strangeness might appear. But Firefox is not where I expected such problems to crop up.

Source: http://swizec.com/blog/firefoxs-funny-css3-image-scaling-quirk/swizec/2641

Published at DZone with permission of Swizec Teller, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

HTML5 is the most dramatic step in the evolution of web standards. It incorporates features such as geolocation, video playback and drag-and-drop. HTML5 allows developers to create rich internet applications without the need for third party APIs and browser plug-ins.  Under the banner of HTML5, modern web standards such as CSS3, SVG, XHR2, WebSockets, IndexedDB, and AppCache are pushing the boundaries for what a browser can achieve using web standards.  This Microzone is supported by Microsoft, and it will delve into the intricacies of using these new web technologies and teach you how to make your websites compatible with all of the modern browsers.

Comments

Lindsay Gordon replied on Mon, 2011/10/24 - 4:33pm

You've been on a run these past few days. Thanks for the great contributions!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.