Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it legal to have children of an anchor tag (<a>) in HTML?

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

like image 448
Parveez Ahmed Avatar asked Aug 17 '13 13:08

Parveez Ahmed


People also ask

What is anchor (< A >) tag?

The <a> tag (anchor tag) in HTML is used to create a hyperlink on the webpage. This hyperlink is used to link the webpage to other web pages or some section of the same web page. It's either used to provide an absolute reference or a relative reference as its “href” value.

Can you have an anchor within an anchor?

Nested links are illegal. Links and anchors defined by the A element must not be nested; an A element must not contain any other A elements.

Which are the valid HTML anchor tags?

The <a> nchor element is simply an anchor to or from some content. Originally the HTML specification allowed for named anchors ( <a name="foo"> ) and linked anchors ( <a href="#foo"> ).

Can an anchor tag have a name?

12.2.An anchor name is the value of either the name or id attribute when used in the context of anchors. Anchor names must observe the following rules: Uniqueness: Anchor names must be unique within a document. Anchor names that differ only in case may not appear in the same document.


2 Answers

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.

like image 73
Adrift Avatar answered Oct 22 '22 09:10

Adrift


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.

like image 3
Jukka K. Korpela Avatar answered Oct 22 '22 07:10

Jukka K. Korpela