Course contentsShow
← Web Development
Lesson 2524 of 2,98245. InternationalizationFree lesson

CSS Transforms and RTL

Apply transforms like translateX correctly in RTL contexts using logical equivalents or calculations.

CSS Transforms and RTL

You'll learn how to apply CSS transforms like translateX correctly in right-to-left layouts without visual breakage.

The Problem with Transform Directions

When you use transforms like translateX(100px) in a left-to-right (LTR) layout, everything works as expected—elements move to the right. But in RTL contexts, your mental model breaks: moving "right" on screen is actually moving toward the start in RTL languages. Unlike layout properties that automatically flip with dir="rtl", transforms are mathematically absolute—they don't respect document direction.

Why This Matters

Imagine an animated slide-in menu that enters from the right side in LTR. In RTL, you'd want it to enter from the left side (the new "start"). But translateX(300px) will still push it rightward regardless of dir, creating awkward UX.

The Solution: Logical Calculations

You have two main approaches:

  1. Manually flip values in RTL: Multiply your X-transform by -1 when RTL is detected:

    /* LTR */
    .slide { transform: translateX(300px); }
    
    /* RTL override */
    [dir="rtl"] .slide { transform: translateX(-300px); }
    
  2. Use CSS custom properties with direction logic:

    :root { --direction: 1; }
    [dir="rtl"] { --direction: -1; }
    
    .slide { transform: translateX(calc(300px * var(--direction))); }
    

Note: There are no native "logical transform" properties yet (like there are for margins/padding), so manual calculations remain necessary.

Key Takeaway:
CSS transforms don't automatically flip for RTL—always multiply translateX values by -1 in RTL contexts or use CSS variables with calc() to create direction-aware movement.