简体   繁体   中英

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?

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM