2

I have what seems to be a simple question: I have a table that I am making as an accessible friendly element, including using the <caption> tag for the table title. This is all fine, but my element titles are styled into a ribbon which has the following structure:

<div class="ribbon"><h2></h2></div>

I have read that the caption for the table must come directly after opening the table tag, so my question is: can I wrap my caption in a div and it still be interpreted as a valid caption for a table like so?

<div class="ribbon"><caption>Title Here</caption></div>

3
  • What is the div for? Can you apply your style (or id) to the caption tag instead> Commented Oct 1, 2012 at 10:43
  • The outer div is necessary but what about doing something like: <caption><span>Title</span></caption> would that be valid? All I need is 2 block elements, the styling can be applied to whatever.
    – user282835
    Commented Oct 1, 2012 at 10:47
  • I've just done it as I suggested above and it works like a charm, but now the question is: is it still valid? :P
    – user282835
    Commented Oct 1, 2012 at 10:52

2 Answers 2

2

No, so use a span inside a caption.

1

No, it won't. That HTML is invalid.

From the HTML 5 specification:

Contexts in which this element can be used: As the first element child of a table element.

6
  • Although I did just run it through W3C validation and it passed, why does that mean then? Does it just do syntax analysis but not semantic?
    – user282835
    Commented Oct 1, 2012 at 10:48
  • @WilliamHand — Exactly what input did you give it? The validator should report an error if you use an element somewhere it is not allowed.
    – Quentin
    Commented Oct 1, 2012 at 10:52
  • I'm currently building the site so I put in the dev URL and it validated fine, no errors. It is still inside a table, but just inside another div so it becomes Table > Div > Caption rather than Table > Caption. I have made an alteration and tried it Table > Caption > Span which gets the effect for me, is this valid? (See comment on question).
    – user282835
    Commented Oct 1, 2012 at 10:55
  • 1
    Captions have never been allowed as child elements of divs, and divs have never been allowed as child elements of tables. HTML 5 just has the most readable expression of that.
    – Quentin
    Commented Oct 1, 2012 at 10:57
  • 1
    A span may be a child element of a caption.
    – Quentin
    Commented Oct 1, 2012 at 10:58