Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is putting a div inside an anchor ever correct?

Tags:

html

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?

like image 335
Tom Avatar asked Dec 01 '09 18:12

Tom


People also ask

Can a div be inside an anchor?

Yes, it is possible in HTML5, because HTML5 allows <a> tag to be wrapped around a <div> element. So, <div> will be visible inside the anchor tag. In HTML 4.0, inline elements aren't allowed to be wrapped around block elements. But if you use “display:block” it will work fine.

Can you wrap a div in an anchor tag?

You can put an anchor tag inside of the divs... make it the last element in the divs, and style it to be display block, and to fill up 100% width and height (you may need to specify the height).

Can a div be inside a TD?

One important thing to remember when putting a div inside of a table is that the div needs to live inside of a particular table cell, meaning inside of a td or th element which is inside of a tr element. This can help with using absolute positioning inside of table cells as well.

What elements can be inside an anchor tag?

Inline elements ( a, span, strong, em among others ) can contain other inline elements and text nodes. An anchor can contain a span, which can contain a text node. Generally, block-level elements may contain inline elements and other block-level elements.


1 Answers

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 and block 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.

like image 67
NickFitz Avatar answered Sep 21 '22 14:09

NickFitz