{"id":152,"date":"2026-01-11T23:58:28","date_gmt":"2026-01-12T04:58:28","guid":{"rendered":"https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/?post_type=chapter&#038;p=152"},"modified":"2026-02-18T13:41:25","modified_gmt":"2026-02-18T18:41:25","slug":"chapter-10-animation","status":"publish","type":"chapter","link":"https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/chapter\/chapter-10-animation\/","title":{"raw":"Chapter 10 \u2022 Animation","rendered":"Chapter 10 \u2022 Animation"},"content":{"raw":"Animation has considerable power to catch readers' attention, to entertain, and to illustrate mechanisms involving motion. Designers must be careful not to over-distract readers with repeating or obtrusive animations.\r\n\r\nThree methods to animate with CSS styles include:\r\n<ol>\r\n \t<li>CSS animation with no user interaction<\/li>\r\n \t<li>CSS animation with user interaction through the mouse-over (:hover) and click (:active) pseudoclasses<\/li>\r\n \t<li>CSS animation with JavaScript interaction<\/li>\r\n<\/ol>\r\n<h2>Requirements for CSS Animation<\/h2>\r\nAnimating a tag with CSS requires three components:\r\n<ol>\r\n \t<li>an image or text object suitable for animating, often a transparent PNG or GIF like the car below<\/li>\r\n \t<li>a named @keyframes rule with major change points specified as percentages of completion<\/li>\r\n \t<li>a link in the tag's CSS selector to the named @keyframes rule using the animation-name style<\/li>\r\n \t<li>an animation-duration style specifying the length of the animation<\/li>\r\n<\/ol>\r\n[caption id=\"attachment_155\" align=\"alignnone\" width=\"200\"]<a href=\"https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/wp-content\/uploads\/sites\/449\/2026\/01\/car.png\"><img src=\"https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/wp-content\/uploads\/sites\/449\/2026\/01\/car-300x300.png\" alt=\"\" width=\"200\" height=\"200\" class=\"wp-image-155\" \/><\/a> Car illustration courtesy of pngtree.com[\/caption]\r\n<h2>Example<\/h2>\r\nAssume we want to animate the above car by having it accelerate and drive across the screen.\r\n\r\nHTML for the car:\r\n<pre>&lt;img id=\"car\" src=\"car.png\" \/&gt;<\/pre>\r\n@keyframes rule for the car animation:\r\n<pre>@keyframes letsgo {\r\n    0% {left: 0;}\r\n  100% {left: 120vw;} \/* vw (viewport widths) units &gt;100 position the tag off the page *\/\r\n}\r\n\r\n\r\n<\/pre>\r\nCSS for the car (remember that \/* denotes comments that explain the style):\r\n<pre>#car {\r\n    width: 200px;\r\n    position: relative; \/* position must be declared even though \"relative\" is the default *\/\r\n    left: 0;\r\n    animation-name: letsgo; \/* connects the car to the @keyframes rule *\/\r\n    animation-duration: 5s; \/* 5 seconds *\/\r\n    animation-fill-mode: forwards; \/* go to the end and stay there *\/\r\n    animation-timing-function: ease-in; \/* to accelerate to full speed *\/\r\n<\/pre>\r\n<h2>Interaction with :hover or :active<\/h2>\r\nTo introduce user interaction with the :hover (mouse over) or :active (click) pseudo classes, first the animation-play-state must be set to paused. This means the animation will not start by itself:\r\n<pre>#car {\r\n    animation-play-state: paused;\r\n}\r\n<\/pre>\r\nThe :hover pseudoclass selects the tag when the mouse is over it, e.g., #car:hover. After selecting the car with #car:hover, you can set the animation-play-state to running:\r\n<pre>#car:hover {\r\n    animation-play-state: running;\r\n}\r\n<\/pre>\r\nFor the animation to keep playing, users must keep the mouse over the animated object (for :hover) or keep the mouse button depressed (for :active).\r\n<h2>Interaction with Javascript<\/h2>\r\nUsing one tag (e.g., a button) to affect another (e.g., the car) requires use of the Javascript programming language. To add a \"Go\" button, the you would set the animation-play-state to paused, as with the :hover or :active pseudoclasses shown above. Then you would add a button that's programmed to set the animation-play-state to running:\r\n<pre>&lt;button&gt;Go!&lt;\/button&gt;\r\n<\/pre>\r\nNote in the Javascript the use of \"camel typography\" where capital letters separate the words. Also nested quotation marks alternate between double and single quotes to avoid confusing the browser.\r\n<h2>Reference Document<\/h2>\r\nAnimation: rich-adams.net &gt; GRC 365 &gt; Reference Documents &gt; <a href=\"https:\/\/r3adams.github.io\/grc365\/animation.html\" target=\"_blank\" rel=\"noopener\">Animation<\/a>","rendered":"<p>Animation has considerable power to catch readers&#8217; attention, to entertain, and to illustrate mechanisms involving motion. Designers must be careful not to over-distract readers with repeating or obtrusive animations.<\/p>\n<p>Three methods to animate with CSS styles include:<\/p>\n<ol>\n<li>CSS animation with no user interaction<\/li>\n<li>CSS animation with user interaction through the mouse-over (:hover) and click (:active) pseudoclasses<\/li>\n<li>CSS animation with JavaScript interaction<\/li>\n<\/ol>\n<h2>Requirements for CSS Animation<\/h2>\n<p>Animating a tag with CSS requires three components:<\/p>\n<ol>\n<li>an image or text object suitable for animating, often a transparent PNG or GIF like the car below<\/li>\n<li>a named @keyframes rule with major change points specified as percentages of completion<\/li>\n<li>a link in the tag&#8217;s CSS selector to the named @keyframes rule using the animation-name style<\/li>\n<li>an animation-duration style specifying the length of the animation<\/li>\n<\/ol>\n<figure id=\"attachment_155\" aria-describedby=\"caption-attachment-155\" style=\"width: 200px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/wp-content\/uploads\/sites\/449\/2026\/01\/car.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/wp-content\/uploads\/sites\/449\/2026\/01\/car-300x300.png\" alt=\"\" width=\"200\" height=\"200\" class=\"wp-image-155\" srcset=\"https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/wp-content\/uploads\/sites\/449\/2026\/01\/car-300x300.png 300w, https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/wp-content\/uploads\/sites\/449\/2026\/01\/car-150x150.png 150w, https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/wp-content\/uploads\/sites\/449\/2026\/01\/car-768x768.png 768w, https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/wp-content\/uploads\/sites\/449\/2026\/01\/car-65x65.png 65w, https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/wp-content\/uploads\/sites\/449\/2026\/01\/car-225x225.png 225w, https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/wp-content\/uploads\/sites\/449\/2026\/01\/car-350x350.png 350w, https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/wp-content\/uploads\/sites\/449\/2026\/01\/car.png 900w\" sizes=\"auto, (max-width: 200px) 100vw, 200px\" \/><\/a><figcaption id=\"caption-attachment-155\" class=\"wp-caption-text\">Car illustration courtesy of pngtree.com<\/figcaption><\/figure>\n<h2>Example<\/h2>\n<p>Assume we want to animate the above car by having it accelerate and drive across the screen.<\/p>\n<p>HTML for the car:<\/p>\n<pre>&lt;img id=\"car\" src=\"car.png\" \/&gt;<\/pre>\n<p>@keyframes rule for the car animation:<\/p>\n<pre>@keyframes letsgo {\r\n    0% {left: 0;}\r\n  100% {left: 120vw;} \/* vw (viewport widths) units &gt;100 position the tag off the page *\/\r\n}\r\n\r\n\r\n<\/pre>\n<p>CSS for the car (remember that \/* denotes comments that explain the style):<\/p>\n<pre>#car {\r\n    width: 200px;\r\n    position: relative; \/* position must be declared even though \"relative\" is the default *\/\r\n    left: 0;\r\n    animation-name: letsgo; \/* connects the car to the @keyframes rule *\/\r\n    animation-duration: 5s; \/* 5 seconds *\/\r\n    animation-fill-mode: forwards; \/* go to the end and stay there *\/\r\n    animation-timing-function: ease-in; \/* to accelerate to full speed *\/\r\n<\/pre>\n<h2>Interaction with :hover or :active<\/h2>\n<p>To introduce user interaction with the :hover (mouse over) or :active (click) pseudo classes, first the animation-play-state must be set to paused. This means the animation will not start by itself:<\/p>\n<pre>#car {\r\n    animation-play-state: paused;\r\n}\r\n<\/pre>\n<p>The :hover pseudoclass selects the tag when the mouse is over it, e.g., #car:hover. After selecting the car with #car:hover, you can set the animation-play-state to running:<\/p>\n<pre>#car:hover {\r\n    animation-play-state: running;\r\n}\r\n<\/pre>\n<p>For the animation to keep playing, users must keep the mouse over the animated object (for :hover) or keep the mouse button depressed (for :active).<\/p>\n<h2>Interaction with Javascript<\/h2>\n<p>Using one tag (e.g., a button) to affect another (e.g., the car) requires use of the Javascript programming language. To add a &#8220;Go&#8221; button, the you would set the animation-play-state to paused, as with the :hover or :active pseudoclasses shown above. Then you would add a button that&#8217;s programmed to set the animation-play-state to running:<\/p>\n<pre>&lt;button&gt;Go!&lt;\/button&gt;\r\n<\/pre>\n<p>Note in the Javascript the use of &#8220;camel typography&#8221; where capital letters separate the words. Also nested quotation marks alternate between double and single quotes to avoid confusing the browser.<\/p>\n<h2>Reference Document<\/h2>\n<p>Animation: rich-adams.net &gt; GRC 365 &gt; Reference Documents &gt; <a href=\"https:\/\/r3adams.github.io\/grc365\/animation.html\" target=\"_blank\" rel=\"noopener\">Animation<\/a><\/p>\n","protected":false},"author":6,"menu_order":10,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":[],"pb_section_license":""},"chapter-type":[],"contributor":[],"license":[],"class_list":["post-152","chapter","type-chapter","status-publish","hentry"],"part":3,"_links":{"self":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/wp-json\/pressbooks\/v2\/chapters\/152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/wp-json\/wp\/v2\/users\/6"}],"version-history":[{"count":10,"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/wp-json\/pressbooks\/v2\/chapters\/152\/revisions"}],"predecessor-version":[{"id":226,"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/wp-json\/pressbooks\/v2\/chapters\/152\/revisions\/226"}],"part":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/wp-json\/pressbooks\/v2\/chapters\/152\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/wp-json\/wp\/v2\/media?parent=152"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/wp-json\/pressbooks\/v2\/chapter-type?post=152"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/wp-json\/wp\/v2\/contributor?post=152"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesign2\/wp-json\/wp\/v2\/license?post=152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}