The Best Way to RTL Websites with Sass!
There are lots of ways to RTL your website on the internet, but none of them are easy to use.
I developed an easy and convenient method to support multi-language website, which RTL languages are also supported.
In English, the aligning of the text start at left and ends at right, but in Semitic languages it is opposite, the aligning of the text starts at right and ends at left.
Let’s say we need padding at start of <p> tag. In English it needs to be padding-left:10px and in Semitic languages it needs to be padding-right:10px, in both of these cases we need padding in the start of the line of 10px.
And from here we start the method of creating a universal style for start or end.
In a similar way we will use it for position, floats, padding, margin, border and transforms CSS.
How to Start?
First: Create two config files _direction-ltr.scss and _direction-rtl.scss .
In the LTR file, enter these variables:
/*** LTR websites ***/
In the RTL file, enter these variables:
/*** RTL websites ***/
Second: Create/Update your main SASS file as an inner file, e.g _main.scss.
Notice: The main file doesn’t @import the directions config files!
How to import the directions files?
Add two new main files in the root directory, main-ltr.scss & main-rtl.scss.
In every main file @import the correct config direction file + the inner _main.scss file.
From this file you will compile the two main files, main-ltr.css & main-rtl.css
How to use the variables?
CSS Directions & text-align:
Just put the variables in the value. $start-direction for LTR languages is left & for Semitic(RTL) languages is right. $direction for English is ltr & for Semitic languages is rtl.
In Positioning, put the right and left in variables according to your needs.
CSS Transform: translateX:
When working with transform you need to change number from positive to negative or vice versa.
Let’s say we have box transform:translateX(200px) in English. We multiply it by $transform-direction (=1), in English the result would be 200px (1*200px), but in rtl it would be -200px (-1*200px).
transform: translateX(200px * $transform-direction);
That’s it! We made an easy and convenient method to RTL websites.
An example of the code in _main.scss:
will be compiled to in main-ltr.css:
will be compiled to in main-rtl.scss
That’s all, You now have an easy way to RTL websites.
I hope you’ve enjoyed this article and learned from my experience.
If you liked this article you may also like :
CSS Architecture for Multiple Websites With SASS
Elad Shechter, Web Developer specializing in CSS & HTML design and architecture, working at Investing.com.