Hi - I’m confused. Some time back sitepoint forums told me to use this to create a target:
<div id=“target”></div>
to replace the old <a name=“target”></a>
But now this page:
says the format should be:
<a id=“tips”>Useful Tips Section</a>
this is the original feedback I received in forum:
-----begin feedback-----
<a name=“recipe”></a> - not good to use, but if you do, it must be in a content-containing element, ie a <td> or <th>. If the anchor isn’t legally placed inside a table cell, it bobs up to sit on top of the table.
It’s much better to put id=“recipe” on the element you want to jump to, rather than messing around with named anchors.
-----end feedback-----
The original problem is that browsers did not to go to the text inside a table cell when I was using <a name=“recipe”>. Instead it went to the top of the table.
Which is better to use to create a bookmark anywhere in doc (both inside and outside table cells):
<div id=“target”></div>
or <a id=“target”></a>
I never enclose text within the bookmark, the way W3schools does. I always place the target immediately before the text that I want to target.
I’m not sure if it’s called bookmark or target. But you reference it like this:
[in this context] The name attribute can only be used on anchors (<a>), whereas the id attribute can be used on any element you like (<div>, <p>, <tr>, <td>, whatever).
Which one you choose is up to you, there are no strict rules for that.
Val, this is called a “fragment identifier”. To identify an element on the page (that is, provide a hook so that you can jump directly to that element), add an id to the element. E.g.
<div [COLOR="#FF0000"]id="target"[/COLOR]>
... content here ...
</div>
So, let’s say that div appears way down one of your pages. The id allows you to do two things.
You can place a link somewhere else on the page, so that people can click the link and jump to the “target” div. E.g. Let’s say you have this at the top of your page:
<a [COLOR="#FF0000"]href="#target"[/COLOR]>Jump to the Target div</a>
Clicking that link will cause the browser to jump to the target div, which will appear at the top of the browser if there’s enough space below it to allow this.
You can link to the target div from outside the page with a URL like this:
No, definitely not. There’s no advantage in that at all (and w3schools has a poor reputation anyway. )
What would be much better would be to do this:
<p>Here we are talking about <a href="#carrot">carrot cake crackers</a> and how delicious they are.</p>
<h2 [COLOR="#FF0000"]id="carrot"[/COLOR]>Carrot Cake Cracker</h2>
That’s the most reasonable way to do it, and certainly best practice. No need for empty, unsemantic elements in your HTML.