{"id":5667,"date":"2018-07-24T21:21:15","date_gmt":"2018-07-24T21:21:15","guid":{"rendered":"https:\/\/www.crimsondesigns.com\/blog\/?p=5667"},"modified":"2023-11-15T16:07:46","modified_gmt":"2023-11-15T16:07:46","slug":"subtle-animations-css-javascript","status":"publish","type":"post","link":"https:\/\/www.crimsondesigns.com\/blog\/subtle-animations-css-javascript\/","title":{"rendered":"Subtle Animations with CSS &#038; JavaScript: Intro"},"content":{"rendered":"<p>Are you curious about adding subtle animations to your website? Wondering if there are practical uses for them? Not sure if you know enough about CSS and JavaScript to pull it off? Then this beginner web design tutorial is for you.<br \/>\n<!--more--><\/p>\n<h2>Why add animations to your website?<\/h2>\n<p>Since the human eye is attracted to motion, animations can be the perfect tool to\u00a0draw attention to specific parts of a web page<i>.<\/i>\u00a0They can provide visual clues, giving visitors hints about actions you\u2019d like them to take. You can also add subtle effects to elements such as logos, icons and buttons or create visual storytelling elements with parallax scrolling.<\/p>\n<p>But, animations\u00a0in the wrong place or at the wrong speed can also annoy users and distract them from their goal. Too many JavaScript animations can also slow page load times. There is such a thing as <strong>&#8220;too much of a good thing.&#8221;<\/strong> So keep them subtle and add them sparingly.<\/p>\n<h3>How do you add subtle animations to a web page?<\/h3>\n<p>First things first. Lets talk about file linking. Most likely, you will link a web page to a separate CSS file and\/or a separate Javascript (JS) file. There are many tutorials where you can copy the CODE and paste the CSS into a <strong>.css<\/strong> file and JavaScript into a <strong>.js<\/strong> file and then link your web page to those files. You don\u2019t necessarily have to understand the code completely. But you do have to link the files correctly. It can be frustrating to read through a whole tutorial, follow the steps and then end up with an animation that just doesn\u2019t work.\u00a0(NOTE: It could also be your web browser. Be sure to check more than one.)<\/p>\n<h3>What about Page Speed?<\/h3>\n<p>Before you go ahead and add CSS and Javascript files to your website, you\u2019ll also want to keep <strong>CSS and Javascript optimization<\/strong> in the back of your mind. The more files you link to and the larger those files are, particularly Javascript files, the slower your web page will be. But there are ways to combine CSS files, combine JS files, minify them and link to them in efficient ways.<\/p>\n<p><b>READ:\u00a0<a href=\"https:\/\/www.sitepoint.com\/how-to-optimize-css-and-js-for-faster-sites\/\" target=\"_blank\" rel=\"noopener\">How to Optimize CSS and JS for Faster Sites.<\/a>\u00a0<\/b><\/p>\n<h3>Should you use CSS or JavaScript?<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"shadow alignright size-full wp-image-5766\" src=\"https:\/\/www.crimsondesigns.com\/blog\/wp-content\/uploads\/2018\/07\/css3-icon.gif\" alt=\"css3 icon\" width=\"187\" height=\"245\" \/>In the past, CSS was only used to change simple properties of elements in a web page such as color and size. But now that CSS3 has come along, we can utilize CSS <strong>transitions<\/strong> and <strong>animations<\/strong> in fun and practical ways.<\/p>\n<p>You can\u00a0add animated effects to icons such as &#8220;thumbs up&#8221; buttons or use them to show users the progress of an action they just took.<\/p>\n<p>The <strong><a href=\"http:\/\/www.justinaguilar.com\/animations\/\" target=\"_blank\" rel=\"noopener\">CSS3 Animation Cheat Sheet<\/a><\/strong> is a great resource with a set of plug-and-play animations. You can add the CSS style sheet to your website and apply the premade CSS rules to the HTML elements you want animated.<\/p>\n<h3>CSS Animations versus Javascript Animations<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"shadow alignright size-full wp-image-6005\" src=\"https:\/\/www.crimsondesigns.com\/blog\/wp-content\/uploads\/2018\/07\/javascript-icon.gif\" alt=\"JavaScript icon\" width=\"187\" height=\"236\" \/>With CSS3, you can do simple animations without any JavaScript at all. They are fast and lightweight. JavaScript can be used to control those CSS animations and make them even better. JavaScript can handle things that CSS can\u2019t such as when you need to stop, pause, slow down or reverse an animation. JavaScript often manipulates the CSS of your document in some way.<\/p>\n<p>They can work together as a team.<\/p>\n<p>Javascript is especially useful for scroll-based animations where content sections gradually appear as a visitor scrolls down a web page. \u00a0CSS animations will automatically start without you doing anything, but if you want to wait until the visitor SCROLLS DOWN a web page and reaches a certain section, you will need to add some JavaScript.\u00a0A JavaScript component like <strong><a href=\"https:\/\/scrollrevealjs.org\/\" target=\"_blank\" rel=\"noopener\">ScrollReveal.js<\/a><\/strong> can be applied to specific containers on a web page so only those elements animate.<\/p>\n<h3>The Nitty-Gritty Details of CSS Animations<\/h3>\n<p>You can add a CSS style sheet to your website, link to that stylesheet and apply a CSS rule to the HTML elements you want animated.<\/p>\n<p><strong>But how?<\/strong><\/p>\n<p class=\"special\">You must first understand HTML elements\u00a0and CSS rules.<\/p>\n<h4>Already comfortable with HTML elements and CSS rules?<\/h4>\n<p>Skip ahead to <strong><a href=\"https:\/\/www.crimsondesigns.com\/blog\/subtle-animations-css-javascript-part-2\">PART II: Beginning CSS3 Animations.<\/a><\/strong><\/p>\n<hr \/>\n<h4>HTML Elements in a Nutshell<\/h4>\n<p>Examples of HTML elements: \u00a0div, h1, p<br \/>\nExamples of elements as tags: <strong>&lt;div&gt;, &lt;h1&gt;, &lt;p&gt;<\/strong><br \/>\nAn element becomes a tag when we use the angle brackets around it.<\/p>\n<p>The &lt;<b>div<\/b>&gt; tag is a container unit that often surrounds other page elements and divides the <b>HTML<\/b> document into sections. Web designers can apply CSS styles to &lt;<b>div<\/b>&gt; elements. You can animate a <strong>&lt;div&gt;<\/strong>\u00a0and make it fade in or move into the page from the left or right. You can do the same things with a <strong>&lt;p&gt; <\/strong>(paragraph).<\/p>\n<p>But, you do not want to have ALL of your <strong>&lt;div&gt;<\/strong> and <strong>&lt;p&gt;<\/strong> elements fade in. Let&#8217;s just choose one. We&#8217;ll give it a class called <span class=\"special\">.fadeInRight<\/span> to make it special.<\/p>\n<p>In our HTML document, we will write: <strong>&lt;div class=&#8221;fadeInRight&#8221;&gt;<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6007 size-full\" src=\"https:\/\/www.crimsondesigns.com\/blog\/wp-content\/uploads\/2018\/07\/HTML-class-selector-example.gif\" alt=\"HTML class selector example\" width=\"350\" height=\"59\" \/><\/p>\n<p>NOTE:\u00a0The <b>class<\/b> attribute specifies a class name for an element. The <b>class<\/b> attribute is mostly used to point to a <b>class<\/b> in a CSS style sheet. It can also be used by a JavaScript\u00a0to make changes to <b>HTML<\/b> elements with a specified <b>class<\/b>.<\/p>\n<p>In our CSS style sheet, we will write:<\/p>\n<div class=\"highlight\">\n<pre>.fadeInRight {\n...you can insert code here to animate this div...\n}\n<\/pre>\n<\/div>\n<p>(Now only the &lt;div&gt; element you gave the class\u00a0<strong>.fadeInRight<\/strong> will be animated.)<\/p>\n<p>Next, we will connect our HTML file to our CSS style sheet with a <strong>&lt;link&gt;<\/strong> tag in the <strong>&lt;head&gt;<\/strong> section of the web page.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6037\" src=\"https:\/\/www.crimsondesigns.com\/blog\/wp-content\/uploads\/2018\/07\/HTML-link-tag.png\" alt=\"HTML link tag\" width=\"300\" height=\"72\" \/><\/p>\n<p><strong>EXAMPLES:<\/strong><\/p>\n<p><strong> #1: <\/strong>&lt;link rel=&#8221;stylesheet&#8221; type=&#8221;text\/css&#8221; href=&#8221;<strong>style.css<\/strong>&#8220;&gt;<\/p>\n<p><strong>#2: <\/strong>&lt;link rel=&#8221;stylesheet&#8221; type=&#8221;text\/css&#8221; href=&#8221;<strong>css\/style.css<\/strong>&#8220;&gt;<\/p>\n<p>In the first example, our CSS style sheet is called &#8220;<strong>style.css<\/strong>&#8221; and is placed at the same level in our file structure as our web page.\u00a0In the second example, the css style sheet is placed in a folder called &#8220;<strong>css<\/strong>&#8220;.<\/p>\n<h3>CSS Style Sheets in a Nutshell<\/h3>\n<ul>\n<li>A CSS style sheet has rules that apply to an HTML document.<\/li>\n<li>CSS files usually end with the <span class=\"code\">.css<\/span> extension.<\/li>\n<li>CSS documents contain nothing but CSS.<\/li>\n<li>Use a <strong><span class=\"code\">&lt;link&gt;<\/span><\/strong> tag in your HTML document to specify which CSS document will be used by the HTML page.<\/li>\n<li>The <strong><span class=\"code\">&lt;link&gt;<\/span><\/strong> tag only occurs in the <strong>&lt;head&gt;<\/strong> section of an HTML document.<\/li>\n<li>The <strong><span class=\"code\">&lt;link&gt;<\/span><\/strong> tag has an <span class=\"code\">href<\/span> attribute, which describes the location of the the CSS stylesheet.<\/li>\n<\/ul>\n<h3><strong>CSS Rules in a Nutshell<\/strong><\/h3>\n<p>You need to have a pretty good grasp of CSS rules in order to have a basic understanding of CSS animations. So let&#8217;s review them quickly.<\/p>\n<ul>\n<li>A <b>CSS<\/b> rule-set consists of a <b>selector<\/b> and a declaration block.<\/li>\n<li>IMPORTANT: The <b>selector<\/b> points to the <b>HTML element<\/b> you want to style.<\/li>\n<\/ul>\n<h4>Anatomy of a Rule<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6041\" src=\"https:\/\/www.crimsondesigns.com\/blog\/wp-content\/uploads\/2018\/07\/css-rule-with-selector.png\" alt=\"CSS rule with selector\" width=\"395\" height=\"130\" srcset=\"https:\/\/www.crimsondesigns.com\/blog\/wp-content\/uploads\/2018\/07\/css-rule-with-selector.png 395w, https:\/\/www.crimsondesigns.com\/blog\/wp-content\/uploads\/2018\/07\/css-rule-with-selector-300x99.png 300w\" sizes=\"auto, (max-width: 395px) 100vw, 395px\" \/><\/p>\n<p>CSS rules have selectors and declarations.<\/p>\n<p>REMINDER: The selector specifies what elements (in the related HTML document) are affected by the declaration. The selector in the example shown here is a &#8220;p.&#8221; (If a CSS style sheet with this CSS rule is linked to an HTML document, ALL the paragraphs in the HTML document will be blue.)<\/p>\n<h4>CSS Syntax<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6013 size-full\" src=\"https:\/\/www.crimsondesigns.com\/blog\/wp-content\/uploads\/2018\/07\/CSS-syntax.gif\" alt=\"CSS syntax: selector {property: value}\" width=\"360\" height=\"61\" \/><\/p>\n<p>We can tidy up a style sheet by grouping declarations that relate to the same selector into a list (separated by semicolons).<\/p>\n<p><strong>div {border-width: 1px; border-style: solid; border-color: red;}<\/strong><\/p>\n<p>Sometimes, we can use SHORTHAND properties to simplify the code.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6043\" src=\"https:\/\/www.crimsondesigns.com\/blog\/wp-content\/uploads\/2018\/07\/CSS-shorthand-properties-precise-code.gif\" alt=\"CSS shorthand properties make CSS code precise.\" width=\"500\" height=\"83\" \/><\/p>\n<p>Let&#8217;s simplify our div.<\/p>\n<p><strong>div {border: 1px solid red;}<\/strong><\/p>\n<p class=\"special\">NOTE: You&#8217;ll see SHORTHAND properties used in CSS3 animations.<\/p>\n<p>Let&#8217;s move on to PART II where we&#8217;ll see some DEMOS.<\/p>\n<p><a class=\"button\" href=\"https:\/\/www.crimsondesigns.com\/blog\/subtle-animations-css-javascript-part-2\">PART II: Beginning CSS3 Animation<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Are you curious about adding subtle animations to your website? Not sure if you know enough about CSS and JavaScript to pull it off? Then this beginner web design tutorial is for you.<\/p>\n","protected":false},"author":1,"featured_media":5994,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,9],"tags":[304,303,50,115,302],"class_list":{"0":"post-5667","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-html-and-css","8":"category-web-design","9":"tag-css-animation","10":"tag-css-syntax","11":"tag-css3","12":"tag-javascript","13":"tag-keyframes","14":"cat-5-id","15":"cat-9-id","16":"has_thumb"},"_links":{"self":[{"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/posts\/5667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=5667"}],"version-history":[{"count":3,"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/posts\/5667\/revisions"}],"predecessor-version":[{"id":7725,"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/posts\/5667\/revisions\/7725"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/media\/5994"}],"wp:attachment":[{"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=5667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=5667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=5667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}