0

I need to know if there is a method or something, that will give me the value of the CSS tag float.

I am working on a system that requires Arabic language meaning it needs to be RTL ( Right-To-Left ) but the English version still needs to work.

I am gonna add a language class to the HTML tag which will change depending on the user's language. So I wold like to add a float right on the tags that need to be RTL.

sorry about the grammar English is not my first language.

4
  • Can you share your code? Of your trials or any bit of html and ur css to which u would like to get float value!
    – Smit
    Commented Aug 19, 2016 at 14:46
  • 1
    If you're using zepto (as tagged on the question) you can do var float = $('.element-class').css('float'); to check the current float CSS property. To change it, the code is almost the same: $('.element-class').css('float', 'right'); for example. Commented Aug 19, 2016 at 14:48
  • Please see How to Ask and The perfect question. Commented Aug 19, 2016 at 14:48
  • Thank you Smit that is exaktly what I needed
    – Thedtxy
    Commented Aug 19, 2016 at 14:52

3 Answers 3

4

You can't rely on el.style.float because that will only return if float was set inline on the element itself: <div style="float:right"></div>

Instead, use getComputedStyle. This will take in account a float applied by a stylesheet

var el = $0; // change to get your element

var floatVal = window.getComputedStyle(el).float

See the example below why getComputedStyle should be used

var result = document.getElementById('result')

var val1 = document.getElementById('d1').style.float
var val2 = document.getElementById('d2').style.float

var val3 = window.getComputedStyle(document.getElementById('d2')).float

result.innerHTML += "div 1: "+val1+"<br>";
result.innerHTML += "div 2: "+val2+"<br>";
result.innerHTML += "div 2: "+val3+"<br>";
#d1, #d2 {
  height: 10px;
  width: 100px;
  background: #ccc;
  margin: 1em;
}

#d2 {
  float: left;
}

#result {
  clear: both;
}
<div id="d1" style="float:left"></div>
<div id="d2"></div>

<div id="result"></div>

3

You can get any property from an element with vanilla JavaScript by using document.querySelector('.your-class').style.cssFloat

You may also do $('.your-class').css("float") if you're using jQuery.

0
    function floatRight() {
        document.getElementById("myTextId").style.cssFloat = "right";
    }

    function floatLeft() {
        document.getElementById("myTextId").style.cssFloat = "left";
    }

if(text == "Arabic"){ floatRight(); }
else if(text == "English"){ floatLeft(); }

you can do this!

Not the answer you're looking for? Browse other questions tagged or ask your own question.