Bhavan Kumar Natarajan Bhavan Kumar Natarajan - 5 months ago 18
jQuery Question

How calculate centered div's left side space

I have centered div for my side. Now I want to calculate left side blank space. How to do it by jquery.

My Markup



<main id="page_main" role="main">
<div class="content">
</div>
</main>


My Css



#page_main
{
width:100%;
}
.content
{
width:960px;
margin:0px auto;
}


Now I want to calculate the screen's left side blank space. Can you help me how to do it?

Answer

You can use offset() which returns coordinates of element relative to document. It return Object with top and left properties. DEMO

$(window).on("resize", function () {
 var c = $(".content").offset().left;
  $('.content').text(c)
}).resize();