Signed in as
We're going to be introducing the idea of pseudo-classes. While they should get the chance to enjoy animating elements on their pages, help them to realize the importance of subtlety 😉
This should be a nice reprieve for students after the higher-order concepts in creating layouts. Let them enjoy the fun of animating their UI a bit!
Have students go back through those sites (one last time!) and look for examples of animations on the page whenever they interact with certain elements. Ask, "Are there patterns that have emerged? What is commonly animated? What happens when an animation occurs?" A difficult part of helping students make the connection for using animations is getting them to understand what is actually being animated (such as the properties of the element). Once they make this link, they can typically abstract the concept and run with it.
You know the drill at this point...guide them through the lesson.
Take the formative assessment at the end of class. Work towards mastery.
CSS also gives us the ability to animate our elements. Especially when used in conjunction with JavaScript, which we'll learn more about in our next module, you can create some pretty powerful, lightweight interactions on your site. We'll cover two concepts in this lesson: state and animations.
State is a common term in computer science. Typically, it's a reference to the values of an object; in CSS, state refers to the contexts by which a certain element can be described. We write rules for states using something called a pseudo-class selector. They're always denoted by a colon as we can see below:
1/* The rules for the button */ 2.actionButton { 3 border: solid 1px black; 4 padding: 10px 20px; 5 border-radius: 9px; 6 cursor: pointer; 7} 8 9/* The rules that change on a user's hover */ 10.actionButton :hover { 11 border: solid 1px tomato; 12 color: tomato; 13}
Here, we can see the button's text and border colors change when its state changes. This change will be immediate.
We can also affect the way in which the state-change animations occur. For example, let's shift the button up a bit on a user's hover but do so a bit more smoothly:
1.actionButton { 2 border: solid 1px black; 3 padding: 10px 20px; 4 border-radius: 9px; 5 cursor: pointer; 6 transition: transform 0.2s; 7} 8 9/* The rules that change on a user's hover */ 10.actionButton :hover { 11 border: solid 1px tomato; 12 color: tomato; 13 transform: translateY(-10px); 14}
In our button's initial style rules, we've added a transition
property. The value is that on any transform
, the duration should be 0.2 seconds instead of instant. This way, our button will glide up smoothly and come back down at the same rate.
We can also combine transformations and make more dynamic interactions.
1.actionButton { 2 border: solid 1px black; 3 padding: 10px 20px; 4 border-radius: 9px; 5 cursor: pointer; 6 transition: transform 0.2s; 7} 8 9/* The rules that change on a user's hover */ 10.actionButton :hover { 11 border: solid 1px tomato; 12 color: tomato; 13 transform: translateY(-10px) rotateZ(-2.5deg) scale(1.03); 14}
Client site: States