The link at the top of this page is a plain-jane, run-of-the-mill, vanilla, nothing-special link. But it can be so much more! Let's turn it into something to write home about!
To get started, let's use a bit of positioning to keep that link visible on the page where we can see it as we continue working through this exercise.
<header> element, with the following rules:position: fixed;top: 0;left: 0;width: 100%;height: 200px;background: #000;This creates a fixed page header, but it's covering our page content. We can fix that.
<body> element, and set padding-top to be the same as our <header> height.Now let's center the link in our page header. Instead of using padding or margin to push the link around, we'll use absolute positioning. This link is not going to be styled like a standard link, so it's best if we give it a class rather than just selecting by tag name.
class="launch-button" to the <a> element..launch-button with the following styles:position: absolute;left: 50%;top: 50%;The link is now in the middle of the header... sort of. It's actually a bit low and to the right because we've positioned its top-left corner at the center. To truly center the link, we need to offset it by half its own width and height. We'll use the transform property to achieve this.
.launch-button selector:transform: translate(-50%, -50%);The link is now exactly centered, even though we don't know its exact dimensions. This technique is useful for centering an absolutely positioned element within its container.
This approach might seem counterintuitive because it looks like we're moving the link left by 50% and then back right by 50%. The key is understanding that the percentages in left and top are relative to the parent container, while the percentages in transform are relative to the element itself.
Time to style the link to look more like a button.
Let's remove some default link styles and add button styles.
.launch-button ruleset, add:text-decoration: none;background-color: #aa0000;color: white;font-size: 20px;padding: 15px 30px;border-radius: 10px;To enhance the interactive feel, we'll add :hover and :active states.
.launch-button:hover and add:background-color: #cc0000;box-shadow: 0 0 8px #ff0000;.launch-button:active and add:background-color: #750000;box-shadow: 0 0 2px #750000;The box-shadow property adds a glowing effect. You can read more about it in the documentation or use a box-shadow generator.
We'll use Font Awesome to add icons and the transform property to animate them. The Font Awesome library is already included in the <head> section.
<a class="launch-button"> element, right after the word "Launch" but before the closing </a> tag, add:
<div class="rocket">
<i class="fas fa-burn"></i>
<i class="fas fa-rocket"></i>
</div>
The <i> tag is used by Font Awesome to display icons. We've added two icons: "burn" and "rocket."
Let's position these icons appropriately.
.launch-button .rocket and set:position: absolute;.launch-button .rocket i and set:position: absolute;top: 0;left: 0;Now, use transform to arrange them:
.launch-button .fa-rocket and set:
transform: translate(-50%, -50%) rotate(-45deg);.launch-button .fa-burn and set:
transform: translate(-50%, -15%) rotate(180deg) scale(0.3);color: orange;Adjust the padding of the button to make room for the rocket:
.launch-button ruleset, change padding to 15px 60px 15px 30px;..launch-button .rocket, add:top: 50%;right: 40px;Add interactive effects to the flame:
.launch-button .fa-burn, change scale(0.3) to scale(0.1)..launch-button:hover .fa-burn and set:
transform: translate(-50%, -15%) rotate(180deg) scale(0.4);.launch-button:active .fa-burn and set:
transform: translate(-50%, 25%) rotate(180deg) scale(0.8);Make the rocket grow on hover:
.launch-button:hover .rocket and add:
transform: scale(1.3);Let's create a background and animate the rocket on click.
<header> selector, replace background with:
background: linear-gradient(to bottom, #000000 40%, #0560aa 80%, #7cd4ef 100%);.launch-button, change top to 75%;.launch-button:active, add:
transform: translate(-50%, -20%) scale(0.7);padding: 15px 30px;.launch-button:active .rocket and add:
transform: rotate(-15deg) translate(60px, -140px) scale(3.5);Enhance the button label:
<span class="label"> tag..launch-button .label and add:
letter-spacing: 0.1em;text-transform: uppercase;color: white;.launch-button:hover .label and add:
color: orange;text-shadow: 0 0 3px rgba(251, 255, 0, 0.5);We'll use CSS transitions to smooth out the animations.
.launch-button, add:
transition: transform 0.5s, background-color 0.1s, padding 0.5s, box-shadow 0.1s;.launch-button .rocket, add:
transition: transform 0.5s;.launch-button .label, add:
transition: color 0.2s, text-shadow 0.2s;.launch-button .fa-burn, add:
transition: transform 0.4s;Finally, let's create a continuous animation for the flame during the rocket launch.
@keyframes burn {
from { transform: rotate(-7deg); }
33% { transform: rotate(4deg); }
66% { transform: rotate(-4deg); }
to { transform: rotate(7deg); }
}
.launch-button:active .fa-burn::before and add:
display: block;transform-origin: center bottom;animation-name: burn;animation-duration: 0.1s;animation-iteration-count: infinite;animation-direction: alternate;Now, when you press the button, the flame will have a flickering effect.
Our mission is complete. In the process of styling this button, we've introduced linear gradients, box shadows, text shadows, transforms, transitions, and animations. Each of these has tremendous capability and flexibility, and you should treat this as only a single example of how and where they might be applied. While we used them for a button, there's nothing that says they can't be used elsewhere and in infinite combinations. Explore!