Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the auto computed margin in firefox with jQuery

With the markup and css below, I tried to get the computed left-margin of shell.

 <section class="page-title">
      <div class="shell">
        <h5 class="title">Welcome!</h5>
      </div>
    </section>

.shell {
  zoom: 1;
  max-width: 1000px;
  margin-left: auto;
  margin-right: auto;
  padding-left: 16px;
  padding-right: 16px;
}

Using

parseInt($('.shell').css('marginLeft'))

it works in Chrome, Safari, IE9 but surprisingly doesn't work in Firefox. Tried the other approach:

($('.shell').outerWidth(true) - $('.shell').outerWidth()) / 2

Also works well but Firefox. So I guess firefox doesn't support to obtain the undefined css with jQuery? A straight forward way to work around with it is:

($('.page-title').width() - $('.shell').outerWidth()) / 2 

But I wonder if there is a better way.

like image 812
Yujun Wu Avatar asked Nov 03 '22 20:11

Yujun Wu


1 Answers

When you say it doesn't work, do you receive errors (in the console) or does it return 0? Returning 0 for the auto setting is, apparently, an issue with different browsers.

You could use the lightweight jSizes library which returns all metrics in pixels. This will save you having to faff around with outerWidth, etc..

like image 186
Andy G Avatar answered Dec 12 '22 10:12

Andy G