I've heard that putting a block element inside a inline element is a HTML sin:
<a href="http://www.mydomain.com"><div>
What we have here is a problem.
You see, an anchor element is an inline element,
and the div element is a block level element.
</div></a>
But what about if you style the outer anchor as display:block
in the stylesheet? Is it still wrong? The HTML 4.01 spec on block-level and inline elements seems to think so:
Style sheets provide the means to specify the rendering of arbitrary elements, including whether an element is rendered as block or inline. In some cases, such as an inline style for list elements, this may be appropriate, but generally speaking, authors are discouraged from overriding the conventional interpretation of HTML elements in this way.
Does anyone have any further tips about this issue?
No it won't validate, but yes it generally will work in modern browsers. That being said, use a span inside your anchor, and set
display: block
on it as well, that will definitely work everywhere, and it will validate!You can't put
<div>
inside<a>
- it's not valid (X)HTML.Even though you style a span with display: block you still can't put block-level elements inside it: the (X)HTML still has to obey the (X)HTML DTD (whichever one you use), no matter how the CSS alters things.
The browser will probably display it as you want, but that doesn't make it right.
There's a DTD for HTML 4 at http://www.w3.org/TR/REC-html40/sgml/dtd.html . This DTD is the machine-processable form of the spec, with the limitation that a DTD governs XML and HTML 4, especially the "transient" flavor, permits a lot of things that are not "legal" XML. Still, I consider it comes close to codifying the intent of the specifiers.
I would interpret the tags listed in this hierarchy to be the total of tags allowed.
While the spec may say "inline elements," I'm pretty sure it's not intended that you can get around the intent by declaring the display type of a block element to be inline. Inline tags have different semantics no matter how you may abuse them.
On the other hand, I find it intriguing that the inclusion of
special
seems to allow nestingA
elements. There's probably some strong wording in the spec that disallows this even if it's XML-syntactically correct but I won't pursue this further as it's not the topic of the question.If you change it to a block-style element, then no, it's no longer 'wrong', but it probably won't validate. But it doesn't make much sense to do what you're doing. You should either just keep the anchor tag as a block level element with no inner div, or put the div on the outside.
If you want to avoid the semantic trouble of placing divs inside anchor tags, just place the anchor tag on the same level as the divs, wrap them all with a container with position: relative, make your anchor tag position: absolute and expand it to fill the container. Also if it's not on the end of the content flow make sure you throw a z-index in there to place it above the content.
As suggested I have added a markup code:
And the css:
Block level elements like
<div>
can be wrapped by<a>
tags in HTML5. Although a<div>
is considered to be a container/wrapper for flow content and<a>
's are considered flow content according to MDN. Semantically it may be better to create inline elements that act as block level elements.