Back

Enhancing User Interaction with Stylish Avatar Hover Effects

A step-by-step tutorial on creating engaging avatars with responsive hover effects and informative overlays using only HTML and CSS.

HTML

In this project, we're going to create a stylish avatar with an interactive hover effect that displays additional information about the user. We'll use a combination of HTML and CSS to achieve this smooth and responsive design. Let's start by setting up the basic HTML structure. We'll have a container that will hold our avatar and the overlay.
        <div class="avatar-container">
          <div class="avatar-wrapper">
            <img src="https://randomuser.me/api/portraits/women/44.jpg" alt="User Avatar" class="avatar">
            <div class="overlay">
              <h3>Jane Doe</h3>
              <p>Web Developer</p>
              <a href="#" class="view-profile">View Profile</a>
            </div>
          </div>
        </div>
      
We have a main container called avatar-container that centers the avatar on the screen. Inside, there's a avatar-wrapper that houses the image and the overlay with the user's information.

CSS

Now that our HTML is structured, we'll focus on the styling part. The aim is to create a circular avatar that enlarges when hovered over, and reveals the overlay information smoothly. The avatar-container is a flexbox container that ensures the avatar is centered both vertically and horizontally in the viewport, providing a clean and focused design.
        .avatar-container {
          display: flex;
          justify-content: center;
          align-items: center;
          height: 100vh;
          background-color: #f4f4f9;
        }
      
In the avatar-wrapper, the image size is constrained to 150x150 pixels, perfectly rounding the corners using the border-radius property, achieving a circular shape. Hovering over this element results in a slight transform: scale(1.1), which provides a zoom-in effect adding a sense of depth.
        .avatar-wrapper {
          position: relative;
          width: 150px;
          height: 150px;
          overflow: hidden;
          border-radius: 50%;
          box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
          transition: transform 0.3s ease-in-out;
        }

        .avatar-wrapper:hover {
          transform: scale(1.1);
        }
      
Our avatar image is styled to cover the entire content area, maintaining its aspect ratio with the object-fit: cover property.
        .avatar {
          width: 100%;
          height: 100%;
          object-fit: cover;
          transition: opacity 0.3s ease-in-out;
        }
      
The overlay is totally transparent by default using opacity: 0, placed absolutely over the avatar. It becomes visible on hover, displaying the user's details: name, profession, and a button to view the profile.
        .overlay {
          position: absolute;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          background-color: rgba(0, 0, 0, 0.7);
          color: white;
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          opacity: 0;
          transition: opacity 0.3s ease-in-out;
        }

        .avatar-wrapper:hover .overlay {
          opacity: 1;
        }
      
The overlay h3 and p elements are styled for clear visibility, centered within the overlay. The view-profile link includes a smooth color transition to enhance interactivity.
        .overlay h3 {
          margin: 0;
          font-size: 1.2em;
        }

        .overlay p {
          margin: 5px 0;
          font-size: 0.9em;
        }

        .view-profile {
          text-decoration: none;
          color: white;
          border: 1px solid #fff;
          padding: 5px 10px;
          border-radius: 4px;
          transition: background-color 0.3s, color 0.3s;
        }

        .view-profile:hover {
          background-color: #fff;
          color: black;
        }
      
This tutorial offers a simple yet powerful design technique, focusing on blending modern aesthetics with functional interactivity using only HTML and CSS. By understanding and customizing these elements, you can create visually striking components that enhance the user experience on any website.

Whole code

<div class="avatar-container">
  <div class="avatar-wrapper">
    <img src="https://randomuser.me/api/portraits/women/44.jpg" alt="User Avatar" class="avatar">
    <div class="overlay">
      <h3>Jane Doe</h3>
      <p>Web Developer</p>
      <a href="#" class="view-profile">View Profile</a>
    </div>
  </div>
</div>


<style>
.avatar-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f4f4f9;
}

.avatar-wrapper {
  position: relative;
  width: 150px;
  height: 150px;
  overflow: hidden;
  border-radius: 50%;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
  transition: transform 0.3s ease-in-out;
}

.avatar-wrapper:hover {
  transform: scale(1.1);
}

.avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: opacity 0.3s ease-in-out;
}

.overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.7);
  color: white;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  opacity: 0;
  transition: opacity 0.3s ease-in-out;
}

.avatar-wrapper:hover .overlay {
  opacity: 1;
}

.overlay h3 {
  margin: 0;
  font-size: 1.2em;
}

.overlay p {
  margin: 5px 0;
  font-size: 0.9em;
}

.view-profile {
  text-decoration: none;
  color: white;
  border: 1px solid #fff;
  padding: 5px 10px;
  border-radius: 4px;
  transition: background-color 0.3s, color 0.3s;
}

.view-profile:hover {
  background-color: #fff;
  color: black;
}
</style>
      
Thank you for reading this article.

Comments

No comments yet. Be the first to comment!

More avatars

Similar

See also