Originally I set up a small test to find out how jQuery's .width()
treats the different box models. Interestingly it shows the width
defined in CSS minus the padding-left
and padding-right
for border-box
. Now when I wrote this small test I zoomed the page so my colleague who was standing behind my desk could read the code from afar.
To see what I'm asking please proceed as follows:
- Open the codepen example in Chrome: http://codepen.io/anon/pen/WoYeVg (it has to be on codepen since I cannot reproduce the same problem here in the snippet)
- Click the first container. => 160
- Now wait 5 seconds until the text reverts to "click me"
- Set page zoom to 175% by holding CTRL (Windows) or cmd (Mac) and pressing + 4 times.
- Now click the first container again. => 160.00001525878906
Main Question: Why is it not exactly 160 any more at 175%?
Side Question: Why does jQuery treat both box models different in the way it does?
In Firefox I was unable to reproduce what seems to me like a decimal rounding problem/error. Anyone who knows exactly what is going on here?
Please check the following snippet:
$(document).ready(function() {
var elem;
$("div").on("click", function() {
elem = $(this);
elem.text(elem.width());
setTimeout(function() {
elem.text("click me");
}, 5000);
});
})
.a,
.b {
background-color: #a00;
color: white;
margin: 50px;
padding: 20px;
width: 160px;
}
.a {
box-sizing: content-box;
}
.b {
box-sizing: border-box;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- http://codepen.io/anon/pen/WoYeVg -->
<div class="a">click me</div>
<div class="b">click me</div>