Back
A step-by-step tutorial on creating engaging avatars with responsive hover effects and informative overlays using only HTML and CSS.
<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.
.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.
<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!