I have a function written in jQuery that automates the margin-bottom
or padding-bottom
(conditionally) of a table. Basically it negates the effect of the table cells' borders pushing down the page, keeping the vertical rhythm intact. Instead of using JS to do this, I believe I should be able to with Less and CSS instead.
The condition (if-statement) is based on the number of rows (and thus borders, plus one more) in the table. Since my "magic number" is 24 (24px is the vertical rhythm unit), if the table has (0,11)∪(24,35)∪… borders, the function will make the margin-bottom
negative and pull the rest of the page up. However if the table has (12,23)∪(36,47)∪… borders, it will add to the padding-bottom
, pushing the rest of the page down to the next vertical rhythm unit. This is all operating under the assumption that each horizontal border in the table is 1px. Extra credit if you can generalize the pattern to work for thicker borders.
Here's my function in jQuery:
/*
Algorithm:
for each table:
take the number of rows (x)
add 12
mod 24
subtract 12
negate.
function notation: g(x) = -(f(x+12)-12) where f(x) = MOD(x,24)
function transformation: MOD(x,24) translated left 12 and down 12, then flipped over the x-axis.
if g(x) <= 0, then margin-bottom that number
else, padding-bottom that number.
*/
$('table').each(function () {
var n_rows = 0;
$(this).find('tr').each(function () {
n_rows++;
});
n_rows++; // once more for the last border
var btm = -(((n_rows + 12) % 24) - 12);
if (btm <= 0) {$(this).css('margin-bottom',btm);}
else {$(this).css('padding-bottom',btm);}
});
Here's what I have so far in my Less:
table {
counter-reset: nrows;
tr {
counter-increment: nrows;
}
counter-increment: nrows; // once more for the last border
// @btm: -(((nrows + 12) % 24) - 12);
// if (@btm <= 0) {
// margin-bottom: @btm;
// } else {
// padding-bottom: @btm;
// }
}
Help me out here?