tags:

views:

347

answers:

8

I want to create something like

<menu>
    <lunch>
        <dish>aaa</dish>
        <dish>bbb</dish>
    </lunch>
    <dinner>
        <dish>ccc</dish>
    </dinner>
</menu>

Can it be done in HTML5? I know I can do it with

<ul id="menu">
    <li>
        <ul id="lunch">
            <li class="dish">aaa</li>
            <li class="dish">bbb</li>
        </ul>
    </li>
    <li>
        <ul id="dinner">
            <li class="dish">ccc</li>
        </ul>
    </li>    
</ul>

but it is so much less readable :(

+3  A: 

This is not an option in any HTML specification :)

You can probably do what you want with <div> elements and classes, from the question I'm not sure exactly what you're after, but no, creating your own tags is not an option.

Nick Craver
A: 

As Nick said, custom tags are not supported by any version of HTML.

But, it won't give any error if you use such markup in your HTML.

It seems like you want to create a list. You can use unordered list <ul> to create the rool elements, and use the <li> tag for the items underneath.

If that's not what you want to achieve, please specify exactly what you want. We can come up with an answer then.

Kirtan
It won't give you an error in the browser, but IE handles CSS on unknown tags oddly.
ceejayoz
Hmmm, now I didn't know that! I knew that the unknown CSS names are handled oddly (that's an error). But, are you sure it handles the unknown tags oddly too? Doesn't it just render them without the text in the `<>` tags?
Kirtan
@Kirtan: you can’t apply CSS to unknown elements in Internet Explorer. Try styling an `<abbr>` element in IE 7. It won’t work, because IE 7 didn’t implement `<abbr>` (even though it’s in the standard!). I have a grim little chuckle to myself when people blindly say that non-standard code won’t cause any errors. How the heck do you know it won’t cause any errors? It’s hard enough getting standard code to work, let alone non-standard code.
Paul D. Waite
+6  A: 

Creating your own tag names in HTML is not possible / not valid. That's what XML, SGML and other general markup languages are for.

What you probably want is

<div id="menu">
    <div id="lunch">
        <span class="dish">aaa</span>
        <span class="dish">bbb</span>
    </div>
    <div id="dinner">
        <span class="dish">ccc</span>
    </div>
</div>

Or instead of <div/> and <span/> something like <ul/> and <li/>.

In order to make it look and function right, just hook up some CSS and Javascript.

LukeN
No, it isn't. SGML was made for this long before XML was on the scene.
David Dorward
Is anyone using plain SGML today? Either people use HTML or XML and many of it's descendants, but i've never seen SGML in the wild! Anyways, I'll update my answer.
LukeN
I bet there are still some people using Docbook SGML for documentation. There certainly were lots just a few years ago.
Ken
A: 

As Michael suggested in the comments, what you want to do is quite possible, but your nomenclature is wrong. You aren't "adding tags to HTML 5," you are creating a new XML document type with your own tags.

I did this for some projects at my last job. Some practical advice:

  1. When you say you want to "add these to HTML 5," I assume what you really mean is that you want the pages to display correctly in a modern browser, without having to do a lot of work on the server side. This can be accomplished by inserting a "stylesheet processing instruction" at the top of the xml file, like <?xml-stylesheet type="text/xsl" href="menu.xsl"?>. Replace "menu.xsl" with the path to the XSL stylesheet that you create to convert your custom tags into HTML.

  2. Caveats: Your file must be a well-formed XML document, complete with XML header <xml version="1.0">. XML is pickier than HTML about things like mismatched tags. Also, unlike HTML, tags are case-sensitive. You must also make sure that the web server is sending the files with the appropriate mime type "application/xml". Often the web server will be configured to do this automatically if the file extension is ".xml", but check.

  3. Big Caveat: Finally, using the browsers' automatic XSL transformation, as I've described, is really best only for debugging and for limited applications where you have a lot of control. I used it successfully in setting up a simple intranet at my last employer, that was accessed only by a few dozen people at most. Not all browsers support XSL, and those that do don't have completely compatible implementations. So if your pages are to be released into the "wild," it's best to transform them all into HTML on the server side, which can be done with a command line tool, or with a button in many XML editors.

Dan Menes
+1  A: 

For embedding metadata, you could try using HTML microdata, but it's even more verbose than using class names.

<div itemscope>
    <p>My name is <span itemprop="name">Elizabeth</span>.</p>
</div>

<div itemscope>
    <p>My name is <span itemprop="name">Daniel</span>.</p>
</div>
Ionuț G. Stan
+1  A: 

Besides writing an XSL stylesheet, as I described earlier, there is another approach, at least if you are certain that Firefox or another full-fledged XML browser will be used (i.e., NOT Internet Explorer). Skip the XSL transform, and write a complete CSS stylesheet that tells the browser how to format the XML directly. The upside here is that you wouldn't have to learn XSL, which many people find to be a difficult and counterintuitive language. The downside is that your CSS will have to specify the styling very completely, including what are block nodes, what are inlines, etc. Usually, when writing CSS, you can assume that the browser "knows" that <em>, for instance, is an inline node, but it won't have any idea what to do with <dish>.

Finally, its been a few years since I tried this, but my recollection is that IE (at least a few versions back) refused to apply CSS stylesheets directly to XML documents.

Dan Menes
Yeah, I think IE goes into a special XML rendering mode, and pretty much shows you a prettified, collapsible version of the XML document.
Paul D. Waite
A: 

The point of HTML is that the tags included in the language have an agreed meaning, that everyone in the world can use and base decisions on, like default styling.

Made-up tags like yours are great for humans (because we can learn English and thus know what your tags mean), but not so good for machines.

Paul D. Waite
A: 

I'm not so sure about these answers. As I've just read: "CUSTOM TAGS HAVE ALWAYS BEEN ALLOWED IN HTML."

http://www.crockford.com/html/

The point here being, that HTML was based on SGML. Unlike XML with its doctypes and schemas, HTML does not become invalid if a browser doesn't know a tag or two. Think of <marquee>. This has not been in the official standard. So while using it made your html page "official unapproved", it didn't break the page either.

Then there is <keygen>, which was Netscape-specific, forgotten in HTML4 and rediscovered and now specified in HTML5. And also we have custom tag attributes now, like data-XyZzz="..." allowed on all HTML5 tags.

So, while you shouldn't invent a whole custom unspecified markup salad of your own, it's not exactly forbidden to have custom tags in HTML. That is however, unless you want to send it with an +xml Content-Type or embed other XML namespaces, like SVG or MathML. This applies only to SGML-confined HTML.

mario
Also of interest here:http://www.librador.com/2009/10/20/Styling-undefined-tag-names-in-HTML/"You can use any tag name in HTML, and it will be part of the DOM and can be styled."
mario
“So while using it made your html page "official unapproved", it didn't break the page either.” — um, try styling your invented elements in Internet Explorer. It won’t work. You might not consider that broken, but the fact that custom tags are invalid and don’t work in Internet Explorer, I don’t see how you can argue they’re “allowed”.
Paul D. Waite
Or how Douglas Crockford can either. Yeah, Crockford. I’m calling you out. *Bring it*, buddy.
Paul D. Waite
I mean, he doesn’t back up his statement with anything. Just because Douglas Crockford said it, it doesn’t mean it’s true.
Paul D. Waite