Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to switch the left and right css values when in a right-to-left language?

I'm working on a site that supports many different locales. Our next locale is Hebrew, which is a right-to-left language. I'm setting the dir=rtl and lang=he on the HTML element, and certain things shift around (as they should).

A lot of the website uses elements with absolute positioning setting the left value. Is there a way to make it so when you are in rtl mode that it would switch it to be the right value?

I know that I could have a rtl class on my html element and do css overrides when that class is present, but the project is pretty large and it would not be fun to hunt down all of these occurrences manually.

like image 506
Jason Addleman Avatar asked Jul 17 '15 18:07

Jason Addleman


People also ask

How do you change the text direction in CSS?

The text-orientation CSS property sets the orientation of the text characters in a line. It only affects text in vertical mode (when writing-mode is not horizontal-tb ). It is useful for controlling the display of languages that use vertical script, and also for making vertical table headers.

How do you shift items to the right in CSS?

If position: absolute; or position: fixed; - the right property sets the right edge of an element to a unit to the right of the right edge of its nearest positioned ancestor. If position: relative; - the right property sets the right edge of an element to a unit to the left/right of its normal position.

How do you convert rtl to LTR?

Shortcuts: Ctrl+Shift+J -- switch to RTL Ctrl+Shift+E -- switch to LTR Note 1: If one or both of the shortcuts don't work, it means that something else is mapped to them.


1 Answers

I think adding classes would be more robust, but considering how much no-fun that would be, hunt them down with JavaScript!

Codepen

$(document).ready(function () {
  var elsToFlip = $('*')
  elsToFlip.each(function () {
    var pos = $(this).css('position')
      , left = $(this).css('left')

    if (pos === 'absolute' && left !== 'auto') {
      $(this).css({
        left: 'initial'
      , right: left
      })
    }
  })
})
like image 122
azium Avatar answered Oct 12 '22 12:10

azium