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

Is there a way to trigger the onclick event while dragging the div?

Exploring a Div: <div id="up" onmousedown="handleMouseDown('url(/scanToUserBox/img/cpt_subfunc_005_prev_p.png)', this)" onclick="changePage('up')"> </div> Upon clicking the div, the onmousedown event is trig ...

Learn the process of setting a timestamp using this PHP script

I have a script that retrieves data based on selected dates, but I want to modify it to work with timestamps instead. <link rel="stylesheet" type="text/css" href="tcal.css" /> <script type="text/javascript" src="tcal.js"></script> <fo ...

How can I implement a jQuery slide down effect for a specific individual instance?

Whenever I click on the "Share your thoughts" button, the comments block slides down for every section instead of just the particular section I clicked on. Is there a way to make it so that only a single block slides down when the button is clicked, regard ...

Ways to bypass a transition using JavaScript

Hello everyone, I would like to start by apologizing for any mistakes in my English. I have been working on a website, which is currently available here: As you scroll down, the height of the image below the menu decreases. I am trying to make the navig ...

Is it possible to utilize a variable in determining the order of operations?

I'm currently developing a simple calculator script using a combination of HTML and PHP. The concept involves inputting two numbers and selecting the operator to perform the calculation. HTML Code <html> <head> <title>Calculat ...

Creating an HTML form that spans across multiple pages: Tips and tricks

When creating a shopping form that includes items like mugs and tshirts, is it permissible according to website standards to design a form that spans multiple pages? One option could be to add radio buttons for choosing a color directly on the main form ...

PHP Multiple Filter Options

Currently, I am developing a filtering system for a mySQL database that utilizes dropdown menus. While the system functions correctly when using one filter, I am encountering difficulties in stacking multiple filters. To achieve this, I am retrieving the c ...

JavaScript Canvas - Preserve Image Transparency

I have been trying to download a snapshot of a canvas using the code below: var strMime = "image/png"; var canvas = document.getElementsByTagName('canvas')[0]; var link = document.createElement("a"); link.href = canvas.toDataURL( ...

Conceal a certain element in development using CSS

Is there a way to hide the footer section from my webpage without affecting the rest of the content? For example, using something like: "div class="poweredby" display: none", but I'm unsure of the correct method... Here is the spe ...

Open a file using the fs module while ensuring the original file is preserved

I've been working on a NodeJS API that sends emails. Each time I need to send an email, I use the index.handlebars template. To customize the template before sending the email, I utilize the Node File System (fs) module to readFileSync() and then ap ...

Why does the browser keep converting my single quotation marks to double, causing issues with my JSON in the data attribute?

This task should be straightforward, but I seem to be facing a roadblock. I am trying to insert some JSON data into an input's data attribute, but the quotes in the first key are causing issues by prematurely closing the attribute. Here is the code ...

The radio button is not providing the necessary notification

I currently have the following elements on my webpage: Table rows that contain radio buttons A link labeled "Issue" that triggers a popup with one submit button A submit button labeled "details" that also triggers a popup The issue I am facing is as fol ...

expanding menu options in a dynamic design

My webpage features a logo alongside a menu: Here's the HTML code: <div id="logomenuwrapper"> <div id="logo"><img src="img/logo_light.jpg"/></div> <div id="menu"> <ul> <li><a href="#">About ...

I'm encountering an issue in Atom where it says "Module 'editorconfig' cannot be found."

I keep encountering an issue in the Atom text editor where it says "Cannot find module 'editorconfig'" every time I try to save a .html file. Even though I had previously installed the EditorConfig package without any problems. How can I resolve ...

Having difficulty animating the height transition of the NextJS Image component

On my webpage, I have a headerbar that changes size (from 120px to 70px) when the user scrolls down. I successfully implemented this transition using Tailwind classes and a height transition. Now, I am trying to make the site logo resize along with the hea ...

Tips for combining a select option and search field into a seamless integrated feature

I'm looking to implement a search field in my project that includes the ability to select specific parameters before running the search. I want it to have a seamless design similar to the image shown below. Although I couldn't find a matching co ...

Adding and removing/hiding tab-panels in Angular 4 with PrimeNg: A step-by-step guide

I'm currently working on a tabView project with a list of tab-panels. However, I am struggling to find a way to dynamically hide and unhide one of the tab panels based on specific runtime conditions. Does anyone have any suggestions or insights on how ...

Indent the text within a span element that is displayed as an inline block

I am in search of a solution using only CSS for the following issue. Take into account the HTML below: <p> <span>Some text</span> <span>Some text</span> </p> Both <span> elements are set as inline-block. ...

`Is it possible to modify the background color of InputAdornment in Material-UI TextField?`

For the first 10% of the text field, one background color is used, while for the remaining 90%, another background color is applied. <TextField className={classes.root} type="text" placeholder="username" var ...

Show only the lower left quadrant within the img tag during the prepend operation

I'm attempting to add an <img> tag in front of a <div> similar to this example on JSFiddle. However, I have a specific requirement to only display the bottom left quarter of the image instead of the entire one. HTML Markup <div id="my ...