Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

cos() — cosine in CSS calculations

`cos()` computes the cosine of one angle or of a number interpreted as radians and returns a dimensionless number.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

cos( <calc-sum> )

cos() computes the cosine of one angle or of a number interpreted as radians and returns a dimensionless number.

The example gives an element width calc(100px * cos(60deg)), whose used layout width is 50px; a second marker uses cos(pi) and moves according to result -1. cos(1s) is rejected because time is not an allowed argument type.

Grammar: cos( <calc-sum> ).

The argument must resolve to a <number> or <angle>; a number denotes radians, while an angle retains its unit semantics. The result is always between -1 and 1. This is useful for circular placement and angle-dependent sizing, but an incompatible dimension invalidates the entire property value.

Examples

const stage = document.createElement('div');
stage.style.cssText = 'position:relative;width:200px;height:20px';
const sized = document.createElement('div');
sized.style.cssText = 'width:calc(100px * cos(60deg));height:10px';
const positioned = document.createElement('div');
positioned.style.cssText = 'position:absolute;left:calc(50px + 20px * cos(pi));width:1px;height:1px';
stage.append(sized, positioned);
document.body.append(stage);
const invalidTime = CSS.supports('width', 'calc(100px * cos(1s))');
console.log('width=' + Math.round(sized.getBoundingClientRect().width) + '; pi-left=' + Math.round(positioned.offsetLeft) + '; invalid-time=' + invalidTime);

Result

width=50; pi-left=30; invalid-time=false

`cos()` computes the cosine of one angle or of a number interpreted as radians and returns a dimensionless number.

Sources