Home > Back-end >  Hovering over an image to show a text box is not working (html, css)
Hovering over an image to show a text box is not working (html, css)

Time:05-01

When hovering over the image, a textbox should be shown. However, it is not working for me and I am completely stuck.

These are the code snippets, thank you in advance.

#passinfo {
  height: 20px;
  width: 20px;
  margin-left: 130px;
  margin-top: -20px;
  margin-bottom: 15px;
  padding: -20px;
  position: absolute;
  display: block;
}

#infotext {
  background-color: #628ca0;
  border-radius: 5px;
  color: rgba(34, 34, 34, 0.829);
  padding: 5px 4px 4px 4px;
  position: relative;
  box-shadow: 0px 1px 5px #999;
  opacity: 0;
  margin-left: 100px;
  left: 10px;
  position: absolute;
  transition: .5s ease;
}

#passinfo .img:hover #infotext {
  opacity: 1;
  visibility: visible;
}
<h4>Password</h4>
<img src="infoicon.png" id="passinfo">
<div id="infotext">Password must be at least 8 characters and must contain an uppercase letter, a lowercase letter, and a number.</div>

CodePudding user response:

Your selector:

#passinfo .img:hover #infotext

is looking for the element with id="infoText" that is a descendant of an element with a class of img, that is hovered-over, that is itself a descendant of an element with id="passinfo".

Your <img> element does not have a class of img, it is an <img> element and itself has the id="passinfo"; and <img> elements cannot have any child, or descendant, elements of any kind (not even pseudo-elements); so this selector will, and can, never apply.

Instead, you're looking for – and should use – the following:

#passinfo:hover   #infotext

Which selects the element with id="infotext" which is the immediately-adjacent sibling of the element with a class of img, that is hovered-over.

#passinfo {
  height: 20px;
  width: 20px;
  margin-left: 130px;
  margin-top: -20px;
  margin-bottom: 15px;
  padding: -20px;
  position: absolute;
  display: block;
}

#infotext {
  background-color: #628ca0;
  border-radius: 5px;
  color: rgba(34, 34, 34, 0.829);
  padding: 5px 4px 4px 4px;
  position: relative;
  box-shadow: 0px 1px 5px #999;
  opacity: 0;
  margin-left: 100px;
  left: 10px;
  position: absolute;
  transition: .5s ease;
}

#passinfo:hover   #infotext {
  opacity: 1;
  visibility: visible;
}
<h4>Password</h4>
<img src="https://via.placeholder.com/150" id="passinfo">
<div id="infotext">Password must be at least 8 characters and must contain an uppercase letter, a lowercase letter, and a number.</div>

CodePudding user response:

Change

#passinfo .img:hover #infotext {
   opacity: 1;
   visibility: visible;
}

to

#passinfo:hover
   opacity: 1;
   visibility: visible;
}

CodePudding user response:

Move your #passinfo into a div wrapping the image and text, and use that to set the :hover.

<h4>Password</h4>
  <div id='passinfo'>
    <img src="icon.png">
    <div id="infotext">Password must be...</div>
  </div>

#passinfo:hover #infotext {
  opacity: 1;
}

W3Schools.com is a great resource for stuff like this if you haven't heard of it. https://www.w3schools.com/howto/howto_css_image_overlay.asp

CodePudding user response:

#passinfo:hover {
  opacity: 1;
  visibility: visible;
}

  • Related