Can we use <a> directly in the body, or it should always in any block level tag? See example (XHTML)
Asked Answered
A

5

2

Which is right? This:

<h2>heading 2</h2>
<p><a href="#" target="_blank" title="Opens in a new window">link 1</a></p>
<h2>heading 2 </h2>
<p><a href="#" target="_blank" title="Opens in a new window">link 2</a></p>
<h2>heading 2 </h2>
<p><a href="#" target="_blank" title="Opens in a new window">link 3</a></p>
<h2>heading 2 </h2>
<p><a href="#" target="_blank" title="Opens in a new window">link 4</a></p>

or this:

<h2>heading 2</h2>
<a href="#" target="_blank" title="Opens in a new window">link 1</a>
<h2>heading 2 </h2>
<a href="#" target="_blank" title="Opens in a new window">link 2</a>
<h2>heading 2 </h2>
<a href="#" target="_blank" title="Opens in a new window">link 3</a>
<h2>heading 2 </h2>
<a href="#" target="_blank" title="Opens in a new window">link 4</a>
Aristocratic answered 28/12, 2009 at 5:39 Comment(0)
S
0

@Jitendra, I agree with your point .. but only if you are using strict.dtd (HTML/XHTML)
According to W3C recommendation(for strict dtd) .. the tag <a> must be used in a block element.

Look at the following example:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
 <head>
   <meta content="text/html; charset=utf-8" http-equiv="content-type"/>
   <title>example</title>
 </head>
 <body>You don't have sufficient privileges to access the page.<a href="www.google.co.in">Click here</a> to go back.
 </body>
</html>

The above code produces error while doing HTML validation .. saying

document type does not allow element "A" here; missing one of "P", "H1", "H2", "H3", "H4", "H5", "H6", "PRE", "DIV", "ADDRESS" start-tag

Making the dtd as loose/transitional you can get rid of the error .. For Strict HTML or Strict XHTML this error is obvious ..

Hope it helped .. :)

regards-Infant Pro

Sesquioxide answered 28/12, 2009 at 7:31 Comment(3)
XHTML 1.1 doesn't have Strict or transtional it's only xhtml 1.1 . and it's giving error in validatorAristocratic
oh!!!! sorry for the inconvenient answer .. I had come to conclusion without proper testing .. Here is the answer .. "For Strict HTML or Strict XHTML" this error is obvious .. If you don't include proper DTD (I have tried it before) the default assumed is strict (I guess and I am sure too) .. That's the reason, it is giving error ..Sesquioxide
(Only) if you don't know .. w3schools.com/Xhtml/xhtml_dtd.asp for XHTML .. and more information on DocType and Tags support : w3schools.com/tags/ref_html_dtd.aspSesquioxide
A
3

Both are right, both will validate

Ankledeep answered 28/12, 2009 at 5:41 Comment(2)
but <a> is inline element and inline element must be in a block elementAristocratic
@Jitendra: Where is that specified? Specifically, HTML4 spec section 7.5.3 states: Generally, block-level elements may contain inline elements and other block-level elements. Generally, inline elements may contain only data and other inline elements. Inherent in this structural distinction is the idea that block elements create "larger" structures than inline elements.Outvote
W
1

Both are fine but having floating inline elements bothers me for some reason

Waldron answered 28/12, 2009 at 5:45 Comment(0)
S
0

@Jitendra, I agree with your point .. but only if you are using strict.dtd (HTML/XHTML)
According to W3C recommendation(for strict dtd) .. the tag <a> must be used in a block element.

Look at the following example:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
 <head>
   <meta content="text/html; charset=utf-8" http-equiv="content-type"/>
   <title>example</title>
 </head>
 <body>You don't have sufficient privileges to access the page.<a href="www.google.co.in">Click here</a> to go back.
 </body>
</html>

The above code produces error while doing HTML validation .. saying

document type does not allow element "A" here; missing one of "P", "H1", "H2", "H3", "H4", "H5", "H6", "PRE", "DIV", "ADDRESS" start-tag

Making the dtd as loose/transitional you can get rid of the error .. For Strict HTML or Strict XHTML this error is obvious ..

Hope it helped .. :)

regards-Infant Pro

Sesquioxide answered 28/12, 2009 at 7:31 Comment(3)
XHTML 1.1 doesn't have Strict or transtional it's only xhtml 1.1 . and it's giving error in validatorAristocratic
oh!!!! sorry for the inconvenient answer .. I had come to conclusion without proper testing .. Here is the answer .. "For Strict HTML or Strict XHTML" this error is obvious .. If you don't include proper DTD (I have tried it before) the default assumed is strict (I guess and I am sure too) .. That's the reason, it is giving error ..Sesquioxide
(Only) if you don't know .. w3schools.com/Xhtml/xhtml_dtd.asp for XHTML .. and more information on DocType and Tags support : w3schools.com/tags/ref_html_dtd.aspSesquioxide
P
0

While I have never cared about the "non-block in block element" rule when writing HTML codes, the body element is supposed to be a block element. (I've forgotten where I read it tho') i.e. your both HTML code fragments are right.

Painting answered 28/12, 2009 at 8:49 Comment(0)
R
0

Neither of them are truly 'right' semantically :-)

Put them as a list and try to avoid the target="_blank". There's probably no reason to put a <p> around each <a> (just ensure that the whole lot is in a block level element if you want to validate with strict - good point infant programmer).

If you want the <a> to act as block level element and thus remove the <p> just use

#somewrapper a
{
 display: block;
}
Ranita answered 28/12, 2009 at 10:25 Comment(2)
why we need to use css, my question is related to HTML and web standards.Aristocratic
Exactly. Target="_blank" will not validate with strict, and semantically (i.e. connected with the HTML structure) this is a list and so would be better using UL LI etc. We could say that perhaps the P + A is not a paragraph and thus the P is unnecessary, but we can make the A a block element like a P anyway if we need.Ranita

© 2022 - 2024 — McMap. All rights reserved.