Create a cohesive user experience by implementing the same CSS animation when hovering over two distinct elements

For a learning exercise, I attempted to create an animation. My goal was to have the circle trigger an animation when hovered over, while also revealing the text in the .hide element. However, as soon as I hover over the circle and then move my mouse onto the text, both animations stop.

Is there a way to maintain both animations even if I hover over the text within the .hide element? I've experimented with creating a separate :hover subclass for the .hide class with animations, added the animation directly to the .hide class, and tried combining the two approaches, but I haven't been successful.

Additionally, that small black line that appears at the beginning of the hover is quite bothersome. If anyone knows how to remove it, I would greatly appreciate the help.

.container {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.circle-icon {
  background: gray;
  border-radius: 50%;
  padding: 15px;
  color: white;
  transition: padding 1s;
  margin: 0px;
}

.circle-icon:hover {
  padding: 30px;
  animation-name: spin;
  animation-duration: 1s;
  animation-iteration-count: 2;
  animation-timing-function: ease-in-out;
  animation-direction: alternate;
}

.hide {
  position: relative;
  left: -15px;
  display: none;
  line-height: 40px;
  height: 40px;
  width: 100px;
  text-align: center;
  border-radius: 0 50px 50px 0;
  margin: 0px;
  vertical-align: middle;
  color: white;
  animation-name: slide;
  animation-duration: 1s;
  animation-iteration-count: 1;
  animation-timing-function: ease-in-out;
  animation-direction: forward;
}

.circle-icon:hover + .hide {
  display: inline-block;
  background-color: gray;
  width: 100px;
}

@keyframes spin {
  0% {
    rotate: 0deg;
  }
  100% {
    rotate: 360deg;
  }
}

@keyframes slide {
  0% {
    width: 0px;
    font-size: 0%;
  }
  100% {
    width: 100px;
    font-size: 100%;
  }
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Home</title>
    <link rel="stylesheet" href="style.css" />
    <script
      src="https://kit.fontawesome.com/7dd2bd858f.js"
      crossorigin="anonymous"
    ></script>
  </head>
  <body>
    <div class="container">
      <a href="#">
        <i class="fas fa-home circle-icon"></i>
        <span class="hide">HOME</span>
      </a>
    </div>
  </body>
</html>

Answer №1

To address the issues you're experiencing, I have provided explanations for each necessary change following the example. In essence:

The primary problem lies in the fact that you are triggering the hover effect solely on the circle-icon. This results in the hover effect ending once you move the cursor away from it, even if it's towards the associated text. To rectify this, you need to trigger the hover effect by hovering over the entire link, not just the circle.

Functional Example:

.container {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.circle-icon {
  background: gray;
  border-radius: 50%;
  padding: 15px;
  color: white;
  transition: padding 1s;
  margin: 0px;
}

a.icontextlink { text-decoration:none;}

.icontextlink:hover .circle-icon {
  padding: 30px;
  animation-name: spin;
  animation-duration: 1s;
  animation-iteration-count: 2;
  animation-timing-function: ease-in-out;
  animation-direction: alternate;
}

.hide {
  position: relative;
  left: -15px;
  display: none;
  line-height: 40px;
  height: 40px;
  width: 100px;
  text-align: center;
  border-radius: 0 50px 50px 0;
  margin: 0px;
  vertical-align: middle;
  color: white;
  animation-name: slide;
  animation-duration: 1s;
  animation-iteration-count: 1;
  animation-timing-function: ease-in-out;
  animation-direction: forward;
}

.icontextlink:hover .hide {
  display: inline-block;
  background-color: gray;
  width: 100px;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
     transform: rotate(360deg); 
  }
}

@keyframes slide {
  0% {
    width: 0px;
    font-size: 0%;
  }
  100% {
    width: 100px;
    font-size: 100%;
  }
}
<script
      src="https://kit.fontawesome.com/7dd2bd858f.js"
      crossorigin="anonymous"
    ></script>

    <div class="container">
      <a href="#" class="icontextlink">
        <i class="fas fa-home circle-icon"></i>
        <span class="hide">HOME</span>
      </a>
    </div>

Necessary Adjustments:

1. Assign a specific class to the link to enable targeted CSS application instead of affecting all a elements within your container, like so:

<a href="#" class="icontextlink">
    <i class="fas fa-home circle-icon"></i><span class="hide">HOME</span>
</a>

2. Implement the animation on the circle upon hovering the entire link. Achieve this by changing the CSS selector from .circle-icon:hover to .icontextlink:hover .circle-icon, as shown below:

.icontextlink:hover .circle-icon { animation-name: spin; /* etc... */ }

3. Ensure the visibility of the hide class when hovering over the entire link - allowing seamless continuation of effects even while transitioning between circle and text. Modify the selector from .circle-icon:hover + .hide to .icontextlink:hover .hide:

.icontextlink:hover .hide {  display: inline-block; /* etc... */ }

4. Conceal the default blue underline during hover - eliminate the browser's default link styling using text-decoration:none;, like so:

a.icontextlink { text-decoration:none;}

5. Correct the rotation animation Though not explicitly mentioned in your query, the spin animation appears nonfunctional due to incorrect usage such as rotate: 0deg;. The proper format is transform: rotate(0deg);:

@keyframes spin {
  0%   { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

Answer №2

Alright, here's the breakdown:

  1. To achieve the "little black line", it involves the use of the text-decorator property within the a tag.
  2. Regarding the animation issue - the challenge arises when you move your cursor away from the home button while not hovering over it. This causes the :hover effect to cease, resulting in a return to display:none. However, since the button then moves back towards the cursor, the hover effect triggers again (creating an endless loop). To address this, I adjusted the display properties so that when you hover over the text, it remains as inline-block.

.container {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.circle-icon {
  background: gray;
  border-radius: 50%;
  padding: 15px;
  color: white;
  transition: padding 1s;
  margin: 0px;
}

.hide:hover {
  display: inline-block;
}

.hide {
  position: relative;
  left: -15px;
  display: none;
  line-height: 40px;
  height: 40px;
  width: 100px;
  text-align: center;
  border-radius: 0 50px 50px 0;
  margin: 0px;
  vertical-align: middle;
  color: white;
  background-color: gray;
  animation-name: slide;
  animation-duration: 1s;
  animation-iteration-count: 1;
  animation-timing-function: ease-in-out;
  animation-direction: forward;
}

.circle-icon:hover+.hide {
  display: inline-block;
}

@keyframes spin {
  0% {
    rotate: 0deg;
  }
  100% {
    rotate: 360deg;
  }
}

@keyframes slide {
  0% {
    width: 0px;
    font-size: 0%;
  }
  100% {
    width: 80px;
    font-size: 100%;
  }
}

a {
  text-decoration: none;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Home</title>
  <link rel="stylesheet" href="style.css" />
  <script src="https://kit.fontawesome.com/7dd2bd858f.js" crossorigin="anonymous"></script>
</head>

<body>
  <div class="container">
    <a href="#">
      <i class="fas fa-home circle-icon"></i>
      <span class="hide">HOME</span>
    </a>
  </div>
</body>

</html>

Answer №3

In order to eliminate the black line, I took the following action:

*{
  text-decoration: none;
}

This adjustment was implemented at the beginning of the css file.

Furthermore, I enhanced the animation for the appearance of the text:

@keyframes slide {
  0% {
    width: 0px;
    font-size: 0%;
  }

  25% {
    font-size: 0%;
  }

  27% {
    font-size: 20%;
  }

  100% {
    width: 100px;
    font-size: 100%;
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

HTML5 pattern grouping feature is not functioning as expected

I am attempting to validate this regular expression pattern="([a-zA-Z]+[0-9]*){4,}", which essentially means: It must always start with an alphabetic character. If a number is included, there must be at least 4 characters in total; for example, aaaa would ...

What is the best way to explain the concept of HTML5?

While reading a question on Stack Overflow, I came across a comment that stated: HTML5 is equal to HTML(5) + CSS3 + JavaScript. This statement truly caught me by surprise. Can it really be argued that HTML5 can be broken down into HTML(5), CSS3, and Ja ...

Is it possible to use the same identifier for both the name and id attributes in HTML?

In the world of coding, the "name" attribute is often used in server-side programming to send name/value pairs in requests. On the other hand, the "id" attribute is commonly utilized in client-side programming such as Javascript and CSS. However, both att ...

Modify the background color of a pseudo-element's property value dynamically

How do I change the background color of my burger menu by clicking on an icon? This is the CSS code I have: :root{ --pseudo-backgroundcolor: yellow; } .menu__icon span, .menu__icon::before, .menu__icon::after{ background: va ...

Create a navigation bar using CSS that is designed from an unordered list without any specific

Is it possible to create a multilevel menu using only CSS for the menu structure mentioned below? Websites like cssmenumaker.com showcase examples of menus with 2-3 level submenus by adding classes like has-submenu. Can we achieve this without adding any ...

Is there a way to implement a css/javascript property specifically for a div that is selected in the url?

Take for instance the scenario where I possess the URL example.com/#foo. In this case, CSS styling will be directed to the div with the id foo. If achieving this solely in CSS is not feasible, what methods in JavaScript or jQuery can be used efficiently ...

Display the initial page and activate the first navigation button when the page loads

Greetings to everyone. I am new to the world of jquery and currently in the process of learning. This is my script: <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"> </script> <script> $(function () { $ ...

Technique for updating URL when submitting a form on a webpage

I'm a newcomer to the world of coding and I have a simple issue that needs fixing. I want to create a form field where users can input the last segment of a URL provided to them. After entering this segment, I want the page to refresh automatically a ...

What is the best way to use AngularJS to extract values from the first dropdown menu that are linked to the values selected in the second

How can I link values in two dropdowns using AngularJS? Hello everyone, I have set up two dropdown fields in MyPlunker and within the ng-option tag, I have applied a filter like filter:{roles: 'kp'} to only display users with the role of kp. T ...

Underline Rows in CSS

When designing my website, I decided to organize the content into rows using CSS instead of tables. To create a line separating each row, I used height:px; for each row manually. However, this method resulted in the line jumping around inconsistently acr ...

Incorporating a YouTube or Vimeo video while maintaining the proper aspect ratio

On my video page, I embed Vimeo videos dynamically with only the video ID. This causes issues with the aspect ratio as black bars appear on the sides due to the lack of width and height settings. The dynamic video ID is implemented like this: <iframe ...

How to achieve a reverse slideToggle effect with jQuery when refreshing the page

After creating a custom menu on Wordpress using jQuery slideToggle to toggle dropdown on hover, everything seemed to be working perfectly. However, I noticed that when I refreshed the page while moving my cursor between two menu items with dropdown menus, ...

Error encountered: Required closing JSX tag for <CCol> was missing

Encountered a strange bug in vscode...while developing an app with react.js. Initially, the tags are displaying correctly in the code file. However, upon saving, the code format changes causing errors during runtime. For instance, here is an example of the ...

Working with CSS styles for the <datalist> element

I currently have a basic datalist drop-down menu and I am looking to customize the CSS to match the style of other fields on my website. However, as someone new to web design, I am unsure of how to go about this process. Check out the code here: http://j ...

What steps can I take to prevent BeautifulSoup from interpreting commas as tab characters?

My recent project involved creating a web scraping code to extract information from a local news website. However, I have encountered two issues with the current code. One problem is that when the code retrieves paragraph data and saves it to a CSV file ...

Are we utilizing this JavaScript function properly for recycling it?

Two functions have been implemented successfully. One function adds the autoplay attribute to a video DOM element if the user is at a specific section on the page. The other function smoothly slides in elements with a transition effect. The only limitatio ...

What is the best way to ensure the dropdown box in a Bootstrap navbar automatically adjusts its width to fit within the

When I click on the dropdown menu, the box extends beyond the width of the page and a horizontal scroll appears. How can I make the dropdown box automatically adjust its position if it reaches the edge of the page without needing a scroll (as shown in the ...

Exploring the integration of Construct 3 objects with ReactJs

Although I am well-acquainted with Construct 3 platform, I now have an interest in website development. Specifically, I would like to incorporate a character-like object into my web project that moves similar to a game character. While I know how to achiev ...

Using JQuery to retrieve part of a className results in a null value being returned

I am facing an issue with a piece of code in codesandbox where it returns null when I attempt to retrieve part of the className from a div using JQuery. How can I troubleshoot this and make it work? Check out the Codesandbox Example import React, { Compo ...

Tips on creating a repeating background image with CSS for a sprite

I'm facing a challenge that I can't seem to solve - I need to set a sprite as the background for a container. The issue is that despite my efforts, I haven't been able to achieve this. I've searched extensively but couldn't find a ...