Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Hovering over an image to show a text box is not working (html, css)

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>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading