views:

132

answers:

5

Which is right?

<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>

and 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>
+3  A: 

Both are right, both will validate

Andrew G. Johnson
but <a> is inline element and inline element must be in a block element
metal-gear-solid
@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.
slebetman
@slebetman - see "infant programmer" answers
metal-gear-solid
+1  A: 

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

Galen
+1  A: 
infant programmer
and what about xhtml 1.1
metal-gear-solid
No problem with XHTML .. It accepts <a> element outside the block elements ..
infant programmer
XHTML 1.1 doesn't have Strict or transtional it's only xhtml 1.1 . and it's giving error in validator
metal-gear-solid
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 ..
infant programmer
(Only) if you don't know .. http://www.w3schools.com/Xhtml/xhtml_dtd.asp for XHTML .. and more information on DocType and Tags support : http://www.w3schools.com/tags/ref_html_dtd.asp
infant programmer
A: 

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.

shinkou
A: 

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;
}
Gazzer
why we need to use css, my question is related to HTML and web standards.
metal-gear-solid
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.
Gazzer