Linking to HTML Elements via id Attribute

My coworker Brian Hall and I came across a posting saying “everyone knows this”, and the this was that you can use a hash to link to an id. ┬áHere’s a snippet to demonstrate:

My Page With Internal Links

Navigation

A

Some content here that if relevant to 'A'

B

Some content here that is relevant to 'B'

C

Some content here that is relevant to 'C'

Previously, the way you would do this is to use an a tag with a name value around the h2 elements:

My Page With Internal Links Using Name Attribute

Navigation

A

Some content here that if relevant to 'A'

B

Some content here that is relevant to 'B'

C

Some content here that is relevant to 'C'

The “everyone knows this” technique has many advantages:

  • No extraneous a tags that have to be unstyled, because they’re not really links
  • Better cooperation with jQuery code because you are now using id attributes
  • You can do some nifty styling using the target pseudo class selector