PHPackages                             irmmr/rtlcss - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. irmmr/rtlcss

ActiveLibrary

irmmr/rtlcss
============

PHP Module for Converting LTR CSS to RTL

v1.5.1(3mo ago)01.6k2MITPHPPHP ^8.2CI passing

Since Nov 22Pushed 3mo ago2 watchersCompare

[ Source](https://github.com/irmmr/rtl-css)[ Packagist](https://packagist.org/packages/irmmr/rtlcss)[ RSS](/packages/irmmr-rtlcss/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (8)Used By (2)

RTL-CSS
=======

[](#rtl-css)

[![PHP Version](https://camo.githubusercontent.com/0be43eddd7df02c94a0fbc04aaa13d5250351c44ab63a3782498d66280b1767a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d253345253344382e322d626c75652e737667)](https://camo.githubusercontent.com/0be43eddd7df02c94a0fbc04aaa13d5250351c44ab63a3782498d66280b1767a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d253345253344382e322d626c75652e737667) [![License](https://camo.githubusercontent.com/8bb50fd2278f18fc326bf71f6e88ca8f884f72f179d3e555e20ed30157190d0d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e2e737667)](https://camo.githubusercontent.com/8bb50fd2278f18fc326bf71f6e88ca8f884f72f179d3e555e20ed30157190d0d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e2e737667) [![GitHub issues](https://camo.githubusercontent.com/88f2ea8b1ae5b17b4f456e86c212c5a5d8b09a0cbfe3774542204850f65d36f8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f69726d6d722f72746c2d6373732e737667)](https://camo.githubusercontent.com/88f2ea8b1ae5b17b4f456e86c212c5a5d8b09a0cbfe3774542204850f65d36f8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f69726d6d722f72746c2d6373732e737667) [![GitHub stars](https://camo.githubusercontent.com/8b39a10ed391ec88fb71d7dc4db82c01fe2bfefcd4751c24a8037739a28bdce3/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f69726d6d722f72746c2d6373732e737667)](https://camo.githubusercontent.com/8b39a10ed391ec88fb71d7dc4db82c01fe2bfefcd4751c24a8037739a28bdce3/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f69726d6d722f72746c2d6373732e737667)

RTL-CSS is a PHP module designed to automatically convert CSS styles from left-to-right (LTR) to right-to-left (RTL) direction. This is particularly useful for applications that require localization support for languages that read from right to left, such as Persian, Arabic or Hebrew.

RTL-CSS utilizes the [MyIntervals/PHP-CSS-Parser](https://github.com/MyIntervals/PHP-CSS-Parser) library for parsing CSS. This powerful library provides a robust and flexible way to work with CSS stylesheets, allowing us to efficiently analyze and manipulate CSS rules.

Table of Contents
-----------------

[](#table-of-contents)

- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
- [Examples](#examples)
- [Directives](#directives)
- [Encoder](#encoder)

Features
--------

[](#features)

- Automatically converts directional CSS rules to their RTL equivalents.
- Intelligently flips layout-sensitive properties without breaking syntax.
- Normalizes legacy vendor-prefixed functions for modern CSS compatibility.
- Easy integration with existing PHP projects.

The flipper mirrors and transforms directional CSS properties, including:

- Directional flow (`direction`, `float`, `clear`)
- Text and layout alignment (`text-align`, `justify-*`)
- Spacing properties (`margin`, `padding`, `border-width`, `border-style`, `border-color`)
- Border radius adjustments (`border-radius`)
- Box and text shadows (`box-shadow`, `text-shadow`)
- Transforms (`transform`, `transform-origin`)
- Background (`background`, `background-position`, `background-image`)
- `object-position` and `perspective-origin`
- Cursor direction values (`cursor`)
- Transition properties

The engine also understands and correctly flips directional CSS functions:

- `linear-gradient()`
- `repeating-linear-gradient()`
- `calc()`

Gradient directions (angles and keyword-based directions such as to left / to right) are mirrored automatically for RTL layouts.

All legacy vendor-prefixed calc variants are normalized before parsing:

- -moz-calc() → calc()
- -webkit-calc() → calc()
- -ms-calc() → calc()

This ensures modern, standards-compliant output while maintaining compatibility with legacy CSS sources.

Installation
------------

[](#installation)

To install RTL-CSS, you can clone the repository or include it in your project using Composer. Here are the steps for both methods:

### Via composer

[](#via-composer)

```
composer require irmmr/rtlcss
```

If you're using Composer, you can add RTL-CSS as a dependency in your composer.json file:

```
{
    "require": {
        "irmmr/rtl-css": "*"
    }
}
```

Usage
-----

[](#usage)

To use RTL-CSS in your project, include the main PHP file and call the conversion function. Here’s a simple example:

```
use Irmmr\RTLCss\Parser as RTLParser;
use Sabberworm\CSS\Parser;
use Sabberworm\CSS\Parsing\SourceException;

// it's very simple and I know this
$css_content = "div { float: left; }";

// parse css code
$css_parser = new Parser($css_content);

try {
    $css_tree = $css_parser->parse();
} catch (SourceException $e) {
    // Error occ
    return;
}

// create rtlcss parser
$rtlcss = new RTLParser($css_tree);

// parsing css rules and properties
$rtlcss->flip();

// get parsed css code ==> div {float: right;}
echo $css_tree->render();
```

Directives
----------

[](#directives)

Directives are a set of instructions that you can use to create certain commands for the generated RTL code. For example, you can change or remove some selectors, or make it so that rtlcss does not modify them.

- `ignore`
- `remove`
- `raw`
- `rename`
- `discard`

#### Ignore

[](#ignore)

```
/* rtl:ignore */
div {
  float: right;
  text-align: right;
  font-size: 15px;
}

/* rtl:ignore:margin-left */
a {
   margin-left: 10px;
   background-position: 10px;
}
```

```
div {
  float: right;
  text-align: right;
  font-size: 15px;
}

a {
   margin-left: 10px;
   background-position: right 10px top 50%;
}
```

Encoder
-------

[](#encoder)

The Encoder is an additional safety layer designed to protect your CSS before it is processed by the underlying parser.

While **MyIntervals/PHP-CSS-Parser** handles most modern CSS correctly (including newer `rgb()` syntax), certain edge cases and vendor-specific functions may still be altered, normalized, or even removed during parsing.

Examples include:

- Legacy vendor-prefixed functions such as -moz-calc(), -webkit-calc(), -ms-calc()
- Complex or uncommon function patterns
- Syntax structures unrelated to RTL transformation

The purpose of the Encoder is simple:

> Preserve CSS integrity.

It temporarily encodes sensitive segments of CSS before parsing, ensuring that:

- Unsupported or non-standard syntax is not removed
- Vendor-specific constructs are not lost
- Unrelated rules remain untouched
- The flipper only modifies what is actually relevant to RTL logic

After processing, all encoded segments are safely restored.

```
use Irmmr\RTLCss\Parser as RTLParser;
use Irmmr\RTLCss\Encode;
use Sabberworm\CSS\Parser;
use Sabberworm\CSS\Parsing\SourceException;

$css_content = "
#element {
    float: left;
    transform: translate3d(-webkit-calc((25%/2) * 10px), 50%, calc(((25%/2) * 10px)));
    box-shadow: 2px 2px 2px 1px rgb(0 0 0 / 20%);
}
";

// encode css code
$encoder     = new Encode($css_content);
$css_content = $encoder->encode();

// parse css code
$css_parser = new Parser($css_content);

try {
    $css_tree = $css_parser->parse();
} catch (SourceException $e) {
    // Error occ
    return;
}

// create rtlcss parser
$rtlcss = new RTLParser($css_tree);

// parsing css rules and properties
$rtlcss->flip();

// get parsed css code
$encoder->setEncoded( $css_tree->render() );

// get final css code
echo $encoder->decode();
```

Output:

```
#element {
    float: right;
    transform: translate3d(calc(-1*(( 25% / 2 ) * 10px)),50%,calc(( ( 25% / 2 ) * 10px )));
    box-shadow: -2px 2px 2px 1px rgb(0 0 0 / 20%);
}
```

Examples
--------

[](#examples)

```
.example {
  display: inline-block;
  padding: 5px 10px 15px 20px;
  margin: 5px 10px 15px 20px;
  border-style: dotted dashed double solid;
  border-width: 1px 2px 3px 4px;
  border-color: red green blue black;
  box-shadow: -1em 0 0.4em gray, 3px 3px 30px black;
}
```

Output:

```
.example {
  display: inline-block;
  padding: 5px 20px 15px 10px;
  margin: 5px 20px 15px 10px;
  border-style: dotted solid double dashed;
  border-width: 1px 4px 3px 2px;
  border-color: red black blue green;
  box-shadow: 1em 0 0.4em gray, -3px 3px 30px black;
}
```

Note
----

[](#note)

I created this project by observing . One of the reasons was that this project was no longer active and needed some improvements. I tried to add new sections to it and at the same time, I aimed to make it more similar to the original project . I don't need to mention that this entire project is modeled and copied from the projects I referred to.

The transformation logic of this library is based on the behavior and principles demonstrated by [RTLCSS](https://rtlcss.com/). During development, the generated RTL output was continuously compared against the output produced by RTLCSS to ensure consistency, correctness, and predictable mirroring behavior.

###  Health Score

46

—

FairBetter than 93% of packages

Maintenance82

Actively maintained with recent releases

Popularity21

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 79.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~74 days

Recently: every ~112 days

Total

7

Last Release

95d ago

PHP version history (2 changes)v1.0PHP &gt;=7.4

v1.5.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/523cf016e8f0fdcee80e35fdca4467d6257e309db7f3eb5bc7b5eb9fba029244?d=identicon)[irmmr](/maintainers/irmmr)

---

Top Contributors

[![marmar8x](https://avatars.githubusercontent.com/u/177212675?v=4)](https://github.com/marmar8x "marmar8x (23 commits)")[![irmmr](https://avatars.githubusercontent.com/u/52796853?v=4)](https://github.com/irmmr "irmmr (6 commits)")

---

Tags

ltr-to-rtlphpphp-rtlcssrtlrtl-cssrtlcssRTLCssphp-rtlcss

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/irmmr-rtlcss/health.svg)

```
[![Health](https://phpackages.com/badges/irmmr-rtlcss/health.svg)](https://phpackages.com/packages/irmmr-rtlcss)
```

###  Alternatives

[phenx/php-svg-lib

A library to read, parse and export to PDF SVG files.

1.4k141.6M36](/packages/phenx-php-svg-lib)[dompdf/php-svg-lib

A library to read, parse and export to PDF SVG files.

1.4k28.4M9](/packages/dompdf-php-svg-lib)[pelago/emogrifier

Converts CSS styles into inline style attributes in your HTML code

94944.1M110](/packages/pelago-emogrifier)[lullabot/amp

A set of useful classes and utilities to convert html to AMP html (See https://www.ampproject.org/)

3802.9M10](/packages/lullabot-amp)[padaliyajay/php-autoprefixer

CSS Autoprefixer written in pure PHP.

468.3M8](/packages/padaliyajay-php-autoprefixer)[northys/css-inliner

PHP Library that converts css file into html inline styles.

4516.2k](/packages/northys-css-inliner)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
