Build a Simple Personal Portfolio Layout
January 15, 2025
Learn how to style a personal portfolio using basic CSS properties like background colors, fonts, and spacing. Focus on layout fundamentals such as margins, padding, and text alignment.
In this project, we'll explore the power of CSS animations by designing interactive button hover effects. We'll focus on using keyframes, transitions, and animation timing functions to create engaging and visually appealing hover effects.
Here's what we'll cover in this tutorial:
By the end of this project, you'll have a set of interactive buttons with various hover effects. Let's get started by setting up our project files!
Before we start coding, let's set up a workspace for our project:
Think of this folder like a container where we'll keep all our project files - just like having a special drawer for your art supplies!
A text editor is like your crafting tool - it's where we'll write our code. We recommend Visual Studio Code because it's free and beginner-friendly!
We need two special files - think of them as two pieces of paper where we'll write different things:
To create these files:
🎯 Success Check: At this point, you should have:
👉 Need Help? If something's not working, try closing VS Code and opening it again, or create the files using the "File → New File" menu.
Before we dive into CSS, let's set up the HTML structure for our buttons:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Button Hover Effects</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<button class="btn btn-1">Button 1</button>
<button class="btn btn-2">Button 2</button>
<button class="btn btn-3">Button 3</button>
<button class="btn btn-4">Button 4</button>
</div>
</body>
</html>
Let's understand the HTML structure:
<div class="container">
: Represents the container for the buttons. <button class="btn btn-1">
: Represents a button with a specific class for styling.Save your "index.html" file. Now we have the basic HTML structure in place. In the next step, we'll start applying CSS styles to our buttons.
Let's start by styling the body and container of our HTML document to ensure a consistent and clean layout.
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
box-sizing: border-box;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
display: flex;
flex-direction: column;
gap: 20px;
padding: 20px;
}
Let's understand the CSS rules we just added:
font-family: Arial, sans-serif;
: Sets the default font for the entire page.margin: 0;
: Removes the default margin from the body.padding: 0;
: Removes the default padding from the body.box-sizing: border-box;
: Ensures that the padding and border are included in the element's total width and height.background-color: #f4f4f4;
: Sets the background color of the body to a light gray.display: flex;
: Uses flexbox to center the content.justify-content: center;
: Horizontally centers the content.align-items: center;
: Vertically centers the content.height: 100vh;
: Sets the height of the body to the full viewport height..container
: Styles the container to display the buttons in a column with a gap between them and adds padding for spacing.Save your "styles.css" file. Now, if you open "index.html" in a web browser, you'll see the basic layout applied to your page.
In the next step, we'll style the buttons.
Now, let's style the buttons to give them a clean and modern look.
.btn {
background-color: #007bff;
color: #fff;
border: none;
padding: 15px 30px;
font-size: 1rem;
border-radius: 5px;
cursor: pointer;
transition: all 0.3s ease;
position: relative;
overflow: hidden;
user-select: none;
}
.btn:hover {
background-color: #0056b3;
}
Let's understand the CSS rules we just added:
.btn
: Styles the buttons with a blue background, white text, no border, padding, font size, border-radius, cursor, and transition for smooth changes..btn:hover
: Changes the background color of the button on hover.position: relative;
: Ensures the button can contain absolute elements for more complex effects.overflow: hidden;
: Ensures any content that overflows the button is hidden, which is useful for effects that involve elements moving in and out of the button.user-select: none;
: Prevents text selection on the button.Save your "styles.css" file. Now, if you open "index.html" in a web browser, you'll see the styled buttons.
In the next step, we'll create and apply CSS animations for hover effects.
Now, let's create and apply CSS animations for the button hover effects.
/* Button 1: Scale Effect */
.btn-1::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(255, 255, 255, 0.3);
transform: scale(0);
transition: transform 0.3s ease;
z-index: -1;
}
.btn-1:hover::before {
transform: scale(1);
}
/* Button 2: Slide In Effect */
.btn-2::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(255, 255, 255, 0.3);
transform: translateX(-100%);
transition: transform 0.3s ease;
z-index: -1;
}
.btn-2:hover::before {
transform: translateX(0);
}
/* Button 3: Rotate and Scale Effect */
.btn-3::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(255, 255, 255, 0.3);
transform: rotate(45deg) scale(0);
transition: transform 0.3s ease;
z-index: -1;
}
.btn-3:hover::before {
transform: rotate(45deg) scale(1);
}
/* Button 4: Background Color Change with Animation */
@keyframes colorChange {
0% {
background-color: #007bff;
}
50% {
background-color: #0056b3;
}
100% {
background-color: #003d80;
}
}
.btn-4 {
animation: colorChange 2s infinite;
}
.btn-4:hover {
animation-play-state: paused;
}
Let's understand the CSS rules we just added:
.btn-1::before
: Creates a pseudo-element for a scale effect on hover..btn-1:hover::before
: Scales the pseudo-element to full size on hover..btn-2::before
: Creates a pseudo-element for a slide in effect on hover..btn-2:hover::before
: Slides the pseudo-element into view on hover..btn-3::before
: Creates a pseudo-element for a rotate and scale effect on hover..btn-3:hover::before
: Rotates and scales the pseudo-element on hover.@keyframes colorChange
: Defines a keyframe animation for a background color change..btn-4
: Applies the keyframe animation to the button..btn-4:hover
: Pauses the animation on hover.Save your "styles.css" file. Now, if you open "index.html" in a web browser, you'll see the animated button hover effects.
In the next step, we'll add final touches to the button hover effects.
Let's add the final touches to our button hover effects to ensure they are fully functional and visually appealing.
.btn-1, .btn-2, .btn-3, .btn-4 {
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
transition: box-shadow 0.3s ease;
}
.btn-1:hover, .btn-2:hover, .btn-3:hover, .btn-4:hover {
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
}
Let's understand the CSS rules we just added:
.btn-1, .btn-2, .btn-3, .btn-4
: Adds a subtle shadow to the buttons..btn-1:hover, .btn-2:hover, .btn-3:hover, .btn-4:hover
: Increases the shadow on hover to create a more pronounced effect.Save your "styles.css" file. Now, if you open "index.html" in a web browser, you'll see the final touches in action.
Congratulations! You've completed the CSS project, "Create a CSS Animation for Button Hover Effects." You've learned how to design interactive button hover effects using CSS animations, keyframes, transitions, and animation timing functions.
Feel free to continue customizing your button hover effects and exploring more CSS techniques. Happy coding! 🚀
January 15, 2025
Learn how to style a personal portfolio using basic CSS properties like background colors, fonts, and spacing. Focus on layout fundamentals such as margins, padding, and text alignment.
January 5, 2025
Style a simple blog post using CSS. Learn how to apply font families, text colors, and adjust line height and spacing to enhance readability.
January 17, 2025
Build a responsive navigation menu that works across devices using media queries. This project focuses on layout design and adapting styles for smaller screens.
January 16, 2025
Create a fully responsive pricing table with CSS grid. Learn to use grid properties to structure rows and columns, adding hover effects for interaction.
January 3, 2025
Learn how to create a parallax scrolling effect using CSS. This project covers how to manipulate background images and layers for a visually engaging web experience.