HTML title attribute precedence

Viewed 69

Can i assume that a title attribute in <img> always takes precedence over a title attribute in <a>? (html5 spec/standard)

EX:

<a title="foo">
  <img title="bar">
</a>

tooltip: bar

EX2:

<a title="foo">
  <img title="bar" id="1">
  <img id="2">
</a>

tooltip#1: bar
tooltip#2: foo
1 Answers

According to the WHATWG (HTML standards working group):

If this attribute is omitted from an element, then it implies that the title attribute of the nearest ancestor HTML element with a title attribute set is also relevant to this element. Setting the attribute overrides this, explicitly stating that the advisory information of any ancestors is not relevant to this element. Setting the attribute to the empty string indicates that the element has no advisory information.

So this tells us that it's not about a or img, it's about where it's at in the child/ancestor relationship. The child will always take precedence.

However, Title attribute is also discouraged. (See the note on the page I linked).

Related