text-align: start & RTL

By  on  

As you've probably heard me mention a billion times on Twitter, I've been proudly work on the Mozilla Developer Network revamp.  This is my first real experience with working on websites with a fair amount of RTL readers. One thing I quickly recognized when browsing in RTL mode was that text wasn't always lining up on the correct side -- I would see text on the left where the browser should have displayed it on the right.  After digging into the stylesheets, I saw explicit text-align:left directives and started queueing up selectors I'd need to change to text-align: right.  Before doing so, however, I took the time to research a better way...and found it quickly.

text-align: left; /* as backup */
text-align: start;

text-align: start allows the browser to decide how to best align text based on the user's LTR or RTL preference.  There's also an "end" value if you prefer the text to show up on the right in LTR mode, and visa versa.  Browser support is good at this point, with Internet Explorer supporting this property within IE10.

Even if RTL isn't a requirement of your website, keep text-align: start in mind -- it's a very simple change that could be a big boost to your RTL visitors!

Recent Features

  • By
    CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

  • By
    Create a CSS Flipping Animation

    CSS animations are a lot of fun; the beauty of them is that through many simple properties, you can create anything from an elegant fade in to a WTF-Pixar-would-be-proud effect. One CSS effect somewhere in between is the CSS flip effect, whereby there's...

Incredible Demos

  • By
    HTML5’s window.postMessage API

    One of the little known HTML5 APIs is the window.postMessage API.  window.postMessage allows for sending data messages between two windows/frames across domains.  Essentially window.postMessage acts as cross-domain AJAX without the server shims. Let's take a look at how window.postMessage works and how you...

  • By
    MooTools Star Ratings with MooStarRating

    I've said it over and over but I'll say it again:  JavaScript's main role in web applications is to enhance otherwise boring, static functionality provided by the browser.  One perfect example of this is the Javascript/AJAX-powered star rating systems that have become popular over the...

Discussion

  1. Brian Compton

    Great tip. I was looking up RTL when I finally got it from the context: Right to Left languages such as Arabic, Hebrew, et al as opposed to LTR or Left to Right languages such as English.

  2. I developed fair amount of Arabic websites. I always add: body { direction:rtl; }

    And this fixes everything. I don’t need to even addwhatever { text-align:right; }
    I’ve tried to use this new rule but I couldn’t feel any difference. I don’t really know how it helps RTL websites! On the MDN it says:

    The same as left if direction is left-to-right and right if direction is right-to-left.

    • I’ve not had the same experience Alfy, but I’ll look some more. “start” seems more semantically correct as well.

  3. Nice tip David,
    As an Arab developer , I agree with ahmed that using the proper direction and text-align is better, because most of people here uses English browsers/OS so the default text align for most of the visitors will be left. Another approach for multi language websites is to include additional style sheet i.e rtl.css or ltr.css .

  4. It simply make the process of localizing websites from an LTR language to a RTL one less painful, since you won’t need to go down the stylesheet and switch every right with left and vice versa in text-align properties.

    And I agree that it IS semantically more meaningful, for what graphic designer originally meant was that some text should be aligned through the end of the line. Which defers in direction for different languages.

  5. Wow! I am not a big commenter (at least not till I’m done with my site) but I had to say THANK YOU!! This property is not on the CSS3 site, so I figured there was no other way. It pays to be persistent, and your post proves that! Keep up the good work!!

  6. Binyamin

    IE10 actually does not support start and end value.
    Browser compatibility https://developer.mozilla.org/en-US/docs/Web/CSS/text-align#Browser_compatibility

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!