CodeGym /Java Course /Frontend SELF EN /Transformation Basics

Transformation Basics

Frontend SELF EN
Level 23 , Lesson 0
Available

1.1 The transform Property

CSS transformations let you change the position, size, rotation, and skew of elements without messing up their usual flow in the document. It's a powerful tool for creating interactive and dynamic web interfaces. Let's dive into the basics of transformations using properties like transform, translate, rotate, scale, and skew.

The transform property lets you apply both 2D and 3D transformations to elements. It combines different transformation functions such as translate, rotate, scale, and skew.

Syntax:

    
      element {
        transform: transformation-function;
      }
    
  

Where:

  • transformation-function: the transformation function applied to the element

Usage Example:

CSS
    
      .element {
        transform: translate(50px, 100px);
      }
    
  

1.2 The translate Transformation

The translate transformation is used to move an element along the X and/or Y axis.

Syntax:

    
      element {
        transform: translate(x, y);
      }
    
  

Where:

  • x: shift along the X axis (can be negative or positive)
  • y: shift along the Y axis (can be negative or positive)

Example:

HTML
    
      <!DOCTYPE html>
      <html lang="en">
        <head>
          <meta charset="UTF-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <title>Translate Transformation</title>
          <style>
            .box {
              width: 100px;
              height: 100px;
              background-color: #3498db;
              transition: transform 0.5s;
            }

            .box:hover {
              transform: translate(50px, 50px);
            }
          </style>
        </head>
        <body>
          <div class="box"></div>
        </body>
      </html>
    
  

Code Explanation:

  • .box: an element with fixed dimensions and color
  • .box:hover: on hover, the translate(50px, 50px) transformation is applied, moving it 50 pixels to the right and down

1.3 The rotate Transformation

The rotate transformation is used to rotate an element around its center.

Syntax:

    
      element {
        transform: rotate(angle);
      }
    
  

Where:

  • angle: the rotation angle in degrees (can be negative or positive)
  • Example:

    HTML
        
          <!DOCTYPE html>
          <html lang="en">
            <head>
              <meta charset="UTF-8">
              <meta name="viewport" content="width=device-width, initial-scale=1.0">
              <title>Rotate Transformation</title>
              <style>
                .box {
                  width: 100px;
                  height: 100px;
                  background-color: #e74c3c;
                  transition: transform 0.5s;
                }
    
                .box:hover {
                  transform: rotate(45deg);
                }
              </style>
            </head>
            <body>
              <div class="box"></div>
            </body>
          </html>
        
      

    Code Explanation:

    • .box: an element with fixed dimensions and color
    • .box:hover: on hover, the rotate(45deg) transformation is applied, rotating it 45 degrees clockwise

    1.4 The scale Transformation

    The scale transformation is used to change the size of an element along the X and/or Y axis.

    Syntax:

        
          element {
            transform: scale(sx, sy);
          }
        
      

    Where:

    • sx: scaling along the X axis (value greater than 1 enlarges the element, less than 1 reduces it)
    • sy: scaling along the Y axis (value greater than 1 enlarges the element, less than 1 reduces it)

    Example:

    HTML
        
          <!DOCTYPE html>
          <html lang="en">
            <head>
              <meta charset="UTF-8">
              <meta name="viewport" content="width=device-width, initial-scale=1.0">
              <title>Scale Transformation</title>
              <style>
                .box {
                  width: 100px;
                  height: 100px;
                  background-color: #2ecc71;
                  transition: transform 0.5s;
                }
    
                .box:hover {
                  transform: scale(1.5, 1.5);
                }
              </style>
            </head>
            <body>
              <div class="box"></div>
            </body>
          </html>
        
      

    Code Explanation:

    • .box: an element with fixed dimensions and color
    • .box:hover: on hover, the scale(1.5, 1.5) transformation is applied, increasing its size by 1.5 times along both axes

    1.5 The skew Transformation

    The skew transformation is used to slant an element along the X and/or Y axis.

    Syntax:

        
          element {
            transform: skew(ax, ay);
          }
        
      

    Where:

    • ax: the skew angle along the X axis (can be negative or positive)
    • ay: the skew angle along the Y axis (can be negative or positive)
    HTML
        
          <!DOCTYPE html>
          <html lang="en">
            <head>
              <meta charset="UTF-8">
              <meta name="viewport" content="width=device-width, initial-scale=1.0">
              <title>Skew Transformation</title>
              <style>
                .box {
                  width: 100px;
                  height: 100px;
                  background-color: #9b59b6;
                  transition: transform 0.5s;
                }
    
                .box:hover {
                  transform: skew(20deg, 10deg);
                }
              </style>
            </head>
            <body>
              <div class="box"></div>
            </body>
          </html>
        
      

    Code Explanation:

    • .box: an element with fixed dimensions and color.
    • .box:hover: on hover, the skew(20deg, 10deg) transformation is applied, slanting it 20 degrees along the X axis and 10 degrees along the Y axis.

    1.6 Combining Multiple Transformations

    The transform property lets you combine multiple transformation functions to achieve complex effects.

    Example:

    HTML
        
          <!DOCTYPE html>
          <html lang="en">
            <head>
              <meta charset="UTF-8">
              <meta name="viewport" content="width=device-width, initial-scale=1.0">
              <title>Combined Transformations</title>
              <style>
                .box {
                  width: 100px;
                  height: 100px;
                  background-color: #3498db;
                  transition: transform 0.5s;
                }
    
                .box:hover {
                  transform: translate(50px, 50px) rotate(45deg) scale(1.5) skew(20deg, 10deg);
                }
              </style>
            </head>
            <body>
              <div class="box"></div>
            </body>
          </html>
        
      

    Code Explanation:

    • .box: an element with fixed dimensions and color
    • .box:hover: on hover, the transformations translate, rotate, scale, and skew are applied simultaneously, creating a complex effect
1
Task
Frontend SELF EN, level 23, lesson 0
Locked
Moving an Element
Moving an Element
1
Task
Frontend SELF EN, level 23, lesson 0
Locked
Element Scaling
Element Scaling
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION