CKTIS / CSS
log() — logarithms and their domain
Calculate a natural logarithm or choose a base, then turn the numeric result into a useful CSS value.
Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntax
log(A)
log(A, B)
transform: translateX(calc(log(8, 2) * 1px));log() answers which exponent produces a number for a chosen base. In log(A, B), both arguments must resolve to <number>; omitting the base uses e. Thus log(8, 2) is 3, log(e) is 1, and log(8, 0.5) is −3. To produce a length, multiply the result by a unit, as in calc(log(8, 2) * 1px).
For ordinary finite logarithms, use a positive input and a positive base other than 1. A base between 0 and 1 is valid. Negative inputs or a base of 1 produce a nonnumeric result; zero and infinities require the special rules in the specification. A length cannot be passed directly as the input, and a fractional result must not be mistaken for an integer count.
CSS.supports() checks whether a declaration is supported, not whether the selected inputs have a useful mathematical result. The negative-input declaration in the example therefore parses even though its calculation produces NaN. Validate dynamic inputs, retain a fallback for older browsers, and do not infer support on every device from one browser execution.
Examples
const box = document.createElement('div');
document.body.append(box);
function offset(expression) {
box.style.transform = 'translateX(calc(' + expression + ' * 1px))';
return Number(new DOMMatrix(getComputedStyle(box).transform).m41.toFixed(6));
}
console.log('base2:', offset('log(8, 2)'));
console.log('natural:', offset('log(e)'));
console.log('fractional base:', offset('log(8, 0.5)'));
console.log('one:', offset('log(1)'));
console.log('length argument:', CSS.supports('width', 'calc(log(8px) * 1px)'));
console.log('negative argument parses:', CSS.supports('width', 'calc(log(-1) * 1px)'));
box.remove();Result
base2: 3 natural: 1 fractional base: -3 one: 0 length argument: false negative argument parses: true
The displayed JavaScript reads actual CSS translation offsets, rounded to six decimal places, for four finite cases. It separately checks syntax and type acceptance; it does not render or measure a NaN result.