CSS doesn't support arithmetic such as margin-left: -60px + 50% of the width of the parent div
. But my desire to do so is overwhelming. Does anyone know a solution using JavaScript or otherwise?
Thanks
Mike
CSS doesn't support arithmetic such as margin-left: -60px + 50% of the width of the parent div
. But my desire to do so is overwhelming. Does anyone know a solution using JavaScript or otherwise?
Thanks
Mike
Try this:
var elem = document.getElementById("foobar"),
parent = elem.parentNode;
while (parent && parent.nodeName != "DIV") {
parent = parent.parentNode;
}
if (parent.nodeName == "DIV") {
elem.style.marginLeft = - 60 + (parent.style.width * 0.5) + "px";
} else {
elem.style.marginLeft = "-60px";
}
It's certainly possible with JS, but the specific code will differ depending on what you use (vanilla, frameworks, etc.). Here's a jQuery example:
$('#div').css('width', (-60+($('#div').parent().width()*.5))+'px');