Skip to main content

Day 45 of 100DaysOfSpec, The dir attribute

I am reading and taking notes on the HTML specifications for 100 days as part of #The100DayProject. Read the initial intent/backstory. I am a Microsoft employee but all opinions, comments, etc on this site are my own. I do not speak on behalf of my employer, and thus no comments should be taken as representative of Microsoft's official opinion of the spec. Subsections not listed below were read without comment.

Currently reading in 3.2.5.6 The dir attribute

3.2.5.6 The dir attribute

This is a global attribute describing the direction of text. The value can be ltr (left-to-right), rtl, or auto.

In the case of auto, the direction will be determined automagically using the contents of the element. The spec says, “the heuristic used by this state is very crude (it just looks at the first character with a strong directionality...)”, so “auto” is basically a hail-Mary pass for when you really don’t know the direction of the language.

It also says “For textarea and pre elements, the heuristic is applied on a per-paragraph level.” I suppose this is because the spec’s authors thought there was a fairer chance of these longer-form elements having mixed language contents.

No missing/invalid value defaults on the dir attribute. But…the algorithm requires a particular value when certain conditions are met. I suppose that’s a little different: setting a default value (probably easier) vs. actually following the spec and implementing the full algorithm as directed.

“...elements from other namespaces always just inherit their directionality from their parent element, or, if they don't have one, default to ‘ltr’.”

The HTML element’s other attributes can have directionality. Certain attributes can have a different direction than the element itself; this direction is measured using the same “crude” look-at-the-first-character algorithm. Otherwise, attributes get the same direction as the element's dir attribute.

And finally, the spec urges people to denote the text direction in HTML rather than CSS, so that the designation is always tied to the content.

Responses

My blog uses Webmentions. Responses from Mastodon and other sites that send Webmentions will show up here.

No Webmentions have been sent to this post yet.