Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position: relative vs Transform: translate [duplicate]

Tags:

css

For example if we would like to move element up on 50px both examples above kind of work

Example 1

position: relative;
top: -50px;

Example 2

transform: translateY(-50px);

Can you please explain possible difference in future behavior or maybe some nuances that exist?

like image 490
volna Avatar asked Dec 26 '16 19:12

volna


People also ask

Why is it better to use translate rather than position?

Using translate() will always “nudge” the element from it's current position which is perfect for an effect like this, or really any design-specific motion.

What is the difference between translate and position?

Translate will do a local translation while setting position doesn't account for rotation. Rotate your object and try both methods and you'll see the difference.

What is the difference between position and relative?

position: relative places an element relative to its current position without changing the layout around it, whereas position: absolute places an element relative to its parent's position and changing the layout around it.

What does transform translate () do?

The translate() CSS function repositions an element in the horizontal and/or vertical directions. Its result is a <transform-function> data type.


1 Answers

Example 1

  • can be used in normal case

  • supported by almost every browser - don't need to rewrite for other browsers

Example 2

  • can be used for animations - using transform you can rotate object in three dimensions
  • limited browser support (requires CSS3 so not early versions of IE nor Opera Mini)
  • combined with using delay, you can animate rotation
like image 136
Ankit vadariya Avatar answered Oct 02 '22 14:10

Ankit vadariya