Teach Tito to Roll Over!
You can make your website more interactive by making cool stuff happen when you hover over things with the mouse cursor!
- Find your CSS rules for the
img
elements, or create some if you don’t have any. Add in a border, and then add a new block of rules right below:
img {
border: 2px solid White;
}
img:hover {
border: 2px dashed Navy;
}
You’ve just used a special type of CSS block called a pseudo-class.
How does it work?
A pseudo-class is a bit different from a class that you create yourself. You can recognise it by the :
.
Pseudo-classes come built in to HTML elements: you can add :hover
style rules to any element, class, or id
selector in your style sheet without needing to add anything extra in your HTML code.
-
What do you think will happen? Check what pages on your website have pictures on them (add a picture if there aren’t any!), then move your cursor over a picture to find out!
-
Let’s use this new
:hover
pseudo-class together with a CSS class to make links glow when you hover over them! Add a link to your web page and include an attribute to specify the class name. Remember, links are defined using the<a>
tag, like so:
<p>
Visit the <a class="niceLinks" href="https://en.wikipedia.org/wiki/Ireland">Wikipedia page</a> to learn even more about Ireland!
</p>
- Add the following code to your style sheet, then run your code to see your lovely links in action.
.niceLinks {
text-decoration: none;
color: #FFFAF0;
}
.niceLinks:hover {
color: #00FF7F;
}
- Why not add the attribute
class="niceLinks"
to all of the links in your menu bar as well?
You can combine all of these tricks with animations too!
- Find the CSS block for the picture of Tito again (or whatever picture you were working on earlier). Add the following code to your style sheet file:
#titoPicture {
border-radius: 100%;
width: 100px;
}
#titoPicture:hover {
animation-name: rollOver;
animation-duration: 1s;
animation-iteration-count: 1;
}
@keyframes rollOver {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(-360deg);
}
}
-
Can you guess what will happen?
-
Now shout “Roll over!” as you move the cursor over the picture!
Challenge: make glowing rainbow links
- Can you use the
rainbowGlow
animation from the previous card to make the links in your menu keep changing colours when the cursor is hovering over them?