JSFiddle - React, Tailwind, and code Playground

by Timmy Willison

HTML

<div id="container">
    <div id="element"></div>
</div>

CSS

#container {
    width: 400px;
    border: 1px solid black; padding: 2px;
}
#element {
    width: 50%;
    margin-right: 50%;
    height: 100px; background: green;
}

JavaScript

var el = jQuery('#element');

console.log('el.width() = %o', el.width());
console.log('expected: 200, iOS5: 200 (correct)');

console.log('el.outerWidth(true) = %o', el.outerWidth(true));
console.log('expected: 400, iOS5: 250 (px and % added)');


console.log('el.css(width) = %o', el.css('width'));
console.log('expected: 200px, iOS5: 200px (correct)');


console.log('el.css(margin-right) = %o', el.css('margin-right'));
console.log('expected: 200px, iOS5: 50% (not incorrect, but not as expected)');

var value = el.css('margin-right');
if (value.substr(value.length - 1, 1) === '%') {
    var valPercentage = value.substr(0, value.length - 1);
    var valWidth = jQuery('#element').parent().width();
    value = (valWidth * valPercentage) / 100 + 'px';
}

// expected: 200px, iOS5: 200px
console.log('corrected margin value = ' + value);