How to Fix Wrong-sized SWFs in Firefox

Flash Web Development

Under certain circumstances, SWFs in Firefox display at the wrong size (usually too small) when their width and height attributes are set to a percent in HTML.  There’s nothing wrong with using percents for width and height — as you’ve likely seen, “full screen” SWFs are a popular practice — but there is a wrong way to code for it.  If you’ve run into this scenario, you’ll be happy to hear the solution is simple.  This is neither a bug in Flash nor Firefox, as a matter of fact, and is resolved with a better understanding of HTML. 

An answer, short and sweet

Check your HTML in the document that contains your SWF.  Is there a document type declaration (DOCTYPE) at the top?  If so, that’s the source of the erroneous display.

Now wait!  Don’t delete that DOCTYPE just yet.  Sure, you can remove that line, and if you do, your SWF will display at the expected size in Firefox, but DOCTYPEs are a good thing.  By removing the DOCTYPE, you’re putting your browser into so-called “quirks mode,” which means that any HTML in the document will be interpreted not according to W3C standards, but according to the whim of whoever built the browser you’re using.  You might compare this to ignoring the Oxford English Dictionary in favor of an arbitrary dictionary written by a local literary club.  Sooner or later, one or more word definitions will veer from what’s officially correct, and you may end up with your foot in your mouth.  See http://www.alistapart.com/articles/doctype/ for more details.

Current best practices strongly recommend coding to standards, so let’s do that, instead.  And let’s do it quickly, while this answer is still “short and sweet.”  ;)

Add the following <style> element to your HTML document.  Put it inside the <head> element, as follows.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Percent-based SWF</title>
  <style>
    html, body { height: 100%; }
    html { overflow: hidden; }
  </style>
</head>

How it works

The <style> element lets the browser know that some or all of its styling choices will be determined by CSS (Cascading Style Sheets), which actually means determined by you.  Apparently, the DOCTYPE for XHMTL 1.0 (and others, as you’ll see) dictates that 100% height doesn’t always apply.  To let the browser know how you would like things interpreted, supply the above two lines of CSS.  The first tells both the <html> and <body> elements to set their own heights to 100%.  (If this begins to sound like a circular argument, don’t worry — it does to me, too.)  In essence, this “opens up” the document’s overall height to allow room for internal elements to do the same.  The second line corrects a side-effect of the first line.  Leave it out to see what I mean.

Variations

If you’re going for a 100%-sized SWF, you probably want to get rid of the natural HTML page margins while you’re at it.

<style>
  html, body { height: 100%; }
  html { overflow: hidden; }
  body { margin: 0; }
</style>

18 Responses to “How to Fix Wrong-sized SWFs in Firefox”

  1. Andy Says:

    Nope, still too small, but has the added drawback of also removing the vertical scroll bars…
    Doesn’t work for me I’m affraid

  2. David Stiller Says:

    Andy,

    With a full-browser SWF, you wouldn’t need the scrollbars, of course. ;)

    What platform are you on? I’ve used the above approach for dozens of websites.

  3. Don Lapre is a Superstar Says:

    This is highly useful. Unfortunately some web developers till do not care about the Firefox users. This just one of many problem . Hope they will learn from you at least

    Don Lapre is a Superstar
    webmaster@j-ams.org
    www.j-ams.org

  4. Jonny Says:

    Hi David,

    I have the same problem as Andy – initially I placed the code in my external style sheet… didn’t have any effect, so to try again I placed the style code directly into the head of my html page which holds the swf file… still no joy.

    I’m testing my ste on a Dual Core Mac, OS X 10.4.

    Regards Jonny

  5. Jeremy Says:

    Thanks for the tip on fixing my Firefox problem. Andy could fix his lack-of-scrollbars problem by changing the style

    html { overflow: hidden; }

    to

    html { overflow: shown; }

    worked for me.

  6. David Stiller Says:

    Jeremy,

    Glad to help. :)

    One note about your CSS comment. The overflow property has a handful of possible values, but, for better or worse, “shown” isn’t one of them. Different browsers handle things differently, of course, but my guess is that the “shown” is ignored and that browser’s default behavior is followed, same as if the overflow property hadn’t been specified.

  7. Jeremy Says:

    oops!

    that was a mistake of accidently mixing some javascript I wrote with css and making nonsense.

    Instead, removing the { overflow: hidden } fixed my page, like you said, reverting to the default behavior. I needed to to this since I have a shockwave file of a map that exists among some other text and such –not just a full page Flash site.

  8. Jonny Says:

    David,

    Thanks for your help with this. I’ve solved my problems using SWFobject in conjunction with CSS, works so well I’ll be applying this to every flash site I produce from now on!

    I have 3 to do in the next 3 months so a lesson well learnt for me!

    And SWFobject – what a perfect solution to the ever present problems of different browsers with different Flash plugins.

    Thanks again.

    Jonny

  9. David Stiller Says:

    Glad to hear it, Jonny! :)

  10. Neil Says:

    Thanks David.
    I’ve been trying to sort that out for ages! I was starting to get desparate!

    Works perfectly!

  11. David Mead Says:

    I haven’t tried it with firefox yet, but this fixed the same problem in Opera. Thanks a lot David.

  12. Jim Says:

    Hi david,

    Thanks for this. If I hadn’t of found this blog it might have taken me months to find the solution. Now I can get on with developing our band website.

  13. David Stiller Says:

    To David …

    Sure thing!

    To Jim …

    Glad it worked for you, man.

  14. Kirsty Says:

    Hi,

    I briefly thought this was going to be the answer to my problem, unfortunately, it doesn’t work for me though. SWF is still displayed at the same (tiny) size, this workaround doesn’t seem to make any difference.

    Sadly, the only thing that does work is to delete the DOCTYPE - exactly what I want to avoid. I’ve tried various DOCTYPES but none of them work for Firefox.

  15. Ryan Says:

    This saved my from a huge headache. I owe you one!

  16. Rod Says:

    Completely screwed my page up - won’t display in IE or FF

  17. David Stiller Says:

    To Ryan …

    Glad to hear it!

    To Rod …

    I can’t tell from your comment if you think it was your own effort or the suggestion in this article that screwed you up, but I’ll be happy to take a look at your site. What’s the URL?

  18. David Stiller Says:

    [Followup on Kirsty …]

    Kirsty and I touched bases via email, but she ended up getting pulled onto other projects, so her case might just be a mystery. Good to have one of those every now and then. ;)

    I hope you did manage to find a useful solution, Kirsty. Write back any time!

Leave a Reply