Should I make HTML Anchors with 'name' or 'id'?

前端 未结 14 1225
旧巷少年郎
旧巷少年郎 2020-11-22 00:49

When one wants to refer to some part of a webpage with the \"http://example.com/#foo\" method, should one use

F

14条回答
  •  礼貌的吻别
    2020-11-22 01:25

    Wikipedia makes heavy use of this feature like this:

    [...]
    History
    

    And Wikipedia is working for everybody, so I would feel safe sticking with this form.

    Also don't forget, you can use this not only with spans but with divs or even table cells, and then you have access to the :target pseudo-class on the element. Just watch out not to change the width, like with bold text, cause that moves content around, which is disturbing.

    Named anchors - my vote is to avoid:

    • "Names and ids are in the same namespace..." - Two attributes with the same namespace is just crazy. Let's just say deprecated already.
    • "Anchors elements without href atribute" - Yet again, the nature of an element (hyperlink or not) is defined by having an atribute?! Double crazy. Common sense says to avoid it altogether.
    • If you ever style an anchor without a pseudo-class, the styling applies to each. In CSS3 you can get around this with attribute selectors (or same styling for each pseudoclass), but still it's a workaround. This usually doesn't come up because you choose colors per pseudo-class, and the underline being present by default it only makes sense to remove, which makes it the same as other text. But you ever decide to make your links bold, it'll cause trouble.
    • Netscape 4 might not support the id feature, but still an unknown attribute won't cause any trouble. That's what called compatibility for me.

提交回复
热议问题