Search code examples
htmlcssxhtmlprinciples

Is there anything wrong with writing parts of a webpage in XHTML with an HTML doctype?


I've just recently started learning HTML/CSS and I've been trying to teach myself sound web programming practices. I'm familiar with XML, so coding up webpages in XHTML was intuitive enough and it just seemed like a sound thing to do. However, I've been reading articles like this one and now I'm ambivalent.

My concerns with coding in either HTML and XHTML stem from the following:

  1. img tags do not need to be closed in HTML, and that makes sense to me. But in XHTML, img tags require a close tag which seems kind of funky. I just think it's weird to have a pair of open and close tags with nothing in between.
  2. p tags (as in paragraph) do not need to be closed in HTML, and that is funky to me. It makes sense to section off a paragraph between open and close tags as you would in XHTML.

These are the only basic examples I can come up with at the moment, but I'm hoping that they at least show how split I am between HTML and XHTML. So here's my actual question: Is it okay to throw in XHTML code (when I feel that it appropriately clarifies something) when I'm coding up a webpage that should be in HTML (with an HTML DTD)? I'm sure the website would look the same, but if people were to look at my source, would they think I were a terribly unprofessional coder?

Also, I've been sort of blindly looking through tutorials and guides via Google on sound web programming practices and I was wondering if you folks had any advice/resources to share with me. I really want to make sure I'm learning how to do this the right way. I would really appreciate it.

Thanks for reading!

Edit: Wow, such fast responses; you guys are awesome! So I understand that web browsers are going to be pretty lenient when it comes to this sort of stuff. My problem is then a question of vanity and how other professionals feel about writing a sort of mix of XHTML and HTML. Is strictly compliant code the way to go? I honestly just don't want to look like an idiot, haha.


Solution

  • img tags do not need to be closed in HTML, and that makes sense to me.

    No tag needs to be closed. All elements need to be closed.

    Most elements consist of a start tag, some content, and an end tag.

    Since an image element cannot have content, the end tag is not needed. In HTML, elements which cannot have content ("EMPTY elements") have the end tags forbidden.

    But in XHTML, img tags require a close tag which seems kind of funky.

    XHTML is an XML application rather than an SGML application, and XML doesn't have all the features that SGML does — including the ability to specify that an end tag is forbidden.

    I just think it's weird to have a pair of open and close tags with nothing in between.

    XML introduces a new kind of tag — a self-closing tag (<foo />) which acts as a start and end tag combined.

    If you are writing HTML-Compatible XHTML (which you should be if you choose to use XHTML and want to support browsers which don't support that language — such as Internet Explorer), then you should use that syntax. (I prefer to stick to HTML myself)

    p tags (as in paragraph) do not need to be closed in HTML, and that is funky to me.

    Many elements in HTML have optional end tags. This allows the end tag to be omitted when it can be implied.

    For example:

    <p>Hello, world
    <p>A second paragraph
    

    Since a p element cannot contain another p element, you can imply (with 100% reliability) that starting a new paragraph will close the previous one.

    That said, they are optional, not forbidden. You can write HTML 4.01 with explicit end tags for any element which isn't intrinsically empty.

    Is it okay to throw in XHTML code (when I feel that it appropriately clarifies something) when I'm coding up a webpage that should be in HTML (with an HTML DTD)? I'm sure the website would look the same, but if people were to look at my source, would they think I were a terribly unprofessional coder?

    It would look unprofessional.

    That said, there are only three times when XHTML syntax is not conforming HTML syntax:

    Empty elements — such as <img> and <br> need to be explicitly closed in XHTML … but you said you didn't like that syntax.

    Scripts and styles — you need to use character references or CDATA markers if you use certain characters in your embedded JS and CSS in XHTML, but these elements are marked as CDATA in the DTD for HTML, so this would be wrong there … but you shouldn't use embedded scripts or styles anyway.

    Namespaces — but there is no time when using an XML namespace in the middle of an HTML document would come close to clarifying something.