Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do the lost pixels go in a percent CSS layout? [duplicate]

Tags:

Possible Duplicate:
Evenly distributed height of child elements with CSS

Lets say i have a design with 6 DIVs that are float left with a width of 16.666%. So the document size is splited in 6 parts.

Now if i have a document size of lets say 620px wide this would make each part 103.333px. Since i don't know screens that can show partial pixels :) I wonder how the browser handles the partial pixels.

Here is my testcase: http://jsfiddle.net/dhQh2/ (Just resize the window to get a result)

When resizing it seams that the 6 DIVs keep the same size. But it some cases it can't be. How does the browser handle those partial PX values?

like image 902
meo Avatar asked Apr 07 '11 13:04

meo


People also ask

How do you measure pixels in CSS?

The CSS Pixel Device-Width can be calculated by dividing the Pixel Width by the CSS Pixel Ratio, and rounding it to the nearest integer.

How big is a CSS pixel?

The term CSS pixel is synonymous with the CSS unit of absolute length px — which is normatively defined as being exactly 1/96th of 1 inch.


2 Answers

If, for example, you're using a % width, and the exact width should be 103.333px, then the browser must make a decision on how display that.

Different browsers make different decisions, read these links for more information:

  • http://ejohn.org/blog/sub-pixel-problems-in-css/
  • http://elasticss.com/determination-of-algorithms-used-for-percentage-based-rounding-divs-on-browsers-and-css-frameworks/

I particularly like this explanation from John Resig/David Baron of why this is even a problem:

I was talking this over with some Mozilla developers and David Baron explained the situation quite well:

We’re trying to meet a bunch of constraints that can’t all be satisfied at the same time (the proof is left as an exercise to the reader, though I may have actually written it out once in a Bugzilla comment):

  1. 4 adjacent objects of width/height 25% (for example) starting at one edge of a container should end exactly at the other edge; there should never be an extra pixel in the container and they should never be wrapped due to being a pixel to wide

  2. objects that are logically adjacent should always touch visually; there should never be a pixel gap or a pixel of overlap due to rounding error

  3. objects given the same width should occupy the same number of pixels

  4. object boundaries should always (visually) be aliased to a specific pixel boundary in the display (they should never be blurred)

The one [Mozilla] sacrifices is typically (3), except for borders where we sacrifice (1) by rounding the widths to pixel boundaries much earlier.

See this question for a JavaScript fix that forces consistency:

Evenly distributed height of child elements with CSS

Another relevant question:

Are the decimal places in a CSS width respected?

like image 63
thirtydot Avatar answered Dec 15 '22 01:12

thirtydot


They go in the heaven of pixels ;)

Just joking. Most likely different browsers deal with it in different ways.

The first approach coming in my mind, is to calculate the width of each area. Then round it to the nearest int. The rest of the pixels gets just left empty.

Another approach, could be filling the last area, whatever it is the width (with a small margin of errors).

Very interesting question: it could be nice to have the control over this behaviour with css indeed.

like image 39
Luca Fagioli Avatar answered Dec 15 '22 01:12

Luca Fagioli