Register to get access to free programming courses with interactive exercises

2D Transformations. Moving CSS: Transform (Transform objects)

The CSS Transform module for two-dimensional space provides several functions that allow you to transform elements by their coordinates:

  • translate() — move an object by x and y coordinates
  • rotate() — rotate an object relative to its upper-left corner
  • scale() — scale an object
  • skew() — skew an element In two-dimensional space, this function deforms the element

This course will cover all of these transformations and how they work. Don't be intimidated by the math in these lessons; you don't need to be a geometry wizard to do simple transformations.

Coordinate system

We were taught in school that in a typical rectangular coordinate system, positive values on the x-axis go from the center to the right, and positive values on the y-axis go from the center up.

Rectangular coordinate system

In browsers, however, the y-axis is inverted, and its positive values go downward from the center. This is important to remember so you don't get confused when moving objects. You might have encountered this same feature when using the top property in the course CSS: Positioning.

Rectangular coordinate system

Reference point

One important feature of the CSS Transform module is the reference point from which the transformation takes place. This point is the upper-left corner of the object. This behavior is very similar to relative positioning, allowing you to use the features of absolute and relative positioning for the same element.

Any HTML object on a page is a rectangle, regardless of its appearance. For the example, create a circle and add a border with the outline, property to see the actual borders of the block. The point with coordinates (0, 0) are in the upper-left corner of the element.

Moving an object

To move the object along the x and y use the transform property's translate() function. This is an abbreviation of two functions:

  • translateX()
  • translateY()

Which allow you to move the object along the x and y-axes respectively. The syntax of the translate function may differ:

  • transform: translate(x, y)
  • transform: translateX(x) translateY(y)

As you can see, the transform property can take on several functions simultaneously. This will come in handy when we learn about more of the available features.


Note: that translate() is counted from the zero point of the object, not the page.


Note the way elements overlap when using the transform property. An element with this property will be higher on the z-axis than the other elements, providing all other conditions are the same.

Relative VS Translate

During the lesson, you may wonder why you should use the translate() function when relative positioning and the top, right, bottom, and leftproperties have existed for a long time.

In terms of visual effect, both approaches will give the same result, but, as usual, the devil is in the details. Two devils in this case:

  • Using percentage values
  • Working with animations

The advantages of using the transform property in animation will be discussed in the corresponding course. In short, browsers process motion animations using translate() faster than they do using positioning.

In this case, it's useful to know the difference regarding the use of percentage values as soon as we start looking at transformations. This is a point that's often forgotten and leads to extra time being wasted when developing pages. In the example below, there are two squares. They're initially centered in their columns, after which the following properties are applied:

  • Number 1 square: translateX(50%)
  • Number 2 square: left: 50% with relative positioning

Assuming everything else is equal, both squares will have shifted by a different number of pixels to the right from their original position. The feature consists of a frame of reference from which the relative sizes of these two properties are counted. For translateX(), this frame is the object itself, and for a relatively positioned element, it's the container in which it's located. In this case:

  • The number 1 square has been moved 100px / 2 = 50px to the right. 100px is the width of the square.
  • The number 2 square has been moved 250px / 2 = 125px to the right. 250px is the size of the column where the square is located.

These modifications can be applied when positioning elements within a page. One of the most common tasks is to center the modal window. The positioning course had a similar test. It's notoriously difficult to do accurate positioning using only absolute positioning. There are dozens of ways to do this, but using a combination of absolute positioning and translate() is the best option.

Additional assignment

Complete the test Html: Modal Box Positioning using absolute positioning and the translate() function.


Hexlet Experts

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

About Hexlet learning process

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.bookmate">Bookmate</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.healthsamurai">Healthsamurai</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.dualboot">Dualboot</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.abbyy">Abbyy</span>
Suggested learning programs

From a novice to a developer. Get a job or your money back!

Layout Designer icon
Profession
New Under development beginner
Layout with the latest CSS standards
start anytime 5 months

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.