Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is figcaption allowed to be a child of an <a> tag in HTML5?

I thought that in HTML5 you could have block elements as children of <a> elements, as understood from the spec:

https://www.w3.org/TR/html-markup/a.html#a

Although previous versions of HTML restricted the a element to only containing phrasing content (essentially, what was in previous versions referred to as “inline” content), the a element is now transparent; that is, an instance of the a element is now allowed to also contain flow content (essentially, what was in previous versions referred to as “block” content)—if the parent element of that instance of the a element is an element that is allowed to contain flow content.

But now when I checked my page with an HTML validator I found this error message:

Error: Element “figcaption” not allowed as child of element “a” in this context. (Suppressing further errors from this subtree.)

The code goes as follows:

<figure class="post">
  <a href="#" title="foo">
    <figcaption class="articuloInfo ">
      <h3>FOO</h3>
      <p class="fecha">4/04/2014</p>
      <div class="descripcion">
      </div>
    </figcaption>
    <div class="imagen">
      <img src="foo.jpg" alt="foo">
    </div>
  </a>
</figure>

Can someone explain me where the error is and why?

like image 937
Vandervals Avatar asked Jun 15 '16 09:06

Vandervals


People also ask

Is Figcaption an element in HTML5?

The <figurecaption> tag in HTML is used to set a caption to the figure element in a document. This tag is new in HTML5.

What is the use of Figcaption tag in HTML5?

The <figcaption> HTML element represents a caption or legend describing the rest of the contents of its parent <figure> element.

Which of the following can be parent tag for Figcaption?

HTML <figcaption> Tag.

Does Figcaption go inside figure?

The figcaption element represents a caption or legend for a figure. The <figcaption> element is optional and can appear before or after the content within the <figure> .


2 Answers

You should not use https://www.w3.org/TR/html-markup/ because it’s outdated (it’s a Working Group Note from 2013). The HTML5 specification is: https://www.w3.org/TR/2014/REC-html5-20141028/

For the figcaption element, the spec lists "Contexts in which this element can be used", wich are:

As the first or last child of a figure element.

It says child (not descendant), so it can’t have an a element as parent.

You could place the a element around the figure element instead (which is possible because of the part you quoted: a can contain flow content now):

<a href="#" title="foo">
  <figure class="post">
    <!-- … -->
  </figure>
</a>
like image 148
unor Avatar answered Oct 04 '22 02:10

unor


A figurecaption can have only figure element as its parent:

https://developer.mozilla.org/en/docs/Web/HTML/Element/figcaption

So a figure caption cannot be a direct child of an anchor tag.

like image 38
Arathi Sreekumar Avatar answered Oct 04 '22 01:10

Arathi Sreekumar