CSS :hover problem, text appearing unexpectedly

I'm currently working on designing a logo that displays hidden text when hovered over, providing instructions to users. However, I've encountered an issue where the hidden text reveals itself even before the logo is hovered over. This is frustrating as I want the text to only appear when the logo is specifically hovered over, not anywhere near the logo. Below is the code I have implemented:

.logovAlign #hoverText {
  background-color: rgba(255, 255, 255, 0.8);
  text-align: center;
  width: 475px;
  padding: 15px;
  position: absolute;
  top: -100%;
  left: 50%;
  transform: translate(-50%, -50%);
  opacity: 0;
  transition: opacity 1s ease-out;
}

.logovAlign:hover #hoverText {
  transition: opacity 0.3s ease-in;
  opacity: 1;
}
<div class="logovAlign">
  <img src="images/favicon.png" width="50" height="50" alt "Lighting Bolt Logo">
  <p id="hoverText">If you see this logo on any of my pages, click it to return to this page!</p>
</div>

Answer №1

.logoAlign:hover indicates that when #hoverText is hovered over, a hover action will occur on the .logoAlign.

An alternative approach would be to detect the hover event on the img element. Then utilize the Adjacent Sibling Selector + to target and style the #hoverText element.

.logovAlign #hoverText {
background-color: rgba(255,255,255,0.8);
text-align: center;
width: 475px;
padding: 15px;
position: absolute;
/*top: -100%;*/
left: 50%;
transform: translate(-50%, -50%);
opacity: 0;
transition: opacity 1s ease-out;
}

.logovAlign img:hover + #hoverText {
opacity: 1;
transition: opacity 0.3s ease-in;
}
<div class="logovAlign">
    <img src="http://via.placeholder.com/350x150" width="50" height="50" alt"Lighting Bolt Logo">
    <p id="hoverText">If you see this logo on any of my pages, click it to return to this page!</p>
</div>

Answer №2

I have made some adjustments to your code to demonstrate my solution:

  1. I converted the image to a div so that it always appears.
  2. I removed the top: -100%; property, which was causing an error in the text preview.

The solution involved using the adjacent sibling selector in CSS: +. This allows you to apply a hover listener to one element and change the CSS properties of its sibling element.

In this scenario, the listener was added to the "red button," while adjusting the opacity of the text inside the p tag.

.logovAlign #hoverText {
  background-color: rgba(255, 255, 255, 0.8);
  text-align: center;
  width: 475px;
  padding: 15px;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  opacity: 0;
  transition: opacity 1s ease-out;
}

#button {
  width: 50px;
  height: 50px;
  background-color: red;
}

#button:hover + #hoverText {
  transition: opacity 0.3s ease-in;
  opacity: 1;
}
<div class="logovAlign">
  <div id="button"></div>
  <p id="hoverText">If you see this logo on any of my pages, click it to return to this page!</p>
</div>

Answer №3

The problem lies in the fact that opacity retains the pointer-events, allowing elements with opacity:0 to still be hovered over and clicked on.

To resolve this, you can either add pointer-events:none to your .logovAlign #hoverText, or switch from visibility:hidden to visibility:visible, as elements with hidden visibility won't trigger pointer-events.

Here is an example demonstrating the issue (as the provided code doesn't fully address it)

.logovAlign{
   display:inline-block;
}
.logovAlign #hoverText {
  background-color: rgba(255, 255, 255, 0.8);
  text-align: center;
  width: 475px;
  padding: 15px;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  opacity: 0;
  transition: opacity 1s ease-out;
}

.logovAlign #logo {
  width: 30px; height: 30px;
  background-color: lime;
  display:inline-block;
}

.logovAlign:hover #hoverText {
  transition: opacity 0.3s ease-in;
  opacity: 1;
}
<div class="logovAlign">
  <div id="logo"></div>
  <p id="hoverText">If you see this logo on any of my pages, click it to return to this page!</p>
</div>

Corrected example

.logovAlign{
   display:inline-block;
}

.logovAlign #hoverText {
  background-color: rgba(255, 255, 255, 0.8);
  text-align: center;
  width: 475px;
  padding: 15px;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  opacity: 0;
  transition: opacity 1s ease-out;
  pointer-events:none;
}

.logovAlign #logo {
  width: 50px; height: 50px;
  display:inline-block;
  background-color: lime;
}

.logovAlign:hover #hoverText {
  transition: opacity 0.3s ease-in;
  opacity: 1;
}
<div class="logovAlign">
      <div id="logo"></div>
      <p id="hoverText">If you see this logo on any of my pages, click it to return to this page!</p>
    </div>

A more effective solution would be to use the logo itself as the hover element instead of the containing div, and utilize a sibling selector to target the hover text. For instance:

#logo:hover + #hoverText {
  transition: opacity 0.3s ease-in;
  opacity: 1;
}

Answer №4

This solution proved to be effective for me. I initially used JavaScript instead of css.

var span = document.getElementById("text");
function showText() {
span.style.visibility = "visible";
}
function hideText() {
span.style.visibility = "hidden";
}
<!DOCTYPE html>
<html>
<body>
<img src="https://i.sstatic.net/8ALM5.png" id="img1" class="img1" onmouseover="showText();" onmouseout="hideText();"/>
<span class="text" id="text" style="visibility:hidden;">If you see this icon on any of pages, click it to return to the index.</span>
</body>
</html>

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

Shade the tag when the checkbox is marked

Is it possible to change the color of a label without using javascript when the associated checkbox is checked? ...

What is the best way to extend an inline-block element to cover the entire text line?

Let's talk about the scenario: https://i.stack.imgur.com/we05U.png In the image, there is a container (displayed as a div) with only one child - a span element. The goal is to introduce a second child that occupies the entire red space depicted in t ...

Parameter within onClick function that includes a dot

I'm attempting to design a table that enables an onClick function for the Change Password column's items so my system administrator can adjust everyone's password. Each onClick triggers the "ChangePassOpen" function which opens a modal with ...

Executing a function on each page within the head tag in Nuxt.js

I successfully made my function accessible by using the plugin attribute in the nuxt.config.js file, allowing me to call the function under mounted on every page. I am looking for a more efficient way to run this function within the head tag and have it b ...

What is the best way to position a Button on the far right of an MUI AppBar?

I'm struggling to grasp the concept of aligning items in MUI. Here is my code snippet: class SignUpForm extends React.Component { render() { return ( <Button sx={{ justifyContent: "flex-end" }} colo ...

Is there a way to populate two mysql tables using just one textarea input?

Hello, I'm currently working on my "Add Blog Post" page where I would like to make a change. Specifically, I want to eliminate one textarea (the post description area) and have the content entered in the Post Content textarea populate both the post de ...

What are the guidelines for incorporating tag names in CSS selectors?

I created a button that when clicked, displays a table containing actors. I have named them as follows: <button class="actors">Show actors</button> <table class="actors">...</div> Next, I want to style and access them in my CSS (a ...

Adjustable dimensions for a collection of squares based on screen size

I am currently designing a grid composed of 1:1 squares and allowing the user to continually add more squares. My main goal is to ensure that the size of each square maintains its aspect ratio while being able to resize accordingly. The challenge lies in k ...

What is the process to discover, upload, and implement a custom font?

In my quest to locate and incorporate a font named neontubes, or if not available, a font called freestyle script, I have learned that I must obtain an src which is then included in my style.css file and subsequently designated in the styling rule like s ...

There seems to be an issue with the rangeslider.js jQuery plugin not being recognized as a function within the project. However, there are

I am currently working on integrating a Range-slider into my Django project with the help of rangeslider.js. I was able to successfully create a functional example on Codepen at https://codepen.io/Slurpgoose/pen/GRRpmpX, and everything seemed to be running ...

What CSS selector should I apply to create a circular profile image for Buddypress users?

In my current WordPress project, I am utilizing the BuddyPress, WooCommerce, and WC Vendors plugins for enhanced functionality. To personalize each vendor's product listings, I decided to display their BuddyPress profile picture alongside their produ ...

When outputting HTML, make sure to use an if statement

Looking to dynamically generate select options without using if statements. Instead of string manipulation, I want a cleaner solution. <select name="test"> <option disabled selected> -- Select an industry -- </option> <?php ...

Issue with Photoswipe pswp class Not Getting Properly Cleared Upon Closing Image

My website has a Photoswipe image gallery from . The issue I'm facing is that the CSS class does not reset or clear after closing the gallery for the second time. For example, when a user opens item 1, the images are loaded into the picture div via A ...

Using CSS to rearrange the order of specific div elements with the nth-child() selector when targeting different

I am attempting to design a centered navbar for a webpage. The navbar consists of 5 divs and my goal is to have the third div become the first one when the screen size goes below 600px. Here is the HTML code: <div class="mainmenu"> < ...

What is the best way to enlarge an image while keeping it contained within a card using Bootstrap?

Is it possible to create a visually appealing card similar to the image provided using Bootstrap 5.3.3 and HTML on a website? https://i.sstatic.net/OH4U0.png ...

Is the background color extending the entire width of the page?

Hey there, I'm currently trying to determine how to set the background-color on a paragraph or h1 so that it only appears behind the words and not across the entire page with blank space. I'm still a beginner when it comes to coding. I'm wor ...

Changing the event when a class is active in Vue3

Question I am looking for a way to trigger an event when the 'active' class is added to an element. How can I achieve this? I believe this could potentially be accomplished using a watcher method, but I am unsure how to watch for the applicatio ...

I am having issues with my JavaScript code, I could really use some assistance

Currently, I am developing a custom file search program. The goal is to have a textarea where users can input text, which will then generate a clickable link below it. However, I am facing issues with the current implementation. Below is the snippet of my ...

Using jQuery to fetch LDAP data and return it in JSON format

I am encountering an issue with a returned JSON value. When the LDAP result is valid, the JSON return is also OK but when the result is invalid, the JSON becomes invalid as well. I am using UWamp and I am new to this. Thank you all. PHP code : <?php ...

Showing information in a table on a webpage using JavaScript and HTML

After running a console log, I am presented with the following data: 0: {smname: "thor", sim: "9976680249", pondo: "10"} 1: {smname: "asd", sim: "9999999999", pondo: "0"} 2: {smname: "asd", sim: "4444444444", pondo: "0"} 3: {smname: "bcvb", sim: "78678967 ...