25

Is it legal to have children of an anchor tag in HTML? For an example:

 <a>
     <font>Example</font>
     <img src="example.jpg"/>
 </a>

It works fine in the browsers.But is it valid? Please help

Parveez Ahmed
  • 1,325
  • 4
  • 17
  • 28
  • 23
    Yes it is legal. The HTML police won't try arresting you. – Austin Brunkhorst Aug 17 '13 at 13:57
  • 3
    It used to be that some elements were legal, some were not. For example, you could not place block elements within the a tag (div, ul, p, etc). Inline elements were acceptable (span, font, img, em, strong, etc). But apparently html5 changed all that! – random_user_name Aug 17 '13 at 13:58
  • 3
    very old school, btw. – fred02138 Aug 17 '13 at 13:58
  • 3
    @cale_b: Yes, HTML5 does change it (to be inline with what browsers actually did anyway), *depending* on the context in which the `a` tag is used. `a` has become "transparent," which basically means it adopts whatever its container's content model is. So the above is valid **provided** it's valid to have a `` and `` in that location. (Of course, it's *never* valid to have a `` tag anywhere, as of HTML5.) – T.J. Crowder Aug 17 '13 at 14:09
  • The question now is, is it good practice? – carloswm85 May 04 '20 at 13:33

2 Answers2

32

Yes - even more so with the advent of HTML 5 (From the spec):

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.

Adrift
  • 58,167
  • 12
  • 92
  • 90
3

Yes, all versions of HTML allow some elements inside an a element. The set of allowed elements depends on the HTML version. The code posted is valid HTML 3.2 and HTML 4.01 as far as element nesting goes, though the img element is not valid HTML 4.01 due to lack of alt attribute.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390