If your problem is a one-pixel rounding error related visual discrepancy, in most cases, you don't actually need to round. A relatively simple solution is to make one of your items take up all of the remaining space.
In a flex scenario, if you don't need your layout to wrap, you can even pick oen of your center items for the least noticeable effect possible.
.some-block {
width: calc( ( 100% / 11 ) - 9.09px );
flex-shrink: 0;
}
.some-block:nth-of-type(5) {
// Will attempt to take the whole width, but its siblings refuse to shrink, so it'll just take every bit of remaining space.
width: 100%;
}
Depending on your exact CSS case, the pixel substraction could potentially be avoided altogether by using paddings or margins and maybe an additional nested element. That way, the CSS calc()
becomes irrelevant and it all could be done in Sass, which has much more powerful math tools and doesn't make the client work for calculations.
Also in a flex context, if your goal is just to make every element equal, you may not need calculations at all. See this question for details.
In a table, since cell widths are calculated left to right, a similar trick can be employed, but you'll have to give width: 100%
to the last cell, since there's no concept of flex-shrink
and column widths are decided left to right.
.my-table {
table-layout: fixed;
}
.table-cell {
width: calc( ( 100% / 11 ) - 9.09px );
}
.table-cell:last-of-type {
// Will attempt to take the whole width, but since previous column widths have already been determined, it cannot grow any further than its allocated width plus any stray pixels.
width: 100%;
}
calc()
rounds up. So if you want to round down you'll need to use JavaScript. – Credendum