2.3: 2D CSS Transforms - skew()
- Page ID
- 20578
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)- Gain an understanding of skew() in the horizontal, vertical, and all directions.
2D CSS Transforms - skew()
When you skew an object in CSS you introduce a slant to it. You can slant it right, left, up, and down! You can even slant it in two directions at once.
The skew() "transformation is a shear mapping that distorts each point within an element by a certain angle in the horizontal and vertical directions. The effect is as if you grabbed each corner of the element and pulled them along a certain angle."1
Figure 1: Skewing on Horizontal, Vertical, and Both Axes
Skewing on the X Axis (Horizontal)
In an animation environment, this is a truly useful property when it comes to making something wobble especially from side to side. The skew() function can be set with either one or two values measured in degrees. If you use just one value, the object will be affected on the x-axis only. Let's look at how to code this.
selector {
transform: skew(Xdeg);
}
**In this example, there are two things being represented by "Xdeg." X is equal to the numerical amount and degrees will be represented by the abbreviation 'deg' in your code.
So, if we put this into code, it would look like what you see below. I have entered '20' for the numerical amount of degrees to skew the rocket. This will slant the rocket 20 degrees to the left.
.rocket {
transform: skew(20deg);
}
Now, it's your turn to play with the code. Here's our next CodePen.
1. In a Chrome browser window, type "transform: skew(20deg);" between the open and closing curly braces for the #skew selector. Notice how the rocket leans to the left.
2. Next, let's try a negative value and see how that affects the rocket. Type "transform: skew(-20deg);" between the open and closing curly braces for the #skew selector. The rocket now leans to the right instead of the left.
3. Play with a few more values and see what they do. Use both positive and negative numbers.
See the Pen skew() one direction (X Axis only) by Rosemary Williams Barker (@rosemary-williams-barker) on CodePen.
- Answers
-
1. .rocket {
transform: skew(20deg);
}
2. .rocket {
transform: skew(-20deg);
}
Skewing on the Y Axis (Vertical)
Skewing an object on just the Y Axis will require us to use two values for the skew() function. It will look like this:
selector {
transform: skew(0deg, Ydeg);
}
Did you see how the 'X' value changed in this one? There is a zero replacing the number there. That is because we don't want the X value to change. Only the Y value should change this time around. Let's see it in real CSS code.
.rocket {
transform: skew(0deg, 20deg);
}
Here's the CodePen for you to try it out on.
1. In a Chrome browser window, type "transform: skew(0deg, 20deg);" between the open and closing curly braces for the #skew selector. Notice how the rocket slants downward.
2. Next, let's try a negative value and see how that affects the rocket. Type "transform: skew(0deg, -20deg);" between the open and closing curly braces for the #skew selector. The rocket now upward instead of downward.
3. Play with a few more values and see what they do. Use both positive and negative numbers.
See the Pen skew() one direction (X Axis only) by Rosemary Williams Barker (@rosemary-williams-barker) on CodePen.
- Answer
-
1. .rocket {
transform: skew(0deg, 20deg);
}
2. .rocket {
transform: skew(0deg, -20deg);
}
Skewing on Both the X and Y Axes
Skewing on both the X and Y axes is quite easy. You just change both of the values for the parameters rather than just one.
selector {
transform: skew(Xdeg, Ydeg);
}
Here is how it would look in CSS code:
.rocket {
transform: skew(30deg, 30deg);
}
Here's the CodePen for you to try it out on.
1. In a Chrome browser window, type "transform: skew(30deg, 30deg);" between the open and closing curly braces for the #skew selector. Notice how the rocket slants downward.
2. Next, let's try a negative value and see how that affects the rocket. Type "transform: skew(30deg, -30deg);" between the open and closing curly braces for the #skew selector. The rocket now faces upward instead of downward.
3. Play with a few more values and see what they do. Use both positive and negative numbers.
See the Pen skew() one direction (X Axis only) by Rosemary Williams Barker (@rosemary-williams-barker) on CodePen.
- Answer
-
1. .rocket {
transform: skew(30deg, 30deg);
}
2. .rocket {
transform: skew(30deg, -30deg);
}
Footnotes
1. “Skew() - CSS: Cascading Style Sheets: MDN.” CSS: Cascading Style Sheets | MDN, MDN Web Docs, https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/skew.