Skip to main content

Right-to-Left (RTL) Styling Best Practices

RTL (Right-to-Left) is a text direction that is used in languages such as Arabic, Hebrew, and Persian. In RTL languages, the text is written from right to left. When designing an app for RTL languages, it is important to consider the following best practices:

Styling Components for RTL and LTR Layouts

Certain UI elements need to be mirrored when switching between RTL and LTR. To handle this:

Direction-Aware Flexbox Layouts

Flexbox automatically handles direction-based layouts, but you should be mindful of flexDirection, alignItems, and justifyContent properties.

For example:

const styles = StyleSheet.create({
container: {
flexDirection: I18nManager.isRTL ? "row-reverse" : "row",
},
});

Padding and Margin Adjustments

While setting padding and margin, use directional properties such as paddingStart, paddingEnd, marginStart, and marginEnd instead of absolute values like paddingLeft and paddingRight.

const styles = StyleSheet.create({
container: {
paddingStart: 10, // Automatically adjusts for RTL
paddingEnd: 10,
},
});

Text Alignment

For text alignment, use textAlign: 'auto' which automatically handles alignment based on the language direction. Alternatively:

textAlign: I18nManager.isRTL ? "right" : "left";

TextInput Component

For TextInput, RTL text should automatically be supported. However, you may need to handle placeholders and input text alignment:

<TextInput
style={{ textAlign: I18nManager.isRTL ? "right" : "left" }}
placeholder="Enter text"
/>

Also, consider setting textAlignVertical if the input spans multiple lines.

Horizontal Scrolling in RTL Mode

  • By default, when FlatList is used with horizontal scrolling (horizontal={true}) and the app is in RTL mode, the list automatically starts from the right.

  • The content will align accordingly, and the list scrolls from right to left, which is the expected behavior for RTL layouts.

  • Inverting the Scroll Behavior:

    <FlatList
    horizontal={true}
    inverted={I18nManager.isRTL} // Inverts the scroll behavior
    data={data}
    renderItem={renderItem}
    />

Below is a detailed table of RTL-supported style properties in React Native. These properties automatically adjust their behavior based on the layout direction (LTR or RTL).

Icons and Images

When using icons and images, ensure that they are mirrored correctly for RTL layouts. Some icons and images might need to be mirrored in RTL mode. React Native supports this with the transform property:

<Image
source={yourImageSource}
style={{
transform: [{ scaleX: I18nManager.isRTL ? -1 : 1 }],
}}
/>

Table of RTL-Supported Styles

Style PropertyDescriptionEquivalent LTR PropertyEquivalent RTL Property
paddingStartSets padding at the leading edge (start) of the container. Adjusts based on layout direction (LTR/RTL).paddingLeftpaddingRight
paddingEndSets padding at the trailing edge (end) of the container. Adjusts based on layout direction (LTR/RTL).paddingRightpaddingLeft
marginStartSets margin at the leading edge (start) of the container. Adjusts based on layout direction (LTR/RTL).marginLeftmarginRight
marginEndSets margin at the trailing edge (end) of the container. Adjusts based on layout direction (LTR/RTL).marginRightmarginLeft
startSets position from the leading edge (start) for absolute/relative positioning. Adjusts based on layout direction (LTR/RTL).leftright
endSets position from the trailing edge (end) for absolute/relative positioning. Adjusts based on layout direction (LTR/RTL).rightleft
textAlignAdjusts text alignment based on the layout direction. Use 'auto' for automatic alignment based on LTR/RTL.left, center, rightSame as LTR
flexDirectionDefines the primary axis of layout. Use 'row-reverse' for RTL-specific adjustments.row, columnrow-reverse, column
borderStartWidthSets border width at the leading edge (start) of the container. Adjusts based on layout direction (LTR/RTL).borderLeftWidthborderRightWidth
borderEndWidthSets border width at the trailing edge (end) of the container. Adjusts based on layout direction (LTR/RTL).borderRightWidthborderLeftWidth
borderStartColorSets border color at the leading edge (start) of the container. Adjusts based on layout direction (LTR/RTL).borderLeftColorborderRightColor
borderEndColorSets border color at the trailing edge (end) of the container. Adjusts based on layout direction (LTR/RTL).borderRightColorborderLeftColor

Key Notes:

  1. Padding and Margin: paddingStart, paddingEnd, marginStart, and marginEnd adjust padding and margin relative to the layout direction (LTR or RTL).
  2. Text Alignment: Use textAlign: 'auto' to automatically align text based on the current language direction.
  3. Flexbox Layout: The flexDirection property handles row direction changes. Using row-reverse is particularly useful in RTL layouts.
  4. Border and Positioning: Border and positional properties (like start, end, borderStartWidth, etc.) ensure correct positioning and borders in both LTR and RTL.

By leveraging these RTL-supported style properties, your React Native app can automatically handle both left-to-right and right-to-left languages without the need for hardcoding directional adjustments.