Styling Right-To-Left Text with CSS on a Multi-Lingual Site
We recently had the pleasure of working with CD Baby creator Derek Sivers on a new project called MusicThoughts. It’s a great collection of quotes from musicians on everything from the music industry to life in general. In short, pretty kick-awesome! Go check it out already!
This was a challenging project from start to finish. First of all, we couldn’t modify the HTML at all, only the CSS. And second was the support for multiple languages. 10 to be exact! This had several ramifications:
- Every piece of text had to be editable. For a design that was going to be largely photographic in nature, this was a huge challenge.
- Different languages take up different amounts of space, so the room we alloted for every bit of text, even the name of the site and navigation, had to shrink and grow appropriately.
- Some languages, like Arabic read from right to left. Holy crap! That changes everything about your design!
And it’s the last point I wanted to explore a bit more. You see, for languages that read from right to left there’s a dir="rtl"
attribute that we can tie into with CSS so we can target it and style appropriately. Mr. Sivers put his on the html element:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ar" dir="rtl">
So for instance, the navigation got all messed up when it was text-aligned right. All we have to do is target it with CSS and make minor modifications:
html[dir="rtl"] #navigation { float: right; display: inline; margin-left: 0px; margin-right: 360px; }
And that’s it! Rinse and repeat for the affected areas. Believe it or not, this targeting seems to be supported by all the major browsers (unheard of, I know!) so try it out on your next multi-lingual site and see how awesome it is.
Comments
1
David Cameron Walker - Mar 17, 2009
Jonathan Longnecker - Mar 17, 2009
Jonathan Longnecker - Mar 17, 2009
4
William Lovaton - Oct 12, 2009
5
hanz - Oct 05, 2010
Jonathan Longnecker - Oct 05, 2010
7
Roberto Maurizzi - Dec 07, 2010
Jonathan Longnecker - Dec 07, 2010
9
ahmed - Nov 14, 2011
10
Miguel Perez - Jul 05, 2012
11
Mohammad - Oct 01, 2012
Jonathan Longnecker - Oct 01, 2012
13
Mohammad - Nov 26, 2012
14
Mauricio - Jun 25, 2013
15
Yasen Vasilev - Nov 07, 2013
16
Samer - Dec 09, 2014
17
Amr - Mar 29, 2015
18
Leena - Sep 06, 2015
Jonathan Longnecker - Sep 07, 2015