This article is a part of our AtoZ CSS Series. You can find other entries to the series here.
You can view the full transcript and screencast for the corresponding video about translateX
here.
Welcome to our AtoZ CSS series! In this series, I’ll be exploring different CSS values (and properties) each beginning with a different letter of the alphabet. We know that sometimes screencasts are just not enough, so in this article, we’ve added quick tips on the differences between translate
and position
.
X is for translate
and position
There are a number of CSS properties for placing elements on the page. These include big-picture layout properties like float
, margin
and padding
and more fine-grained tools like position
and translate()
.
On the surface, position:relative
and transform:translate()
seem to work in quite a similar way but there are some subtle differences which are important to grasp so we choose the right tool for the job.
What’s the difference between translate
and position:relative
?
In this post we’ll look at a number of differences between these methods for element placement but first, let me clarify what these various properties do.
If we set position:relative
on an element we can use it to create a boundary for absolutely positioning elements within. This is probably the more common use of relative positioning but it’s not the use that we’re discussing here.
If we combine position:relative
with one of the offset properties top
, bottom
, left
or right
the element will be moved from its original place in the layout whilst preserving the space in the document it once occupied. The element will be moved on to a new layer and its “layer order” or its stacking order can then be controlled with the z-index
property.
.thing {
position: relative;
top: 100px;
left: 50px;
}
In the above example the element will be moved 100px
away from the top and 50px
away from the left of its original position.
When using transform:translate(x,y)
we get a very similar visual result to using relative position. The same result as above could be achieved with the following snippet:
.thing {
transform: translate(50px, 100px);
}
In this case, we’re translating the coordinates of the element by 50px
along the x-axis and 100px
along the y-axis. The end result is visually the same as the previous position
example.
So, why do we have two ways of doing the same thing? Well, there are some differences between these approaches…
Browser support
position
is a CSS2 property whereas transform
is a CSS3 property. There are differences in browser support as a result although really the only browsers that don’t support 2D transforms are IE8 and below.
If you need to support old versions of IE, transform
won’t be an option for you.
GPU Acceleration
The transform
property will use hardware acceleration where possible so using translate()
over position
will see performance benefits if any animations or transitions are also being used on the element.
If you want to move an element as part of a transition or keyframe animation, favor using translate
rather than position
(this goes for both absolute and relative positioning). For more depth on this, including an explanation and performance profiling, check out this video from Paul Irish.
Percentage based values behave differently
One major difference between these two methods of placing elements is how they respond to percentage based values.
Take the following markup and styles:
<div class="box position"></div>
<div class="box transform"></div>
.box {
width: 200px;
height: 200px;
}
.position {
position: relative;
left: 50%;
background: red;
}
.transform {
transform: translateX(50%);
background: blue;
}
Both elements have been given an offset from their left edge of 50%
.
The left edge of the red box will be 50% away from the edge of its parent container.
The left edge of the blue box will be 100px
away from the left edge of its parent container. This distance is because 50% of 200px
is 100px
.
When setting percentage values with translate
, the percentage is measured as a percentage of the elements computed width or height.
See the Pen vyYxgJ by SitePoint (@SitePoint) on CodePen.
Combine position
and translate
together
One final point to make is that because position
and transform
are two different properties, we can combine them together. This allows us to combine absolute positioning to place an element in a very specific place on the page and then modify that position
with transform
.
An example of this could be to have a positioned element animate up and down or left and right. Or we can combine positioning with translate
to achieve flexible vertical centering.
So while these two methods of placing elements can be used to achieve similar results, there are some significant differences and combining the strengths of each approach makes them a really powerful set of tools.
Frequently Asked Questions (FAQs) about CSS Translate vs Position
What is the main difference between CSS translate and position?
The main difference between CSS translate and position lies in how they manipulate the position of an element. CSS position changes the element’s position in the document flow, meaning it can affect the layout of other elements. On the other hand, CSS translate is a transform function that moves an element without affecting the layout of other elements. It changes the visual rendering position, not the actual position in the document flow.
When should I use CSS translate instead of position?
CSS translate is ideal when you want to move an element without affecting the layout of other elements. It’s also beneficial for animations because it promotes better performance and smoother animations. This is because translate uses the GPU (Graphics Processing Unit), which is more efficient at rendering graphics and animations.
Can I use both CSS translate and position on the same element?
Yes, you can use both CSS translate and position on the same element. However, it’s important to understand how they work together. The position property will affect the element’s position in the document flow first, and then the translate function will move the element from that position.
Why is CSS translate often recommended for animations over position?
CSS translate is often recommended for animations because it uses the GPU, which is more efficient at rendering graphics and animations. This results in smoother animations and less CPU (Central Processing Unit) usage, which can improve the overall performance of a webpage.
Does CSS translate work with all types of position values?
Yes, CSS translate works with all types of position values – static, relative, absolute, fixed, and sticky. The translate function will move the element from its position, regardless of the position value.
What units can I use with CSS translate?
With CSS translate, you can use pixel values, percentages, and viewport units. Percentages are relative to the size of the element itself, not its parent. This gives you more flexibility and control over the movement of the element.
Can I animate the movement of an element with CSS position?
While it’s technically possible to animate the movement of an element with CSS position, it’s not recommended. This is because changing the position property can trigger layout shifts, which can negatively impact performance. For animations, CSS translate is the better option.
How does CSS translate affect the z-index of an element?
CSS translate doesn’t directly affect the z-index of an element. However, applying a translate function to an element creates a new stacking context, which can influence how the z-index is applied.
Can I use CSS translate to move an element diagonally?
Yes, you can use CSS translate to move an element diagonally. You can do this by specifying both the X and Y values in the translate function. For example, translate(50px, 50px) will move the element 50 pixels to the right and 50 pixels down, effectively creating a diagonal movement.
Does CSS translate affect the clickable area of an element?
No, CSS translate does not affect the clickable area of an element. The clickable area remains at the original position in the document flow, even if the visual rendering of the element has been moved with translate.
Front-end dev and teacher at The General Assembly London. A to Z CSS Screencaster, Founder of sapling.digital and Co-founder of The Food Rush.