I have a big <div> with three little <div>'s inside of it, inline. They each have 33% width, but that causes some alignment problems because 3 * 33% != 100%. What is the best way to represent a perfect third in CSS like this? Maybe just 33.33%?
- 4Just throw enough decimal places at it until it's unlikely someone has a desktop large enough to make sig.figs relevant to the calculation.Marc B– Marc B2011-03-01 18:31:59 +00:00Commented Mar 1, 2011 at 18:31
- 3You can't specify fractions, so 33.33% is pretty much your only option.user229044– user229044 ♦2011-03-01 18:32:21 +00:00Commented Mar 1, 2011 at 18:32
- 4Useful info: Sub-Pixel Rendering Problems in CSSjrn.ak– jrn.ak2011-03-01 18:34:40 +00:00Commented Mar 1, 2011 at 18:34
- 1This is why it would be awesome to count in base 12 rather than in base 10. Half=6, third=4, quarter=3, sixth=2. Two thirds=8, three quarters=9. Base 10 is a painJeremy Thille– Jeremy Thille2021-02-22 11:07:30 +00:00Commented Feb 22, 2021 at 11:07
4 Answers
Now that calc is widely supported among modern browsers, you can use:
#myDiv { width: calc(100% / 3); } Or you can use this as a fallback if your browser wouldn't support it:
#myDivWithFallback { width: 33.33%; width: calc(100% / 3); } 3 Comments
.column-one-third { width: 33.33%; width: calc(100%/3); }. That way you can support non calc browsers.width: 33.333333%;calc(200% / 3) and calc(100% / 1.5) work nicely as 2/3rdsAre you making any allowance for margins? You could go 30% per column with 5% margin either side of the center column.
Comments
.col_1_3 { width: 33%; float: left; } .col_1_3:nth-of-type(even) { width: 34%; } 1 Comment
The highest resolution screen is a non-production NEC LCD screen with a resolution of 2800x2100. Even at that size, one pixel is 0.0357% of the width of the screen. So 33.33% should be close enough until 5,000-pixel-wide screens become the norm.
John Resig did some research into sub-pixel rounding in different browsers, though, so depending on your three columns to equal exactly the width of the screen may never have a perfect solution.