Valid XHTML to Embed a YouTube Video

Thu 2 Nov 2006

I have a confession to make.  For the last few days, my blog has not used valid XHTML.  But don’t panic!  I’ve fixed it.

Problem

The problem started when I used my first YouTube video in this post.  The code supplied by YouTube uses the now-deprecated <embed> tag:

<object width="425" height="350">
<param name="movie"
 value="http://www.youtube.com/v/mwc0wQt-L1Q"></param>
<param name="wmode" value="transparent"></param>
<embed src="http://www.youtube.com/v/mwc0wQt-L1Q"
 type="application/x-shockwave-flash"
 wmode="transparent" width="425"
 height="350"></embed>
</object>

Solution

Bernie Zimmerman has a solution that works.  He points out that by supplying correct type, style and data attributes to the <object> tag, we can leave out the <embed> tag altogether.

However, the solution I like best was suggested by minghong, one of Bernie’s commenters.  It avoided using the style attribute and used the width and height attributes instead.  Here’s what I eventually used:

<object type="application/x-shockwave-flash"
 width="425" height="350"
 data="http://www.youtube.com/v/mwc0wQt-L1Q">
<param name="movie"
 value="http://www.youtube.com/v/mwc0wQt-L1Q"></param>
<param name="wmode" value="transparent"></param>
</object>

But Why?

Using valid XHTML has a number of benefits.  As well as giving the widest range of compatibility across various web browsers, my blog can be read on mobile phones and devices for visually impaired and disabled people.

See, it’s not just because I am a bit OCD (according to Mrs Fitz, anyway).  When I go blind (The Pope told me I would), I’ll still be able to read my blog on my braille-reader.  (Although, music videos might be a bit of a challenge.)

8 Responses to “Valid XHTML to Embed a YouTube Video”

  1. Wile Coyote (Neil) Says:

    Going blind is not as much of an issue Mike… despite the Vatican threat… I mean music videos… who looks at them any more… They all look alike..

    Its the deafness that impacts more…

    I gather Im going deaf… Ive been tuning my wife out so much in the last few months that its actually become contagious… I tuned out a client because his problem became repetitive…. except that in one of the iterations he introduced a new element that I missed… This of course cost me dearly… as I did not get it into spec and missed a critical issue for them.

    Maybe i should use UTube to illustrate my point… nah… I will obviously have to get myself a brail UI…

    CHeers

  2. bonestorm Says:

    If I ever do get a mobile phone that can read blogs, this will be the first one I check out. :)

  3. Charlie Says:

    Thanks a lot Mike, I’ve been looking for this code to display correctly regardless of browser… fixed all my validation errors.

  4. MikeFitz Says:

    Neil: I don’t think music videos, as an artform, are going to die out any day (or generation) soon.

    Bonestorm: I use an O2 XDA II, which is both a phone and a handheld PC. (It’s a bit old now; it runs Windows Mobile 2003, not Windows Mobile 5.) When I bought it, I could tell Mrs Fitz that I wasn’t buying a fancy phone; I was buying a software development platform!

    Charlie: Happy to help.

  5. Hans Henrik Says:

    Just wanted to say thanks for the guide. I’m trying to keep my site valid (HTML 4.01 Transitional), so this really helps. Found out that if you’re using HTML 4.01 Transitional, you can skip the "'s".

    Once again, thanks!

  6. MikeFitz Says:

    You’re welcome, Hans.

    Nice site, by the way. I especially enjoyed the links to Pink Floyd’s work.

  7. Indiana Jones Says:

    You can also use this simple too to make your embed code valid XHTML: http://www.tools4noobs.com/online_tools/youtube_xhtml/

  8. yipyipdog Says:

    I have blue american pitbull puppies for sale at my website and my wife gave me this youtube embed code to put up on some of the videos she made about our pitbull puppies and it messed up my perfect xhtml. thanks so much for this info.

    yipyipdog

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Comment moderation is in use. Please do not submit your comment twice -- it will appear shortly.