How to use RTL (right-to-left) in Ionic 5 apps and PWA
By: Vaibhav Gehani
Tags: Ionic, Ionic Framework, Rtl, Arabic, Right To Left

RTL is a very specific feature desired by people whose language is written from right to left. In this post you’ll learn how to implement RTL in Ionic 5.

I will also show how various Ionic components look in an RTL layout.

All the layouts used in this blogpost can be found in Ionic 5 Full app starter

What is RTL (Right to Left)

LTR (left-to-right) and RTL (right-to-left) are the two major layout patterns based on various languages in the world. The major difference between LTR and RTL language scripts is the direction in which content is displayed:

LTR languages display content from left to right

RTL languages display content from right to left

RTL content also affects the direction in which some icons and images are displayed, particularly those depicting a sequence of events. For example, ion-item component has a left-to-right flow by default, whatever element is added inside it. But making the content RTL makes the whole layout mirrored.

In general, the passage of time is depicted as left to right for LTR languages, and right to left for RTL languages.

LTR and RTL layout comparison for a simple form

In the above image, notice the

Change in text alignment

Change in position of * on the words

Position of down arrow for the dropdown

Change in position of Hamburger menu and filter icon in header

This will give you a basic idea of RTL layouts.

Notice that since both the images above have English as the language, you still see the actual sentence going from left-to-right. But when you are writing in an RTL language, the text will start from right to left.

What is Ionic 5

I know most of the readers reading this blog will know what is Ionic 5, but just for the sake of beginners, I explain this in my every blog.

You probably already know about Ionic, but put this section in every blog just for the sake of beginners. Ionic is a complete open-source SDK for hybrid mobile app development. Ionic provides tools and services for developing hybrid mobile apps using Web technologies like CSS, HTML5, and Sass. Apps can be built with these Web technologies and then distributed through native app stores to be installed on devices.

In other words — If you create native apps in Android, you code in Java. If you create native apps in iOS, you code in Obj-C or Swift. Both of these are powerful, but complex languages. With Cordova (and Ionic) you can write a single piece of code for your app that can run on both iOS and Android (and windows!), that too with the simplicity of HTML, CSS, and JS. I’m a huge fan of Ionic and been developing Ionic apps for last 4 years.

How to implement RTL in Ionic 5 apps

Implementing RTL in Ionic 5 apps is very easy. It can be done in two ways

Using dir="rtl" attribute in HTML tags

attribute in HTML tags Using direction= rtl in css

All implementations revolve around these two methods. Following are the ways you can implement RTL in your Ionic 5 app.

1. Using HTML tag

If you want to convert the complete app into RTL layout, then use dir="rtl" on <html> tag itself. For Ionic 5 apps, the parent HTML is index.html in src folder

Global RTL setting on <html> tag

2. Using router-outlet or ion-app

Editing HTML tag with dir="rtl" is not a good solution if you want to change the layout dynamically from a setting inside the app.

In such case, you can make use of ion-router-outlet or <ion-app> tag which is the parent of all the pages in Ionic 5 apps. Use dir="rtl" on <ion-router-outlet> or <ion-app> tag, and all the pages under that will get RTL layout

RTL implementation on ion-router-outlet

One thing to notice here - when you apply RTL on ion-router-outlet , the sidemenu behaves opposite to how it behaves with dir="rtl" on <html tag

Notice in both cases, the Hamburger menu icon in header is on right, but the menu opens in different way for both implementations.

3. Using CSS at global level

You can also convert the complete app to RTL using CSS in global.scss

:root {

direction: rtl;

}

This will apply RTL to all pages and components. Again, sidemenu in this implementation behaves a little different from Method 1

So RTL at root css level and dir="rtl" on <ion-app> tag have same effect.

4. Using CSS at Page level