Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

translateY(): vertical movement without moving the next block

Read percentage movement against the element’s reference height and keep layout coordinates separate.

Version: CSS Modules

Syntax

transform: translateY(<length-percentage>);

The translateY() function moves along the local Y axis and leaves X unchanged. In a page without other rotations, a positive value moves downward and a negative value upward. A percentage uses the reference box height selected by transform-box; it is not a percentage of the parent height. Lengths and compatible calc() expressions work as well, including unitless zero.

Translation changes painting and hit geometry while the original layout position stays reserved. Moving a box down can overlap the next block instead of pushing it down. Choosing border-box or content-box changes percentage distances when padding or borders are present. Pure translation does not depend on transform-origin, but a combined rotation or scale can make both origin and order relevant.

The example uses 40px of content height, 5px padding and a 2px border on each side: 54px overall. With border-box, 50% moves the box down 27px; with content-box it moves 20px. The content-box values -25% and calc(25% + 5px) produce -10px and 15px. Throughout, the layout top is 0, the layout height is 54 and the following block starts at 54. Its position does not follow the translated box.

Examples

document.body.style.margin='0';
const parent=document.createElement('div');parent.style.cssText='height:300px';
const box=document.createElement('div');box.style.cssText='width:80px;height:40px;padding:5px;border:2px solid;transform-box:border-box';
const next=document.createElement('div');next.textContent='Next';
parent.append(box,next);document.body.append(parent);
const measure=value=>{box.style.transform=value;const r=box.getBoundingClientRect();return [r.left,r.top,r.height].join(',')};
const border=measure('translateY(50%)');box.style.transformBox='content-box';
const content=measure('translateY(50%)');const negative=measure('translateY(-25%)');
const calc=measure('translateY(calc(25% + 5px))');
console.log('border='+border+'; content='+content+'; negative='+negative+'; calc='+calc+'; layout='+box.offsetTop+','+box.offsetHeight+'; next='+next.offsetTop+'; zero='+CSS.supports('transform','translateY(0)'));

Result

border=0,27,54; content=0,20,54; negative=0,-10,54; calc=0,15,54; layout=0,54; next=54; zero=true

Each geometry triple contains left, top and visible height. The 300px parent height is deliberately different from both reference heights.

Sources