How to Styling iframes with CSS

Mar 16, 2012, by admin

If you still haven’t heard, hear it now: FRAMES ARE DEAD – well, in theory, they are more like the undead – the zombies of webdesign. They should be gone and forgotten but decline to stay buried. It’s much better not to use them.

However, there may be a circumstances, where including a separate html document into your page makes sense. And there’s an app for that: Iframes (aka inline frames). This code includes a basic page into an html page:

<iframe src=”http://www.killersites.com/”>

<p>Your browser does not support iframes.</p>

</iframe>

At this point, there is completely no styling applied to the iframe, and it looks like this. Note the line considering lack of browser support. Iframes are supported by all the major browsers, but it’s a good idea to add this for the odd browser that doesn’t.

While I am a strong supporter of using the HTML strict doctype and writing validating, standard-compliant code – iframes will not authenticate, which leaves us with two options: Either suck up the iframe-related validation errors or switch to HTML transitional doctype for the pages with iframes.

CSS provides you the power to dress your iframe up a bit. The following features are available:

src (the URL of your iframe)

longdesc (this specifies the URL of the iframe, but is hardly supported and can just as well be ignored)

name (obviously, the name of your iframe)

height (the height of your iframe)

width (the width of your iframe)

frameborder (1 or 0 – 1 is for yes, 0 for no)

marginheight (determines the top and bottom margin of your iframe)

marginwidth (determines right and left margins of your iframe

scrolling (Do you want a scrollbar? Yes, no, or auto – ‘Yes’, is apparent. ‘No’ should not be used – what if someone does not see the whole content? If this is set to ‘no’, that viewer can never get to the hidden parts of what you’re including. ‘Auto’ lets the browser choose if one is needed and looks the best option here to me.)

(align) – I’m just mentioning this for totality; it has been deprecated. Don’t use it.

So this inline CSS:

<iframe src=”http://www.killersites.com”

name=”Killersites.com” height=”400px” width=”700px”

frameborder=”0″ marginheight=”20″ marginwidth=”35″

scrolling=”auto”>

<p>Your browser does not support iframes.</p>

</iframe>

Gives you that. And if you add a bit more CSS to the frame itself:

iframe {

margin-left: 95px;

}