CSS Function - translate()
The translate() function in CSS is used to translate, or move, an element horizontally and/or vertically along the X-axis and/or Y-axis, respectively. It's one of the transformation functions in CSS that allows you to modify the position and appearance of elements on a web page. The result is a <transform-function> datatype.
The translate() function results in the transformation, based on the two-dimensional vector [tx, ty]. The coordinates value determine the direction in which the element will move.
Possible values
The function translate() can take any of the following values as parameter(s).
Single <length-percentage> value: Can be a <length> or <percentage> value that represents the abscissa (horizontal, x-component) of the translating vector [tx,0]; whereas the ordinate (vertical, y-component) of the translating vector will have the value of 0. A <percentage> value (when assigned) is the width of the reference box defined by the transform-box property.
Double <length-percentage> values: Specifies two <length> or <percentage> values that represent both the abscissa (horizontal, x-component) of the translating vector and the ordinate (vertical, y-component) of the translating vector [tx, ty]. A <percentage> (when assigned) as first value is the width and as second part, to the height of the reference box defined by the transform-box property.
Syntax
transform: translate(<length-percentage>, <length-percentage>?);
CSS translate() - Length Value
Following is an example of translate() function with all the various ways in which length values can be passed to it, i.e., single value, double value, 0:
<html> <head> <style> div { height: 110px; width: 110px; border: 2px solid black; background-color: aquamarine; margin: 25px; } .translate-0 { transform: translate(0); background-color: tomato; } .translate-single { transform: translate(20px); background-color: yellowgreen; } .translate-double { transform: translate(20px, 50px); background-color: lightcyan; } .translate-negative { transform: translate(-2.1rem, -2rem); background-color: pink; } </style> </head> <body> <div>no translate() applied</div> <div class="translate-0">translate(0)</div> <div class="translate-single">translate(20px)</div> <div class="translate-double">translate(20px, 50px)</div> <div class="translate-negative">translate(-2.1rem, -2rem)</div> </body> </html>
CSS translate() - Percentage Value
Following is an example of translate() function with all the various ways in which percentage values can be passed to it, i.e., single value, or double values:
<html> <head> <style> div { height: 110px; width: 110px; border: 2px solid black; background-color: aquamarine; margin: 25px; } .translate-single { transform: translate(20%); background-color: yellowgreen; } .translate-double { transform: translate(20%, 50%); background-color: tomato; } </style> </head> <body> <div>no translate() applied</div> <div class="translate-single">translate(20%)</div> <div class="translate-double">translate(20%, 50%)</div> </body> </html>
To Continue Learning Please Login
Login with Google