javascript - Browsers truncate border values to integers -


whenever non-integer pixel value used border of element, browser truncates value become integer. why case?

i'm aware border not actually take part of pixel, these types of values used in combination others form full pixels. example, left , right border having widths of 1.6px should cause total width of element increase 3px. works because the full value stored in memory , used calculations.

however, seems not case when rendering border though width, padding, , margin behave correctly.

var div = document.getelementsbytagname('div'),      len = div.length,      style;  (var = 0; < len; i++) {      style = getcomputedstyle(div[i]);      div[i].innerhtml = div[i].classname + ': ' + style.getpropertyvalue(div[i].classname) + '<br>height: ' + style.getpropertyvalue('height');  }
div {      width: 300px;      border: 1px solid black;      padding: 50px 0;      text-align: center;      -webkit-box-sizing: border-box;      -moz-box-sizing: border-box;      box-sizing: border-box;  }  div.width {      width: 300.6px;  }  div.padding-top {      padding-top: 50.6px;  }  div.margin-top {      margin-top: 0.6px;  }  div.border-top-width {      border-top-width: 1.6px;  }
<div class="width"></div>  <div class="padding-top"></div>  <div class="margin-top"></div>  <div class="border-top-width"></div>

when tested, code produced same results (disregarding exact precision) consistently. major browsers (chrome, firefox, opera) behaved same. exceptions safari 5.1 (which rendered padding , margin similar border, due version) , internet explorer (which calculated border-top-width correctly).

width, padding, , margin remembered decimal values , allowed padding affect height accordingly, border not. truncated integer. why case width border? there way make border value remembered in fuller form true height of element retrieved using javascript?

the simple explanation browser uses integers border widths internally (or @ least exposes them publicly such).

an example of source code of chrome (chromium) in file computedstyle.h defines border-widths integers (line 508):

source code snapshot

there little can , why: there little information border widths in w3c specification css backgrounds , borders. states line-width no units, type or definition how treat unit except absolute (non-negative):

value: <line-width>
[...]
computed value: absolute length; ‘0’ if border style ‘none’ or ‘hidden’

and:

the lengths corresponding ‘thin’, ‘medium’ , ‘thick’ not specified, values constant throughout document , thin ≤ medium ≤ thick. ua could, e.g., make thickness depend on ‘medium’ font size: 1 choice might 1, 3 & 5px when ‘medium’ font size 17px or less. negative values not allowed.

the same information found in box model document no new details.

as all values end pixel values (as our screens pixel-devices) number coming through em, vw, % etc. seems end integer when comes border widths without considering sub-pixeling.

not transforms (scale) seem affect in browsers use integers border widths.

in end, seems browser vendor how treat these values (it aesthetic reasons doing so, performance, .. can guess..).


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -