In this tutorial, you’ll learn how to create a fun and interactive eye-tracking animation using HTML, CSS, and a touch of creativity. This project features two buttons designed to resemble eyes, complete with pupils that dynamically follow the cursor’s movement. Surrounding the buttons are invisible sensors that detect the mouse’s direction, causing the pupils to shift accordingly. Additionally, the project incorporates hover and click animations, where the “eyes” blink and respond to user interactions, making the experience more engaging and lifelike.
Explanation:
HTML Structure:
The HTML structure forms the backbone of the project:
- The
<body>contains a<div>with a class ofwrapper, which serves as the container for all elements. - The
wrapperincludes eight directional sensors (<div>elements with the classbtn-sensor) positioned around the buttons. Each sensor uses a custom property--ato define its angular position (e.g., 0°, 45°, 90°). These sensors detect hover events, guiding the pupil movements. - The two buttons styled as “eyes” are
<button>elements with a classbtn-button. Inside each button are two nested<div>elements:btn-lid: Represents the eyelid and enables blinking animations.btn-pupil: Represents the pupil that moves dynamically based on cursor interactions.
This structure ensures semantic, organized, and easy-to-maintain HTML code. By logically grouping elements (sensors, buttons, and nested components), the layout achieves both functionality and visual appeal.
CSS Styling and Animations:
The CSS code transforms the static HTML structure into an interactive animation with styling and animations:
- Global Reset:
- A universal selector (
*) resets padding and margins for consistent cross-browser behavior. box-sizing: border-boxsimplifies size calculations.
- A universal selector (
- Centering and Layout:
- The
htmlandbodyelements usedisplay: gridandplace-items: centerto center the content. overflow: hiddenprevents scrolling, whilebackground-color: #212121provides a dark contrast for the eyes.
- The
- Buttons as Eyes:
- The
btn-buttonclass styles the buttons to look like eyes withborder-radius: 10rem, white backgrounds, and solid borders. - The
btn-pupilclass shapes the pupils as black circles with light blue borders. Hover effects apply dynamic transformations to simulate eye movements.
- The
- Sensors for Eye Tracking:
- The
btn-sensorelements act as invisible zones, positioned usingclip-path: polygonto form triangular sections. Hovering over these sections triggers the pupils to move in the respective directions.
- The
- Animations:
- Blinking: The
@keyframes eye-lidanimation changes the background ofbtn-lidto simulate natural blinking. - Random Eye Movement: The
@keyframes pupilanimation creates slight, random pupil shifts, mimicking natural eye motion.
- Blinking: The
This combination of CSS techniques ensures smooth transitions and responsive animations, providing an interactive experience for users.
Conclusion:
This eye-tracking animation project is a perfect example of combining HTML and CSS to create an interactive and visually engaging user experience. The clean HTML structure and advanced CSS styling allow seamless integration of hover and click interactions, simulating natural eye movements and blinks. By understanding and implementing this tutorial, you can explore new possibilities for interactive web elements, enhancing user engagement on your projects.
With this foundational knowledge, you can further expand the project by introducing features like adjusting the eye’s sensitivity, adding multiple pairs of eyes, or even integrating JavaScript for enhanced interactivity. The potential for creativity is limitless!

