I have div
with margin:auto;
and I need get only margin-left
size value using javascript :)
//css
.test{
margin: auto;
width: 100px;
height: 100px;
outline: 1px solid red;
}
// html
<div class="test">Test</div>
Live example
You can use window.getComputedStyle() method if you don't need IE8 support.
var test = document.querySelector('.test');
var left_margin = window.getComputedStyle(test).getPropertyValue("margin-left"); // returns margin e.g. '655px'
left_margin = left_margin.match(/\d+/); //returns bare number e.g. '655'
Use this:
1) With jQuery
var left = $(".test").offset().left;
2) Or, second version is that:
Replace your div to <div class="test" id="test"></div>
, and use this js.
var left = document.getElementById("test").offsetLeft;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With