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?
The <figurecaption> tag in HTML is used to set a caption to the figure element in a document. This tag is new in HTML5.
The <figcaption> HTML element represents a caption or legend describing the rest of the contents of its parent <figure> element.
HTML <figcaption> Tag.
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> .
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With