Use iframes with XHTML 1.0 Strict

Nov 4, 2009

Recently I had to use iframes on a website conforming to XHTML 1.0 Strict. As you might know, the XHTML 1.0 Strict doctype doesn’t allow the use of iframes. The XHTML 1.0 Transitional doctype on the other hand, does allow you to use iframes, but I don’t like to use that doctype. The reason is, as the name implies, that it’s a doctype meant for make the transition from HTML into XHTML – a sort of a temporary solution.

When building new websites I like to use a strict doctype because it doesn’t allow for many of the style and behavioral tags that is much better placed in stylesheets and JavaScript.

What’s needed is a doctype that conforms to XHTML 1.0 Strict which also allows for iframes.

A solution

What I came up with was very simple, but may be considered a hack by some. Basically, I took the doctype declaration (DTD) of XHTML 1.0 Strict and added support for iframes. I found how iframes was supported in the transitional DTD and copied it to the Strict DTD. It allows for some attributes like width and height that the Strict DTD doesn’t, so I removed those and then added support for the allowtransparency attribute.

So to make iframes work on your own invalid XHTML 1.0 Strict page, just replace the doctype at the top of your pages with this new one:

<!DOCTYPE html SYSTEM "http://madskristensen.net/custom/xhtml1-iframe.dtd">

I suggest you download and host the DTD on your own server instead of using mine in case I forget to pay my hosting fee.

Check out the demo of XHTML 1.0 Strict with Iframe

A hack?

Some might say it’s a hack because by using this DTD the page is no longer XHTML 1.0 Strict. That is correct. It is now something new and different, but completely identical to XHTML 1.0 Strict with support for iframes. So it’s XHTML 1.0 Strict with Iframe.

XHTML have build in support for custom DTDs and thus this is completely supported and valid XHTML. If you don’t like using other doctypes than the few main ones created by the W3C then I have to ask why? What does it give you, your users or the quality of the page that this new one doesn’t?

In my book, it comes down to using a doctype that is based on known standards (XHTML 1.0 Strict in this case) so it still make sense to other devs when they read the markup. It’s also important that the DTD is strict (yep, this DTD is still strict), but most of all it’s important that the markup conforms correctly to the DTD so the entire page is valid. Remember, when using custom DTDs your page is still valid XHTML.

Note

I tried using XHTML 1.1 modules to build the DTD, but it never worked out for me. I got to the point where the iframe tag was valid, but not allowed in any other tags including body. I couldn’t seem to find a way to get full support for it. If you know how, please let me know.

* $4.95/month BlogEngine.net Hosting – Click Here!

Comments (13) -

Martin S.
Martin S. Sweden
11/4/2009 6:57:14 AM #

Interesting!

I wouldn't do anything like this though, instead I would use the transitional doctype on those pages where iframes are used.

By the way, iframes are evil and shall not be used. ;)

Mads Kristensen
Mads Kristensen United States
11/4/2009 7:06:38 AM #

The problem is that there is no real alternative to iframes. The object tag page-include doesn't work in IE. I also think iframes are evil, but if you need them, you need them.

Jason Derrasa
Jason Derrasa United States
11/4/2009 7:23:08 AM #

Thank you for the tutorial,but can google hurt ranking for this?Because google don't like iFrames.

Mads Kristensen
Mads Kristensen United States
11/4/2009 7:40:47 AM #

Google don't care if you use this new DTD instead of XHTML 1.0 Strict or Transitional when using iframes. Google doesn't read the DTD

Craig
Craig United States
11/4/2009 8:30:28 AM #

Thanks for an innovative solution.  By the way, the Google home page uses iframes.

Stickle Bricks
Stickle Bricks United Kingdom
11/5/2009 11:11:22 AM #

Thanks for posting, I can get a bit obsessive about XHTML validation, although thankfully I haven't needed to use iframes recently.

Out of interest, if the W3C have deemed that iframes are outdated, what methods do they propose should be used instead?

Mads Kristensen
Mads Kristensen United States
11/5/2009 12:40:54 PM #

You should use the object tag as shown here intranation.com/test-cases/object-vs-iframe/

The problem with using the object tag for this is that it doesn't work in IE

Kim Larsen
Kim Larsen Denmark
11/5/2009 3:01:02 PM #

Why XHTML when the language is dead (until it probably gets a new life in HTML5)?

Niche Profit Classroom
Niche Profit Classroom United States
11/6/2009 6:26:18 AM #

great solution on the iframes. Thank you. I'm going to try it on my blog..

Chris
Chris United States
11/6/2009 8:10:33 AM #

This is awesome. I can't even imagine the amount of headaches you just saved people from. IFrames are used so often, even today, and it's generally a pain in the @ss. Though I try to avoid using IFrames at all cost, this is definitely a great future resource. Thanks Mads!

Cheers,

Chris

matt
matt United States
11/6/2009 11:47:32 AM #

or just use the <object> tag. Easy.

Vinc
Vinc Netherlands
11/8/2009 4:02:52 AM #

Yeah true, but then ie won't show external url content when using objects.

Petr
Petr Czech Republic
11/6/2009 3:05:08 PM #

Conditional comments with object should work, but thank you for nice solution.
BTW: is there a way how to add own attributes and stay strict?

Pingbacks and trackbacks (3)+

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.