In this exercise we nested an <img> element within an <a> element to make images clickable links. Can we nest any element within <a> tags? (li elements?)
Answer
With HTML5, the <a> tag can now contain both inline and block-level elements. For example, we can nest heading elements within <a> tags like so:
<a href=”#”><h1>Example</h1> </a>
This would make the entire <h1> element clickable!
That having been said, one block-level tag which we do not want to nest within an <a> tag is the <li> tag. <li> tags should always be direct children to either <ol> , <ul>, or the experimental <menu> tags.
To learn more about inline and block-level elements, check out our “Learn CSS” course.
From the basic definition of an anchor, it is an inline element, not a container for other block level elements. This example is blatantly incorrect and (without yet testing) surely to raise some warnings from a validator.
<h1><a>Example</a></h1>
is more semantically clear. The inline element is contained by the block element. That makes sense.
An OL and UL parent element can only have LI as direct children. We would never wrap an LI in a link. The link would go inside that block level element.
Browsers are very forgiving and try their best to render something. The A in this example might even take the dimensions and position of the DIV, but it still goes against the grain. Inline elements should not be used to contain block level elements. It traces back to semantics and document outline.
Now as we’ve seen in terms of anchors, above, the same would apply to inline elements without a parent container. That is, inline elements floating in a sea of block elements. This is a rendering issue waiting to happen.
Inline elements need to be contained. Block level elements ARE containers. At the very minimum, images should be contained by a DIV, but a P (paragraph) is even better.
HTML5 gives us the FIGURE element which when studied will reveal so much of what early developers had to grapple with in terms of WCAG and SEO.
Pretty much anything goes in HTML. The browser will never raise an exception or even complain. That’s why the specifications are recommendations, not syntax rules.
HTML development is designed around document structure and the outline. Validators look for well-formedness. Accessibility testing looks for roughly the same thing, only along a stricter line. Don’t be surprised that anything one tries will work. Is it well formed, though? That’s the question a responsible developer will ask.
Does this mean that this should be <h1><a href="https://www.example.com">Click me!</a></h1>
instead of <a href="https://www.example.com"><h1>Click me!</h1></a>
This image in the link below is from the Learn React Course here in Codecademy. image|412x412
Technically, the anchor element may contain flow content and phrasing content, so H1 is an allowed child. Some of us still go by the old rule that stated inline elements should not contain block level elements. That was just a rule of thumb, though, not a spec.
To experiment with differences in the above examples, give the H1 a background color.
This makes sense. The block element wraps the inline element and there is no need for a printable character between the links (an accessibility requirement when links are next to each other).
In order to give an answer like that above we need to examine use case and context.