惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
小众软件
小众软件
Engineering at Meta
Engineering at Meta
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
G
Google Developers Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Google DeepMind News
Google DeepMind News
Security Archives - TechRepublic
Security Archives - TechRepublic
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
罗磊的独立博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 热门话题
酷 壳 – CoolShell
酷 壳 – CoolShell
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Help Net Security
Martin Fowler
Martin Fowler
G
GRAHAM CLULEY
Simon Willison's Weblog
Simon Willison's Weblog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - Franky
V
Vulnerabilities – Threatpost
云风的 BLOG
云风的 BLOG
博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
量子位
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
I
Intezer
Scott Helme
Scott Helme
A
About on SuperTechFans
博客园 - 司徒正美
Hacker News: Ask HN
Hacker News: Ask HN
The GitHub Blog
The GitHub Blog
Forbes - Security
Forbes - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Spread Privacy
Spread Privacy
T
Tailwind CSS Blog
S
Security Affairs
宝玉的分享
宝玉的分享

CSS-Tricks

Get Ready For the Powerful CSS border-shape Property! | CSS-Tricks What’s !important #14: Gap Decorations, random(), <select> field sizing, and More | CSS-Tricks The Shifting Line Between CSS States and JavaScript Events | CSS-Tricks translateZ() | CSS-Tricks translateX() | CSS-Tricks translate() | CSS-Tricks Using Scroll-Driven Animations for Opposing Scroll Directions | CSS-Tricks A First Look at Scroll-Triggered Animations | CSS-Tricks The Siren Song of  ariaNotify() | CSS-Tricks Prop For That | CSS-Tricks What’s !important #13: @function, alpha(), CSS Wordle, and More | CSS-Tricks There’s no need to include ‘navigation’ in your navigation labels | CSS-Tricks Why Isn't My 3D View Transition Working? | CSS-Tricks Creating Memorable Web Experiences: A Modern CSS Toolkit | CSS-Tricks
translateY() | CSS-Tricks
Gabriel Shoyombo · 2026-06-25 · via CSS-Tricks

The CSS translateY() function shifts an element vertically by the specified amount. Specifically, it shifts an element either up or down, depending on whether the value is positive or negative.

.parent:hover .box {
  transform: translateY(50%); /* Shift down by half the element's height */
}

Along with other transform functions, it is used inside the transform property.

It is defined in the CSS Transforms Module Level 1 draft.

Syntax

The translateY() function’s syntax looks like this:

<translateY()> = translateY( <length-percentage> )

Just a fancy way of saying: Translate (or move) the element vertically by this much.

Arguments

/* <length> */
translateY(80px) /* Element moves 80px down*/
translateY(-24ch) /* Element moves 24ch up */

/* <percentage> */
translateY(50%) /* Element moves 50% of the element's height downward */
translateY(-100%) /* Element moves 100% of the element's height upward */

The translateX() function takes a single <length-percentage> argument that specifies how far to move the element and in which direction, which can be either up (negative) or down (positive).

It can take either a <length> or a <percentage> argument:

  • <length>: When it’s positive, e.g. 20px, it pushes the element 20 pixels down, while negative values like -20px will push the element 20 pixels up.
  • <percentage>: Percentages are relative to the element’s height, so translateY(40%) on a 100px-tall element pushes the element 40px down, while translateY(-40%) pushes the element 40px up.

Basic usage

The translate functions are ideal for simpler animations since they don’t disturb the document flow. Specifically, the translateY() function is great for “pop-up” or “fade-in” animations where the elements can slide from the bottom. Take for example, a card component (let’s call it .stat-card) that slides up when the user clicks a button or scrolls down.

The components are initially displaced 50 pixels downwards, hidden with an opacity value of 0.

.stat-card {
  /* ... */
  opacity: 0;
  transform: translateY(50px);

  transition:
    opacity 0.8s ease-in,
    transform 0.8s ease-in,
    box-shadow 0.3s ease;
}

Then, when another element (let’s call it .dashboard) becomes .active, the .stat-card components become fully visible and are translated into their original position on the page.

.dashboard.active .stat-card {
  opacity: 1;
  transform: translateY(0);
}

We can even add a “micro-animation” whenever the user hovers over any of the .stat-card by moving it slightly up by 8px, thanks to using a negative value:

.dashboard.active .stat-card:hover {
  transform: translateY(-8px);
}

Focused form field animation

Form fields usually use a blue border to indicate focus, but they can be more interesting. In UI libraries like MUI, the TextField component’s label initially serves as a placeholder, but when a user focuses on the field, it moves to the top and assumes the label’s position.

We can implement a similar animation by applying the translateY() function to the input and label elements.

To start, we initially place the label element within the input element as a placeholder using absolute positioning.

label {
  position: absolute;
  left: 15px;
  top: 15px;

  pointer-events: none;
  transform-origin: left top;
  transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

Then, when the user focuses on the input field, the label is translated 32px up to serve as a label.

input:focus ~ label,
input:not(:placeholder-shown) ~ label {
  transform: translateY(-32px) scale(0.8);
  color: #6200ee;
  font-weight: bold;
}

The label‘s position is restored when the user loses focus from the input element.

It doesn’t affect other elements

The translateY() function, like other transform functions, does not affect the document flow. Instead, it visually displaces the translated element to a new position without pushing the elements in its surroundings or the ones at the new position. Also, the space the element originally occupied remains reserved in the layout as if it hadn’t moved at all.

/* Translated element */
.translated {
  position: absolute;
  top: 0;
  left: 0;

  transform: translateY(40px);
}

Notice how shifting the “Translated” element does not affect the placement of the other elements around it.

Unlike margin which can trigger reflows or shift neighboring elements, translateY() only changes where the element is visually rendered.

Issues with pointer pseudo-classes

Using translateY() directly on a pointer pseudo-classes like :hover can sometimes break interactions. In a situation where the element is translated far and it moves away from the cursor, the :hover state gets lost, causing the element to snap back immediately to its original position. At the initial position, the cursor is there, so it translates again, resulting in a continuous flickering loop.

A simple solution to this is to place the element to be translated in a parent container, and apply the pseudo-class (:hover) to the parent element, while the main element takes the translate function.

/* Problem case */
.bad:hover {
  transform: translateY(160px);
}

/* Solution */
.parent:hover .good {
  transform: translateY(160px);
}

Demo

Specification

The CSS translateY() function is defined in the CSS Transforms Module Level 1, which is currently in Editor’s Drafts.

Browser support

The translateY() function has baseline support on all modern browsers.