Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

tan(): a ratio from an angle, with unstable asymptotes

Use a dimensionless tangent in CSS calculations and avoid assuming a portable result at a right angle.

Version: CSS Modules

Syntax

transform: translateX(calc(20px * tan(45deg)));

tan() takes one calculation resolving to an angle or a number. An angle can use deg, rad or turn; a unitless number is interpreted in radians. The result is a number, not a length, so multiplying it by 20px produces a displacement. Unlike sine and cosine, tangent is not bounded between -1 and 1. A length such as 1px is not a valid argument.

Near 90deg plus multiples of 180deg, tiny input changes can produce very large differences. At those asymptotes the numeric result is implementation-defined: do not rely on a particular infinity, sign or large finite value. An infinite argument produces NaN in the calculation. Acceptance by CSS.supports() only checks declaration support; it does not turn that exceptional result into useful geometry.

The example reads actual translation matrices. The equivalent inputs 45deg, 0.125turn and pi / 4 each give 20.000px after multiplying by 20px; -45deg gives -20.000px. At 89deg the recorded displacement is 1145.800px, illustrating growth before the asymptote. The example rejects a length argument and checks only parsing for infinity. It deliberately does not calculate geometry at the exact asymptote or present a NaN result as a valid distance.

Examples

const box=document.createElement('div');document.body.append(box);
function distance(argument){box.style.transform='translateX(calc(20px * tan('+argument+')))';return new DOMMatrix(getComputedStyle(box).transform).m41.toFixed(3)}
console.log('degrees='+distance('45deg')+'; turn='+distance('0.125turn')+'; radians='+distance('pi / 4')+'; negative='+distance('-45deg'));
console.log('near-asymptote='+distance('89deg'));
console.log('length-argument='+CSS.supports('transform','translateX(calc(20px * tan(1px)))')+'; infinity-parses='+CSS.supports('transform','translateX(calc(20px * tan(infinity)))'));
box.remove();

Result

degrees=20.000; turn=20.000; radians=20.000; negative=-20.000
near-asymptote=1145.800
length-argument=false; infinity-parses=true

Output is formatted to three decimals from the tested browser’s computed matrix. Near-asymptote rounding is an observation from Chromium 157, not a promise of identical precision on every engine.

Sources