[英]P wrap A or A wrap P
what is the best way to make a link in html i mostly use html4 but im trying to implement html5 什么是在html中建立链接的最佳方法,我主要使用html4,但是我试图实现html5
so my question is basically what should be the wrapping tag, the p or a? 所以我的问题基本上是包装标签是p还是a?
this: 这个:
<p class="center">
<a href="#">
<img src="addToCart.png">
</a>
</p>
or this: 或这个:
<a href="#">
<p class="center">
<img src="addToCart.png">
</p>
</a>
thanks. 谢谢。
Under HTML 4.1 an a
element, display: inline
by default, cannot contain a block-level element, such as a p
; 在HTML 4.1中,
a
元素display: inline
默认情况下, display: inline
不能包含块级元素,例如p
; therefore p
should wrap a
. 因此
p
应该包装a
。
If, though, you're using HTML 5, and have <!doctype html>
, then either way is acceptable (and valid), so use whichever you find best meets your requirements. 但是,如果您使用的是HTML 5,并且具有
<!doctype html>
,那么任何一种方法都是可以接受的(并且是有效的),因此请使用最能满足您要求的方法。
Anchors can only contain inline elements (span, strong, em etc). 锚只能包含内联元素(跨度,强度,em等)。 Paragraph tags are block - and therefor not valid.
段落标签是阻止的,因此无效。 Set the display:inline and it will certainly work, but its no longer valid.
设置display:inline,它当然可以工作,但是不再有效。
Your first example is correct. 您的第一个例子是正确的。
As noted = this is true in html4 only. 如前所述=仅在html4中为true。
In summary, both of the above can be used and are valid. 综上所述,以上两种方法都可以使用并且有效。
<a>
has a transparent content model (with the exception of excluding other interactive content) so it can wrap practically anything. <a>
具有透明的内容模型(除了排除其他交互式内容之外),因此它几乎可以包装任何东西。 <p>
has "phrasing content" as its model, and <a>
is in the phrasing content category. <p>
具有“短语内容”作为其模型,并且<a>
在短语内容类别中。 <img>
is also in "phrasing content." <img>
也在“措辞内容”中。
Thus, both of your examples are totally valid. 因此,您的两个示例都是完全有效的。 It entirely depends on what you want to do specifically.
这完全取决于您要具体做什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.