Friday, October 1, 2010

Problems with iframes in Css

Problem: Internet Explorer spawns both horizontal and vertical scroll bars when content is
larger than the declared width or height. This means that if your iframe is 200 pixels high,
but your content is 400 pixels high, you’ll end up with a vertical scroll bar and a horizontal
one, even if your content is narrower than the iframe dimensions. Other browsers don’t
make this mistake, displaying only the relevant scroll bar. Also, styling iframes can cause
problems. Turning off the default border is a good move, because it looks clunky. Adding
a border using CSS should be possible by applying it directly to the iframe (via a class or
iframe tag selector); in practice, however, this partially fails in Internet Explorer versions 6
and below, creating an ugly gap between your scroll bars and iframe borders (which happens
to be the same size as the defined border).


Solution: If you know your iframe content is always going to be too large for the iframe,
set scrolling="yes" in the iframe start tag. Alternatively, add a conditional comment in
the head of the iframe content document, with the following code, experimenting with
the width property until the scroll bar disappears. If you use similar iframes on a number
of pages, you should instead assign a class value to the body element of the relevant pages
and define the html, body rule in an IE 6-and-below-specific style sheet.

<!--[if lte IE 6]>
<style type="text/css">
html, body {margin:0; width:180px;}
</style>
<![endif]-->
For border styles, you can work around the problem in one of two ways: you can override
the original border value, setting it to 0 for Internet Explorer 6 and below; or you can nest
the iframe in a div and provide the div with a border instead.

No comments:

Post a Comment