Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

2D transform fundamentals

From: CSS: Transitions and Transforms

Video: 2D transform fundamentals

Whereas Transitions are designed to interpolate the changes in CSS properties over a specified time, Transforms actually manipulate HTML objects, such as divs and images. The W3C working group for CSS 2D Transforms says in their specification that CSS 2D Transforms allows elements rendered by CSS to be transformed in two-dimensional space. Granted, not the most informative definition, but you really don't need it once you can see what transforms can do.

2D transform fundamentals

Whereas Transitions are designed to interpolate the changes in CSS properties over a specified time, Transforms actually manipulate HTML objects, such as divs and images. The W3C working group for CSS 2D Transforms says in their specification that CSS 2D Transforms allows elements rendered by CSS to be transformed in two-dimensional space. Granted, not the most informative definition, but you really don't need it once you can see what transforms can do.

All transforms follow the same basic syntax, the keyword "transform" followed by a colon and then the function name with any parameters in parentheses. There are four basic transform functions, the first, Translate, moves objects around the screen. transform: translate(10px, 10px) moves the target element right 10 pixels and down 10 pixels. You can also use negative values to move elements up and left.

There are also functions to apply if you just want to move the object along one axis translateX and translateY. Let's take a look at how transform translate works. Here I have a ball set up with the translateY function, using the Active Trigger. So when I move my mouse over it, nothing happens, but when I put the mouse down and click on it, it translates down 300 pixels along the Y axis. Now no transitions are being applied here, but you can do that as you'll see later, this is just the raw transform.

I'll take my mouse off, and it translates back up. Not only can you move objects, but you can resize them with transform: scale. Like translate, the scale function takes two arguments, X and Y, where 1 is the same size as the original, .5 is half the size, and 2 is twice the scale. There're versions for resizing along a single axis as well, scaleX and scaleY. Here's a quick look at transform scale in action applied to a series of form buttons, and I'm using the Hover State as a trigger.

First I'll scale up in both directions, now I'll scale up just in the X direction, and now Y. Objects can also be rotated to any degree from 0 to 360 and beyond. Let me show you how that looks. Here I have a baseball set up to rotate 60 degrees on hover, and out, in, and out. There's one last transform function that can be applied, skew.

Page elements can be skewed horizontally along the X axis or vertically on the Y. Skew values are given in degrees. Here is what skewing a textbox looks like both in the X and Y directions, with quotes from the highly-skewed Mark Twain. There is a skewX and skewY. Those are all 2D transform functions. But there is another related property that can be used in conjunction with any of the transform functions, transform-origin.

By default, all transform functions assume a center origin. You can modify that by specifying a different set of coordinates, either in percentages, pixels, or keywords, like left and bottom. To demonstrate transform origin, let's take our rotate example, the baseball, and switch the origin point for the transformation to the bottom left corner. Now when I hover over the baseball, instead of staying in place and rotating, it rotates down and to the right.

And now I'll my mouse away, and it rotates back up. You'll need to use vendor prefixes for all of the 2D transforms as a quick look as that caniuse site indicates. However, I still recommend using the generic CSS transform property to future-proof your code. Finally, I'm sure you noticed that all these transforms were applied immediately like any standard hover state change. The good news is that transforms can be combined with transition, so you can manipulate your page elements with a much smoother final appearance.

Here's one final demo, to drop a ball and give it a little bounce. Now that you've gotten the taste of what's possible with 2D transforms, you're ready to explore the 3D realm.

Show transcript

This video is part of

Image for CSS: Transitions and Transforms
CSS: Transitions and Transforms

26 video lessons · 14312 viewers

Joseph Lowery
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed CSS: Transitions and Transforms.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.