CSS Properties
CSS translate Property
Photo Credit to CodeToFun
🙋 Introduction
The translate
property in CSS is used to move an element from its current position, both horizontally and vertically.
It is part of the CSS transform property family, which allows developers to manipulate elements in 2D and 3D space without affecting the layout of the document.
💡 Syntax
The syntax for the translate
property is as follows:
transform: translate(tx, ty);
- tx: Specifies the horizontal translation value.
- ty: Specifies the vertical translation value.
🎛️ Default Value
The default value is translate(0, 0)
, which means no translation (no movement) from the element's original position.
🏠 Property Values
Value | Description |
---|---|
tx | A length value (px, em, rem, %, etc.) specifying the horizontal translation. |
ty | A length value (px, em, rem, %, etc.) specifying the vertical translation. |
📄 Example
In this example, we'll translate
a <div> element horizontally and vertically using the translate
property.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS translate Example</title>
<style>
.box {
width: 100px;
height: 100px;
background-color: #3498db;
transform: translate(50px, 20px);
}
</style>
</head>
<body>
<h1>Box with Translate Transformation</h1>
<div class="box"></div>
</body>
</html>
🖥️ Browser Compatibility
The translate
property is supported in all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. It is part of the CSS transform module, which has excellent support across all major browsers.
🎉 Conclusion
The translate
property is essential for creating smooth and interactive web experiences by moving elements dynamically on the page.
It is widely supported and offers a straightforward way to manipulate the position of elements without affecting their layout properties. Experiment with different values for tx and ty to achieve the desired visual effects in your web projects.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (CSS translate Property), please comment here. I will help you immediately.