Skip to content

Instantly share code, notes, and snippets.

@MohammadYounes
Last active January 9, 2023 08:16
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MohammadYounes/3d8af806cd80ad6bbe94 to your computer and use it in GitHub Desktop.
Save MohammadYounes/3d8af806cd80ad6bbe94 to your computer and use it in GitHub Desktop.
Adding RTL support to font-awesome

You can control icons direction using the following CSS:

[dir=ltr].fa-dir-flip .fa.fa-flip-horizontal,
[dir=rtl].fa-dir-flip .fa,
[dir=rtl] .fa.fa-dir-flip {
  filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=0, mirror=1);
  -webkit-transform: scale(-1, 1);
      -ms-transform: scale(-1, 1);
          transform: scale(-1, 1);
}
[dir=rtl].fa-dir-flip .fa-flip-horizontal,
[dir=ltr].fa-dir-flip .fa,
[dir=ltr] .fa.fa-dir-flip {
  filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=0, mirror=0);
  -webkit-transform: scale(1, 1);
      -ms-transform: scale(1, 1);
          transform: scale(1, 1);
}
:not([dir=rtl]) [dir=ltr] .fa.fa-dir-flip {
  filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=0, mirror=0)!important;
  -webkit-transform: scale(1, 1)!important;
      -ms-transform: scale(1, 1)!important;
          transform: scale(1, 1)!important;
}

:not([dir=ltr]) [dir=rtl] .fa.fa-dir-flip {
  filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=0, mirror=1)!important;
  -webkit-transform: scale(-1, 1)!important;
      -ms-transform: scale(-1, 1)!important;
          transform: scale(-1, 1)!important;
}

It gives you the ability to do global and local flip for .fa icons (live demo)

  1. Globally, add direction attribute dir="rtl" and fa-dir-flip class:
```html
<html dir="rtl" class="fa-dir-flip">
 .
 .
    <!-- this will be RTL -->
    <span class="fa fa-arrow-left"></span>
 .
 .   
<html>
```
  1. Locally, add direction attribute dir="rtl" to the container and fa-dir-flip class to the icon you want to flip.
```html
<html dir="rtl">  
 .
 .
     <div dir="ltr">
         <!-- this will be LTR, following the closest ancestor direction -->
         <span class="fa fa-arrow-left fa-dir-flip"></span>
         <!-- this will be RTL, falling back to the farthest ancestor direction -->
         <span class="fa fa-arrow-left"></span>
     </div>       
 .
 .
</html>
```
@evilaliv3
Copy link

@MohammadYounes do you have any recipe for the current font awesome? (5) ?

@MohammadYounes
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment