CSS transforms tutorial: Difference between revisions

The educational technology and digital learning wiki
Jump to navigation Jump to search
 
(16 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{stub}}
{{stub}}
{{Web technology tutorial|Beginners}}
{{Web technology tutorial|Beginners}}
<pageby nominor="false" comments="false"/>
<!-- <pageby nominor="false" comments="false"/> -->


<div class="tut_goals">
<div class="tut_goals">
Line 23: Line 23:
== Introduction ==
== Introduction ==


{{quotation|CSS transforms allows elements styled with CSS to be transformed in two-dimensional or three-dimensional space. This specification is the convergence of the [http://www.w3.org/TR/css3-2d-transforms/ CSS 2D transforms], [http://www.w3.org/TR/css3-3d-transforms/ CSS 3D transforms] and [http://www.w3.org/TR/2009/WD-SVG-Transforms-20090320/ SVG transforms] specifications.
{{quotation|CSS transforms allows elements styled with CSS to be transformed in two-dimensional or three-dimensional space. This (future) specification is the convergence of the [http://www.w3.org/TR/css3-2d-transforms/ CSS 2D transforms], [http://www.w3.org/TR/css3-3d-transforms/ CSS 3D transforms] and [http://www.w3.org/TR/2009/WD-SVG-Transforms-20090320/ SVG transforms] specifications.


The CSS visual formatting model describes a coordinate system within each element is positioned. Positions and sizes in this coordinate space can be thought of as being expressed in pixels, starting in the origin of point with positive values proceeding to the right and down. This coordinate space can be modified with the <code>transform</code> property [plus some "helper" properties]. Using transform, elements can be translated, rotated and scaled in two or three dimensional space.}} [http://www.w3.org/TR/css3-transforms/ CSS Transforms], retrieved Aug 27, 2013).
The CSS visual formatting model describes a coordinate system within each element is positioned. Positions and sizes in this coordinate space can be thought of as being expressed in pixels, starting in the origin of point with positive values proceeding to the right and down. This coordinate space can be modified with the <code>transform</code> property [plus some "helper" properties]. Using transform, elements can be translated, rotated and scaled in two or three dimensional space.}} [http://www.w3.org/TR/css3-transforms/ CSS Transforms], retrieved Aug 27, 2013).
CSS transforms specifications are (as of April 2018):
* [https://www.w3.org/TR/css-transforms-1/ CSS Transforms Module Level 1], W3C Working Draft, 30 November 2017
Of course, transforms then can be used to create Flash-like animations...


== Simple examples ==
== Simple examples ==
Line 32: Line 38:


=== Translation and rotation ===
=== Translation and rotation ===


<source lang="html5" enclose="div">
<source lang="html5" enclose="div">
Line 39: Line 46:
<div style="background-color:blue; height: 100px; width: 100px; transform-origin: 50px 50px; transform: translate(50px, 20px) rotate(45deg);"></div>
<div style="background-color:blue; height: 100px; width: 100px; transform-origin: 50px 50px; transform: translate(50px, 20px) rotate(45deg);"></div>


Remark :
This method works only in relatively recent browsers, in order for it to work with old versions of Internet Explorer, one have to add the "-ms-" prefix and the "-webkit-" prefix for it to work with old Safari and Chrome browsers.


The following code allows dealing with older legacy browsers:


<source lang="html5" enclose="div">
<div style="background-color:blue; height: 100px; width: 100px; transform-origin: 50px 50px; transform: translate(50px, 20px) rotate(45deg); -ms-transform-origin: 50px 50px; -ms-transform: translate(50px, 20px) rotate(45deg); -webkit-transform-origin: 50px 50px; -webkit-transform: translate(50px, 20px) rotate(45deg);"></div>
</source>
<div style="margin-bottom: 40px;">
<div style=" background-color:blue; height: 100px; width: 100px; transform-origin: 50px 50px; transform: translate(50px, 20px) rotate(45deg); -ms-transform-origin: 50px 50px; -ms-transform: translate(50px, 20px) rotate(45deg); -webkit-transform-origin: 50px 50px; -webkit-transform: translate(50px, 20px) rotate(45deg);"></div>
</div>


=== Adding perspective ===
=== Adding perspective ===
Line 47: Line 65:


<source lang="html5" enclose="div">
<source lang="html5" enclose="div">
<div style="perspective: 500px; border: 1px solid black;">
<div style="background-color:lightgrey; height:100px; width:100px; perspective: 500px; border: 1px solid black;">
  <div style="background-color:blue; height: 100px; width: 100px; transform-origin: 50px 50px; transform: translate(50px, 20px) rotateY(45deg) rotate(45deg);"></div>
  <div style="background-color:blue; height: 100px; width: 100px; transform-origin: 50px 50px; transform: rotateY(45deg) rotate(45deg);"></div>
<div>
</div>
</source>
</source>


<div style="height:100px; width:100px; perspective: 500px; border: 1px solid black;">
Once again, for this method to work with old Safari and Chrome editions, add the "-webkit-" prefix.
  <div style="background-color:blue; height: 100px; width: 100px; transform-origin: 50px 50px; transform: translate(50px, 20px) rotateY(45deg) rotate(45deg);"></div>
<source lang="html5" enclose="div">
<div style="background-color:lightgrey; height:100px; width:100px; perspective: 500px; border: 1px solid black;">
  <div style="background-color:blue; height: 100px; width: 100px; transform-origin: 50px 50px; transform: rotateY(45deg) rotate(45deg); -webkit-transform-origin: 50px 50px; -webkit-transform: rotateY(45deg) rotate(45deg);"></div>
</source>
 
<div style="margin-left:100px; margin-top: 20px; margin-bottom: 20px;">
<div style="background-color:lightgrey; height:100px; width:100px; perspective: 500px; border: 1px solid black;">
<div style="background-color:blue; height: 100px; width: 100px; transform-origin: 50px 50px; transform: rotateY(45deg) rotate(45deg); -webkit-transform-origin: 50px 50px; -webkit-transform: rotateY(45deg) rotate(45deg);"></div>
</div>
</div>
</div>


== Links ==
== Links ==
; Tutorials and demos
* [https://developer.mozilla.org/en-US/docs/Web/CSS/transform transform] (MDN)
* [https://www.w3schools.com/css/css3_2dtransforms.asp CSS 2D Transforms] (W3C Schools)
* [https://www.w3schools.com/css/css3_3dtransforms.asp CSS 3D Transforms] (W3C)


; Official
; Official
* [http://www.w3.org/TR/css3-transforms/ CSS Transforms], W3C Working Draft 11 September 2012 (as of Aug. 2013)
* [https://www.w3.org/Style/CSS/current-work List of various CSS specifications] completed or in progress. As of April 2018, there are '''many'''.
* [http://dev.w3.org/csswg/css-transforms/ CSS Transforms Level 1], Editor's Draft 4 July 2013 (as of Aug 2013)
* [http://www.w3.org/TR/css3-transforms/ CSS Transforms], W3C Working Draft, 30 November 2017
* [http://dev.w3.org/csswg/css-transforms/ CSS Transforms Level 1], Editor’s Draft, 14 January 2018


[[Category: CSS]]
[[Category: CSS]]

Latest revision as of 08:29, 4 April 2018

Draft

Learning goals
  • Be able to create simple CSS transforms
Prerequisites
Level and target population
  • Beginners
Teaching materials
  • Example text: ....
Remarks
  • This tutorial is intended for students in educational technology or any other field that is technology intensive. For people who need less, there exist many easy CSS tutorials on the web. This text is intended for students who also must learn principles and who are willing to learn CSS by doing a project, looking at CSS code and online reference manuals.
  • Ideally, a teacher also should assign a text formatting task, during or before assigning this tutorial for reading).

Introduction

“CSS transforms allows elements styled with CSS to be transformed in two-dimensional or three-dimensional space. This (future) specification is the convergence of the CSS 2D transforms, CSS 3D transforms and SVG transforms specifications.

The CSS visual formatting model describes a coordinate system within each element is positioned. Positions and sizes in this coordinate space can be thought of as being expressed in pixels, starting in the origin of point with positive values proceeding to the right and down. This coordinate space can be modified with the transform property [plus some "helper" properties]. Using transform, elements can be translated, rotated and scaled in two or three dimensional space.” CSS Transforms, retrieved Aug 27, 2013).

CSS transforms specifications are (as of April 2018):


Of course, transforms then can be used to create Flash-like animations...

Simple examples

All, slightly modified, from the specification (Aug 2013 version)

Translation and rotation

<div style="background-color:blue; height: 100px; width: 100px; transform-origin: 50px 50px; transform: translate(50px, 20px) rotate(45deg);"></div>

Remark :

This method works only in relatively recent browsers, in order for it to work with old versions of Internet Explorer, one have to add the "-ms-" prefix and the "-webkit-" prefix for it to work with old Safari and Chrome browsers.

The following code allows dealing with older legacy browsers:

<div style="background-color:blue; height: 100px; width: 100px; transform-origin: 50px 50px; transform: translate(50px, 20px) rotate(45deg); -ms-transform-origin: 50px 50px; -ms-transform: translate(50px, 20px) rotate(45deg); -webkit-transform-origin: 50px 50px; -webkit-transform: translate(50px, 20px) rotate(45deg);"></div>

Adding perspective

This example shows how to use perspective. In addition, we do a 3D rotation around the Y-axis using the rotateY() function.

<div style="background-color:lightgrey; height:100px; width:100px; perspective: 500px; border: 1px solid black;">
 <div style="background-color:blue; height: 100px; width: 100px; transform-origin: 50px 50px; transform: rotateY(45deg) rotate(45deg);"></div>
</div>

Once again, for this method to work with old Safari and Chrome editions, add the "-webkit-" prefix.

<div style="background-color:lightgrey; height:100px; width:100px; perspective: 500px; border: 1px solid black;">
 <div style="background-color:blue; height: 100px; width: 100px; transform-origin: 50px 50px; transform: rotateY(45deg) rotate(45deg); -webkit-transform-origin: 50px 50px; -webkit-transform: rotateY(45deg) rotate(45deg);"></div>

Links

Tutorials and demos
Official