简体   繁体   English

用文本包装锚元素

[英]Wrapping anchor elements with text

If an anchor element has text (and only text), but is not part of a larger block of text, does it matter (eg semantics, SEO, maintenance) whether or not that anchor tag is wrapped by a paragraph element? 如果锚元素具有文本(并且只有文本),但不是较大文本块的一部分,那么该锚标签是否由段落元素包装是否重要(例如语义,SEO,维护)?

<!-- Wrapped -->
<p><a href=''>Link Description</a></p>

<!-- Standard -->
<a href=''>Link Description</a>

Looking at large websites, both ways appear to be popular. 在大型网站上,两种方式似乎都很流行。 But are there any notable differences? 但是有什么明显的区别吗?

(Note that p should only be used for paragraphs . If it's not a paragraph, and you want to use such a wrapper, use div instead; if it is a paragraph, p should be used anyway.) (请注意, p仅应用于段落 。如果它不是段落,并且您要使用这样的包装器,请改用div ;如果是段落,则无论如何都应使用p 。)

I can think of two cases where such a wrapper semantically matters (see below). 我可以想到两种情况,这种包装在语义上很重要(请参阅下文)。 Another good reason for using a wrapper is, of course, if the existing CSS/JS suggests/requires it (eg, if the CSS author decided to style div elements, and an a without a div wrapper would break the layout). 另一个很好的理由使用的包装,当然,如果现有的CSS / JS表明/需要时(例如,如果CSS笔者决定风格div元素,和a没有div包装将打破布局)。

Line breaks 换行

User agents that don't support author-provided CSS (like text browsers, feed readers, etc.) typically start a new line for elements like div , p , etc. (ie, "block-level elements" in HTML 4), but not for elements like span , a , etc. ("inline elements"). 不支持作者提供的CSS的用户代理(例如文本浏览器,提要阅读器等)通常会为divp等元素(例如HTML 4中的“块级元素”)换行,但不适用于spana等元素(“内联元素”)。

So if you have something like this: 因此,如果您有这样的事情:

<p>Teaser with some text.</p>
<a href="link">Read more</a>
<cite>John Doe</cite>

such a user agent might display the last two elements on the same line: 这样的用户代理可能会在同一行上显示最后两个元素:

Teaser with some text.

Read more John Doe

while it might be preferable to have each on its own line: 虽然最好每个都放在自己的行上:

Teaser with some text.

Read more

John Doe

So for this and similar cases a wrapping element (that typically creates a new line by default) should be used. 因此,对于这种情况和类似情况,应使用包装元素(通常默认情况下会创建新行)。

If the a is the only phrasing content element (eg, between two p elements), then this is not an issue, of course. 如果a是唯一的短语内容元素(例如,在两个p元素之间),那么这当然不是问题。

Unclosed p 未关闭的p

If a p element has no closing tag (which is valid), a following a would become part of the p : 如果p元素没有结束标记(有效),则后面的a将成为p一部分:

<p>some text

<a href="link">Read more</a>

If closing the p is not an option, one would have to use a wrapper for the a (one that can't be a child of p , eg, a div ). 如果关闭p是不是一种选择,一个人必须要使用的包装a (一个不能为的子p ,例如,一个div )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM