Is putting a div inside an anchor ever correct?
I've heard that putting a block element inside a inline element is a HTML sin:
开发者_开发问答<a href="http://example.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?
Depending on the version of HTML you're catering to:
HTML 5 states that the
<a>
element "may be wrapped around entire paragraphs, lists, tables, and so forth, even entire sections, so long as there is no interactive content within (e.g. buttons or other links)".HTML 4.01 specifies that
<a>
elements may only contain inline elements. A<div>
is a block element, so it may not appear inside an<a>
.Of course you are at liberty to style an inline element such that it appears to be a block, or indeed style a block so that it is rendered inline. The use of the terms
inline
andblock
in HTML refers to the relationship of the elements to the semantic structure of the document, whereas the same terms in CSS are related more to the visual styling of the elements. If you make inline elements display in a blocky manner, that's fine.However you should ensure that the structure of the document still makes sense when CSS is not present, for example when accessed via an assistive technology such as a screen reader - or indeed when examined by the mighty Googlebot.
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!
The W3C doc doesn't use concepts like wrong and sin, but it does use those like provide the means, may be appropriate and discouraged.
Actually, in the second paragraph of section 4, the 4.01 spec itemizes its words as follows
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in [RFC2119]. However, for readability, these words do not appear in all uppercase letters in this specification.
With that in mind, I believe the definitive statement is in 7.5.3 Block-level and inline elements, where it says
Generally, inline elements may contain only data and other inline elements.
The condition "generally" appears to introduce enough ambiguity to say that HTML 4.01 does allow inline elements to contain block elements.
Certainly, CSS2 has a display property value, inline-block, that appears to be suited to the purpose you describe. I'm not sure if it was ever widely supported, but it seems that someone anticipated the need for that kind of behavior.
The DTD appear to be less forgiving here, but the text of the DTD defers to the spec:
The HTML 4.01 specification includes additional syntactic constraints that cannot be expressed within the DTDs.
In another comment, you suggest that you want to make a block active by wrapping it in an anchor. I don't believe HTML prohibits that, and CSS clearly allows it. So to answer the title question about whether it is ever correct, I say yes. By the standards, it is sometimes correct.
With HTML5 specification... It is now possible to put a block-level element inside of an inline element. So now it's perfectly appropriate to put a 'div' or 'h1' inside of an 'a' element.
Update in Jul, 2022: latest HTML5 version
- Nothing is wrong with placing a
div
insidea
tag. In fact, you can place just about anything insidea
tag as long as they are categorized astransparent
, except that no descendant may beinteractive content
(eg: buttons or inputs) or ana
element, and no descendant may have a specifiedtabindex
attribute. Please refer to their documentation: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#properties. - If you inspect Angular Material official website, you can tell that they are indeed using
div
insidea
element.
This is an example I took from their website:
<a href="/components/badge">
<div class="mat-list-item-content">
<div mat-ripple="" class="mat-ripple mat-list-item-ripple"></div>
<div class="mat-list-text"></div>
</div>
</a>
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.
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.
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:
<div class="div__container>
<div class="div__one>
</div>
<div class="div__two">
</div>
<a href="#"></a>
</div>
And the css:
.div__container {
position: relative;
}
.div__container a {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
z-index: 999;
}
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.
<!ELEMENT A - - (%inline;)* -(A) -- anchor -->
<!ENTITY % inline "#PCDATA | %fontstyle; | %phrase; | %special; | %formctrl;">
<!ENTITY % fontstyle "TT | I | B | BIG | SMALL">
<!ENTITY % phrase "EM | STRONG | DFN | CODE | SAMP | KBD | VAR | CITE | ABBR | ACRONYM" >
<!ENTITY % special "A | IMG | OBJECT | BR | SCRIPT | MAP | Q | SUB | SUP | SPAN | BDO">
<!ENTITY % formctrl "INPUT | SELECT | TEXTAREA | LABEL | BUTTON">
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 nesting A
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're going to go to the effort of making <a>
block, why not put <a>
inside the div, being a block element it'll give you the same effect.
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.
you can achieve this by adding "::before" Pseudo-element
Pure CSS Trick ;)
a:before{
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 1;
pointer-events: auto;
content: "";
background-color: rgba(0,0,0,0);
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="card" style="width: 18rem;">
<img src="https://via.placeholder.com/250" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">Card with stretched link</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#" class="btn btn-primary stretched-link">Go somewhere</a>
</div>
</div>
Anything works ... until you nest them.
You can stuff in tables and divs and birds and bees, and the browser will patiently play along. But as soon as you put another <a>
tag inside, it will break them apart, putting the inner tag after the closing of the outer one. It's worth a try, even in the F12 console, you'll be surprised how strict your browser can be when it doesn't like something.
So there is NO SUCH THING as:
<a href="#1">
<a href="#2"> Now what </a>
</a>
Instead, it will become:
<a href="#1"></a>
<a href="#2"> Now what </a>
The same thing happens with <p>
and a few others; in fact, there's a Stacko article about this - Can I nest these tags? - and the only answer has the key, look for the bulleted list at the "HTML specifications indicate" part. The pickiest ones are clearly a
and p
, with honorable mentions like h1..h6
. (Now let's be honest: have you ever used a p
tag specifically because it gave you something no one else could?... Thought so. I'd just ditch it.)
TL;DR - you can't put a
inside a
.
I think that most of the time when people ask this question, they have build a site with only divs, and now one of the div needs to be a link.
I seen someone use a transparent empty image, PNG, inside an anchor tag just to make a link inside a div, and the image was the same size as the div.
Pretty sad actually...but it works...
It's wrong. Use a span.
Just as an FYI.
If your goal is to make your div clickable you can use jQuery / Java Script.
Define your div like so:
<div class="clickableDiv" style="cursor:pointer">
This is my div. Try clicking it!
</div>
Your jQuery would then be implemented like so:
<script type="text/javascript">
$(document).ready(function () {
$("div.clickableDiv").click(function () {
alert("Peekaboo");
});
});
</script>
This would also work for multiple divs - as per Tom's comment in this thread
精彩评论