How to move element center from both horizontal and vertical using transform:translate
while using transform: translate(); you should use position: absolute; to the element.
position: absolute right: 50%; bottom: 50%; transform: translate(50%,50%);
answered Nov 1, 2016 at 5:24
Venu Madhav Venu Madhav
413 1 1 gold badge 5 5 silver badges 14 14 bronze badges
By default, every element is static which means you cannot move them. So add this to your rule.
position: relative;
So you can move it
Click HERE to learn more about position
answered Nov 1, 2016 at 5:08
6,676 5 5 gold badges 25 25 silver badges 38 38 bronze badges
I believe to accomplish that trick you’ll need absolute positioning and a top/left value of 50%.
.main
answered Nov 1, 2016 at 5:14
398 5 5 silver badges 13 13 bronze badges
- html
- css
-
The Overflow Blog
Related
Hot Network Questions
Subscribe to RSS
Question feed
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.11.15.1019
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
CSS Transform: How to Use It on Your Website
From colors and fonts to grid layouts and carousels, CSS affects how your website looks and how much your visitors will enjoy it.

When you dig deeper into what CSS can do, you’ll discover features like CSS transform, which lets you change the positions and shapes of elements on a page.
By knowing the transform property, you’ll find ways to further build engaging interfaces from scratch. In this post, you’ll learn everything you need to know to start using the CSS transform property, including:
- what CSS transform is
- how to transform elements in two dimensions
- how to transform elements in three dimensions
- the CSS transform origin property
- how to animate CSS transform
transform
The transform CSS property lets you rotate, scale, skew, or translate an element. It modifies the coordinate space of the CSS visual formatting model.
Try it
If the property has a value different than none , a stacking context will be created. In that case, the element will act as a containing block for any position: fixed; or position: absolute; elements that it contains.
Warning: Only transformable elements can be transform ed. That is, all elements whose layout is governed by the CSS box model except for: non-replaced inline boxes, table-column boxes, and table-column-group boxes.
Syntax
/* Keyword values */ transform: none; /* Function values */ transform: matrix(1, 2, 3, 4, 5, 6); transform: matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); transform: perspective(17px); transform: rotate(0.5turn); transform: rotate3d(1, 2, 3, 10deg); transform: rotateX(10deg); transform: rotateY(10deg); transform: rotateZ(10deg); transform: translate(12px, 50%); transform: translate3d(12px, 50%, 3em); transform: translateX(2em); transform: translateY(3in); transform: translateZ(2px); transform: scale(2, 0.5); transform: scale3d(2.5, 1.2, 0.3); transform: scaleX(2); transform: scaleY(0.5); transform: scaleZ(0.3); transform: skew(30deg, 20deg); transform: skewX(30deg); transform: skewY(1.07rad); /* Multiple function values */ transform: translateX(10px) rotate(10deg) translateY(5px); transform: perspective(500px) translate(10px, 0, 20px) rotateY(3deg); /* Global values */ transform: inherit; transform: initial; transform: revert; transform: revert-layer; transform: unset;
The transform property may be specified as either the keyword value none or as one or more values.
If perspective() is one of multiple function values, it must be listed first.
Values
One or more of the CSS transform functions to be applied. The transform functions are multiplied in order from left to right, meaning that composite transforms are effectively applied in order from right to left.
Specifies that no transform should be applied.
Accessibility concerns
Scaling/zooming animations are problematic for accessibility, as they are a common trigger for certain types of migraine. If you need to include such animations on your website, you should provide a control to allow users to turn off animations, preferably site-wide.
Also, consider making use of the prefers-reduced-motion media feature — use it to write a media query that will turn off animations if the user has reduced animation specified in their system preferences.
- MDN Understanding WCAG, Guideline 2.3 explanations
- Understanding Success Criterion 2.3.3 | W3C Understanding WCAG 2.1
Formal definition
| Initial value | none |
|---|---|
| Applies to | transformable elements |
| Inherited | no |
| Percentages | refer to the size of bounding box |
| Computed value | as specified, but with relative lengths converted into absolute lengths |
| Animation type | a transform |
| Creates stacking context | yes |
transform-origin
The transform-origin CSS property sets the origin for an element’s transformations.
Try it
The transform origin is the point around which a transformation is applied. For example, the transform origin of the rotate() function is the center of rotation.
In effect, this property wraps a pair of translations around the element’s other transformations. The first translation moves the transform origin to the true origin at ( 0 , 0 ) . Then the other transformations are applied, and because the transform origin is at ( 0 , 0 ) , those transformations act about the transform origin. Finally, the opposite translation is applied, moving the transform origin back to its original location. Consequently, this definition
transform-origin: -100% 50%; transform: rotate(45deg);
results in the same transformation as
transform-origin: 0 0; transform: translate(-100%, 50%) rotate(45deg) translate(100%, -50%);
Reading from right to left, translate(100%, -50%) is the translation to bring the transform origin to the true origin, rotate(45deg) is the original transformation, and translate(-100%, 50%) is the translation to restore the transform origin to its original location.
By default, the origin of a transform is center .
Syntax
/* One-value syntax */ transform-origin: 2px; transform-origin: bottom; /* x-offset | y-offset */ transform-origin: 3cm 2px; /* x-offset-keyword | y-offset */ transform-origin: left 2px; /* x-offset-keyword | y-offset-keyword */ transform-origin: right top; /* y-offset-keyword | x-offset-keyword */ transform-origin: top right; /* x-offset | y-offset | z-offset */ transform-origin: 2px 30% 10px; /* x-offset-keyword | y-offset | z-offset */ transform-origin: left 5px -3px; /* x-offset-keyword | y-offset-keyword | z-offset */ transform-origin: right bottom 2cm; /* y-offset-keyword | x-offset-keyword | z-offset */ transform-origin: bottom right 2cm; /* Global values */ transform-origin: inherit; transform-origin: initial; transform-origin: revert; transform-origin: revert-layer; transform-origin: unset;
The transform-origin property may be specified using one, two, or three values, where each value represents an offset. Offsets that are not explicitly defined are reset to their corresponding initial values.
If two or more values are defined and either no value is a keyword, or the only used keyword is center , then the first value represents the horizontal offset and the second represents the vertical offset.
- One-value syntax:
- The value must be a , a , or one of the keywords left , center , right , top , and bottom .
- One value must be a , a , or one of the keywords left , center , and right .
- The other value must be a , a , or one of the keywords top , center , and bottom .
- The first two values are the same as for the two-value syntax.
- The third value must be a . It always represents the Z offset.
Values
Is one of the left , right , top , bottom , or center keyword describing the corresponding offset.
Is one of the left , right , or center keyword describing how far from the left edge of the box the origin of the transform is set.
Is one of the top , bottom , or center keyword describing how far from the top edge of the box the origin of the transform is set.
The keywords are convenience shorthands and match the following values:
Keyword Value left 0% center 50% right 100% top 0% bottom 100% Formal definition
Initial value 50% 50% 0 Applies to transformable elements Inherited no Percentages refer to the size of bounding box Computed value for the absolute value, otherwise a percentage Animation type simple list of length, percentage, or calc Note: The initial value of transform-origin is 0 0 for all SVG elements except for root elements and elements that are a direct child of a foreignObject, and whose transform-origin is 50% 50% , like other CSS elements. See the SVG transform-origin attribute for more information.
Formal syntax
transform-origin =
[ left | center | right | top | bottom | ] |
[ left | center | right | ] [ top | center | bottom | ] ? |
[ [ center | left | right ] && [ center | top | bottom ] ] ?=
|Examples
A demonstration of various transform values
This example shows the effect of choosing different transform-origin values for a variety of transformation functions.
div class="container"> div class="example"> div class="box box1"> div> div class="box original"> div> div> pre> transform: none; pre> div class="example"> div class="box box2"> div> div class="box original"> div> div> pre> transform: rotate(30deg); pre> div class="example"> div class="box box3"> div> div class="box original"> div> div> pre> transform: rotate(30deg); transform-origin: 0 0; pre> div class="example"> div class="box box4"> div> div class="box original"> div> div> pre> transform: rotate(30deg); transform-origin: 100% 100%; pre> div class="example"> div class="box box5"> div> div class="box original"> div> div> pre> transform: rotate(30deg); transform-origin: -1em -3em; pre> div class="example"> div class="box box6"> div> div class="box original"> div> div> pre> transform: scale(1.7); pre> div class="example"> div class="box box7"> div> div class="box original"> div> div> pre> transform: scale(1.7); transform-origin: 0 0; pre> div class="example"> div class="box box8"> div> div class="box original"> div> div> pre> transform: scale(1.7); transform-origin: 100% -30%; pre> div class="example"> div class="box box9"> div> div class="box original"> div> div> pre> transform: skewX(50deg); transform-origin: 100% -30%; pre> div class="example"> div class="box box10"> div> div class="box original"> div> div> pre> transform: skewY(50deg); transform-origin: 100% -30%; pre> div>
.container display: grid; grid-template-columns: 200px 100px; gap: 20px; > .example position: relative; margin: 0 2em 4em 5em; > .box display: inline-block; width: 3em; height: 3em; border: solid 1px; background-color: palegreen; > .original position: absolute; left: 0; opacity: 20%; > .box1 transform: none; > .box2 transform: rotate(30deg); > .box3 transform: rotate(30deg); transform-origin: 0 0; > .box4 transform: rotate(30deg); transform-origin: 100% 100%; > .box5 transform: rotate(30deg); transform-origin: -1em -3em; > .box6 transform: scale(1.7); > .box7 transform: scale(1.7); transform-origin: 0 0; > .box8 transform: scale(1.7); transform-origin: 100% -30%; > .box9 transform: skewX(50deg); transform-origin: 100% -30%; > .box10 transform: skewY(50deg); transform-origin: 100% -30%; >Specifications
Specification CSS Transforms Module Level 1
# transform-origin-propertyBrowser compatibility
BCD tables only load in the browser
See also
- Using CSS transforms
- https://css-tricks.com/almanac/properties/t/transform-origin/
transform
CSS-свойство transform позволяет вам поворачивать, масштабировать, наклонять или сдвигать элемент. Оно модифицирует координатное пространство для CSS визуальной форматируемой модели.
Интерактивный пример
Если свойство имеет значение, отличное от none , будет создан контекст наложения. В этом случае, элемент будет действовать как содержащий блок для любых элементов position: fixed; или position: absolute; которые он содержит.
Предупреждение: Только трансформируемый элемент может быть transform . Т.е. все элементы, шаблоны которых регулируются блочной моделью CSS, кроме : неизменяемые инлайновые блоки, блоки таблица-колонка, и блоки таблица-колонка-группа (en-US) .
Синтаксис
/* Значения ключевым словом*/ transform: none; /* Значения функций */ transform: matrix(1, 2, 3, 4, 5, 6); transform: matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); transform: perspective(17px); transform: rotate(0.5turn); transform: rotate3d(1, 2, 3, 10deg); transform: rotateX(10deg); transform: rotateY(10deg); transform: rotateZ(10deg); transform: translate(12px, 50%); transform: translate3d(12px, 50%, 3em); transform: translateX(2em); transform: translateY(3in); transform: translateZ(2px); transform: scale(2, 0.5); transform: scale3d(2.5, 1.2, 0.3); transform: scaleX(2); transform: scaleY(0.5); transform: scaleZ(0.3); transform: skew(30deg, 20deg); transform: skewX(30deg); transform: skewY(1.07rad); /* Мультифункциональные значения */ transform: translateX(10px) rotate(10deg) translateY(5px); transform: perspective(500px) translate(10px, 0, 20px) rotateY(3deg); /* Глобальные значения */ transform: inherit; transform: initial; transform: unset;
Свойство transform может быть указано как значение ключевого слова none или как одно или более значений .
Значения
Одна или более применяемых функций CSS-трансформации (en-US) . Функции трансформации умножаются в порядке слева направо, что означает, что составное трансформирование эффективнее применять в порядке справа налево.
Указывает, что трансформация не должна применяться.
Формальный синтаксис
transform =
none | (en-US)
=
(en-US) + (en-US)Если perspective() (en-US) является одним из мультифункциональных значений, оно должно быть указано первым.
CSS transform Property
Rotate, skew, and scale three different elements:
div.b transform: skewY(20deg);
>div.c transform: scaleY(1.5);
>Definition and Usage
The transform property applies a 2D or 3D transformation to an element. This property allows you to rotate, scale, move, skew, etc., elements.
Default value: none Inherited: no Animatable: yes. Read about animatable Try it Version: CSS3 JavaScript syntax: object.style.transform=»rotate(7deg)» Try it Browser Support
The numbers in the table specify the first browser version that fully supports the property.
Numbers followed by -webkit-, -moz-, or -o- specify the first version that worked with a prefix.
Property transform (2D) 36.0
4.0 -webkit-10.0
9.0 -ms-16.0
3.5 -moz-9.0
3.2 -webkit-23.0
15.0 -webkit-
10.5 -o-transform (3D) 36.0
12.0 -webkit-12.0 10.0 9.0
4.0 -webkit-23.0
15.0 -webkit-Syntax
transform: none|transform-functions|initial|inherit;
Property Values
Value Description Demo none Defines that there should be no transformation Demo ❯ matrix(n,n,n,n,n,n) Defines a 2D transformation, using a matrix of six values Demo ❯ matrix3d
(n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n)Defines a 3D transformation, using a 4×4 matrix of 16 values translate(x,y) Defines a 2D translation Demo ❯ translate3d(x,y,z) Defines a 3D translation translateX(x) Defines a translation, using only the value for the X-axis translateY(y) Defines a translation, using only the value for the Y-axis translateZ(z) Defines a 3D translation, using only the value for the Z-axis scale(x,y) Defines a 2D scale transformation Demo ❯ scale3d(x,y,z) Defines a 3D scale transformation scaleX(x) Defines a scale transformation by giving a value for the X-axis scaleY(y) Defines a scale transformation by giving a value for the Y-axis scaleZ(z) Defines a 3D scale transformation by giving a value for the Z-axis rotate(angle) Defines a 2D rotation, the angle is specified in the parameter Demo ❯ rotate3d(x,y,z,angle) Defines a 3D rotation rotateX(angle) Defines a 3D rotation along the X-axis Demo ❯ rotateY(angle) Defines a 3D rotation along the Y-axis Demo ❯ rotateZ(angle) Defines a 3D rotation along the Z-axis skew(x-angle,y-angle) Defines a 2D skew transformation along the X- and the Y-axis Demo ❯ skewX(angle) Defines a 2D skew transformation along the X-axis Demo ❯ skewY(angle) Defines a 2D skew transformation along the Y-axis Demo ❯ perspective(n) Defines a perspective view for a 3D transformed element initial Sets this property to its default value. Read about initial inherit Inherits this property from its parent element. Read about inherit More Examples
Images thrown on the table
This example demonstrates how to create «polaroid» pictures and rotate the pictures.8.6 CSS transform
We’ve seen so far how CSS allows us to apply colors, set fonts, tweak the text settings, position elements, space them, decorate them, move them around.
CSS transforms are a collection of functions that allow to shape elements in particular ways:
- translate: moves the element along up to 3 axis (x,y and z)
- rotate: moves the element around a central point
- scale: resizes the element
- skew: distorts the element
transform properties
There are 3 CSS transform properties available:
- transform defines which transform function to use (translate, rotate, scale…)
- transform-origin allows to modify the origin point of a transformation (works like background positions)
- transform-style is for 3d settings
Note that unlike background and border , transform is not a shorthand property.
We’ll only use transform here.
Doesn’t break the Flow
To prevent unexpected behavior, transformed elements do not affect the Flow. Whether rotated, scaled or translated, they won’t affect other elements.
translate
The translate() function allows to move an element across the plane (on the x and y axis). It accepts either:
- 1 parameter: moves the element along the x axis
- 2 parameters: first value is for the x axis, second for the y one
It’s like using relative positioning with left and top values.
Let’s redo our circuit animation using translation instead of left/top positioning:
@keyframes translating 0% transform: translate(0, 0);> 25% transform: translate(240px, 0);> 50% transform: translate(240px, 140px);> 75% transform: translate(0, 140px);> 100% transform: translate(0, 0);> > p animation: translating 4s linear infinite;>Remember: transform is the CSS property, translate() is the CSS value attached to that property (and also happens to be a function).
You can use translateX() and translateY() to only move your element along the x and y axis respectively.
rotate
The rotate() function allows to make an element revolve around a fixed point. By default, it revolves around the element’s center. Think of it as vinyl being played on a turntable.
rotate() accepts only 1 parameter, which is an angle value defined in degrees deg , gradians grad , radians rad or turns turn (with 1 turn being equivalent to a full circle).
@keyframes rotating 0% transform: rotate(0deg);> 100% transform: rotate(360deg);> > p animation: rotating 4s linear infinite;>scale
The scale() function allows to resize an element. It can either expand or shrink it. The function accepts either:
- 1 parameter: the element is resized uniformily in height and width
- 2 parameters: the first value resizes the element horizontally, the second one vertically
The range of possible value is:
- 1 : the element retains its original size
- 2 : the element doubles in size
- 0.5 : the element is half of its size
- 0 : the element basically disappears (as its height and width are equal to zero)
- -1 : the element is mirrored
@keyframes scaling 0% transform: scale(1);> 20% transform: scale(2);> 40% transform: scale(0.5);> 60% transform: scale(0);> 80% transform: scale(-1);> 100% transform: scale(1);> > p animation: scaling 10s steps(1) 0s infinite;>scale(1): normal size
scale(2): double size
scale(0.5): half size
scale(0): zero size (invisible)
scale(-1): mirrored
Like translate() , the scale() function has x and y versions: scaleX() and scaleY() to resize horizontally and vertically respectively.
skew
The skew() function allows to distort an element, by dragging its sides along a line basically.
This transform function is rarely used, as its effects are quite unpredictable, and its results not necessarily appealing. Nevertheless, let’s see how it works.
Like scale() , the skew() functions accepts either:
- 1 parameter: the element is distorted horizontally
- 2 parameters: the first value distorts the element horizontally, the second one vertically
And like rotate() , skew() only accepts angle values like degrees deg .
@keyframes skewing 0% transform: skew(0deg);> 20% transform: skew(10deg);> 40% transform: skew(45deg);> 60% transform: skew(90deg);> 80% transform: skew(120deg);> 100% transform: skew(0deg);> > p animation: skewing 10s steps(1) 0s infinite;>skew(0deg): no distortion
skew(10deg): subtle horizontal distortion
skew(45deg): quarter distortion
skew(90deg): half distortion (invisible)
skew(120deg): same as -60deg
3d functions
We’ve seen how transform functions operate on a plane, along the x and y axis.
- translate() with up to 2 parameters:
- translate(x)
- translate(x,y)
But all these functions also have a 3d version.
For example, translate() has a translate3d() version that performs transformation along 3 dimensions, which means it includes the z axis as well (and as such, a standalone translateZ() function exists as well).
The z parameter basically make the element move closer and further, whether we increase or decrease its value. It’s like zooming in and out.
@keyframes zooming 0% transform: translate3d(0, 0, 0);> 100% transform: translate3d(0, 0, 200px);> > p animation: zooming 5s alternate;>The green block rise 200px “upwards”, as if coming closer to us, along the z axis.
perspective: 500; needs to be applied to the parent element in order for the 3d space to become active. Alternatively, transform: perspective(500px); can be used as well.
Learn CSS with my ebook
This ebook is a step by step guide in which I teach you how to build your own personal webpage from scratch, line by line, with HTML5, CSS3, and even JS.
The underlying source code used to format and display this website is licensed under the MIT license.
Powered by Jekyll. Hosted on GitHub.
CSS 2D Transforms
CSS transforms allow you to move, rotate, scale, and skew elements.
Mouse over the element below to see a 2D transformation:
In this chapter you will learn about the following CSS property:
Browser Support
The numbers in the table specify the first browser version that fully supports the property.
Property transform 36.0 10.0 16.0 9.0 23.0 CSS 2D Transforms Methods
With the CSS transform property you can use the following 2D transformation methods:
- translate()
- rotate()
- scaleX()
- scaleY()
- scale()
- skewX()
- skewY()
- skew()
- matrix()
Tip: You will learn about 3D transformations in the next chapter.
The translate() Method
The translate() method moves an element from its current position (according to the parameters given for the X-axis and the Y-axis).
The following example moves the element 50 pixels to the right, and 100 pixels down from its current position:
Example
div <
transform: translate(50px, 100px);
>The rotate() Method
The rotate() method rotates an element clockwise or counter-clockwise according to a given degree.
The following example rotates the element clockwise with 20 degrees:
Example
div <
transform: rotate(20deg);
>Using negative values will rotate the element counter-clockwise.
The following example rotates the element counter-clockwise with 20 degrees:
Example
div <
transform: rotate(-20deg);
>The scale() Method
The scale() method increases or decreases the size of an element (according to the parameters given for the width and height).
The following example increases the element to be two times of its original width, and three times of its original height:
Example
div <
transform: scale(2, 3);
>The following example decreases the element to be half of its original width and height:
Example
div <
transform: scale(0.5, 0.5);
>The scaleX() Method
The scaleX() method increases or decreases the width of an element.
The following example increases the element to be two times of its original width:
Example
div <
transform: scaleX(2);
>The following example decreases the element to be half of its original width:
Example
div <
transform: scaleX(0.5);
>The scaleY() Method
The scaleY() method increases or decreases the height of an element.
The following example increases the element to be three times of its original height:
Example
div <
transform: scaleY(3);
>The following example decreases the element to be half of its original height:
Example
div <
transform: scaleY(0.5);
>The skewX() Method
The skewX() method skews an element along the X-axis by the given angle.
The following example skews the element 20 degrees along the X-axis:
Example
div <
transform: skewX(20deg);
>The skewY() Method
The skewY() method skews an element along the Y-axis by the given angle.
The following example skews the element 20 degrees along the Y-axis:
Example
div <
transform: skewY(20deg);
>The skew() Method
The skew() method skews an element along the X and Y-axis by the given angles.
The following example skews the element 20 degrees along the X-axis, and 10 degrees along the Y-axis:
Example
div <
transform: skew(20deg, 10deg);
>If the second parameter is not specified, it has a zero value. So, the following example skews the element 20 degrees along the X-axis:
Example
div <
transform: skew(20deg);
>The matrix() Method
The matrix() method combines all the 2D transform methods into one.
The matrix() method take six parameters, containing mathematic functions, which allows you to rotate, scale, move (translate), and skew elements.
The parameters are as follow: matrix(scaleX(), skewY(), skewX(), scaleY(), translateX(), translateY())
Example
div <
transform: matrix(1, -0.3, 0, 1, 0, 0);
>CSS Transform Properties
The following table lists all the 2D transform properties:
Property Description transform Applies a 2D or 3D transformation to an element transform-origin Allows you to change the position on transformed elements CSS 2D Transform Methods
Function Description matrix(n,n,n,n,n,n) Defines a 2D transformation, using a matrix of six values translate(x,y) Defines a 2D translation, moving the element along the X- and the Y-axis translateX(n) Defines a 2D translation, moving the element along the X-axis translateY(n) Defines a 2D translation, moving the element along the Y-axis scale(x,y) Defines a 2D scale transformation, changing the elements width and height scaleX(n) Defines a 2D scale transformation, changing the element’s width scaleY(n) Defines a 2D scale transformation, changing the element’s height rotate(angle) Defines a 2D rotation, the angle is specified in the parameter skew(x-angle,y-angle) Defines a 2D skew transformation along the X- and the Y-axis skewX(angle) Defines a 2D skew transformation along the X-axis skewY(angle) Defines a 2D skew transformation along the Y-axis