Reputation: 61
Using CSS & HTML I am trying to create a popup which should appear on a button hover. But in the code below the popup appears both for the <button>
& <h2>
tag. Since I am a beginner I am not able to understand where the changes are required to get the right output. Please help me fix this.
HTML PAGE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title> POPOUP</title>
<link rel="stylesheet" type="text/css" href="tooltip.css">
</head>
<body style="text-align:center">
<div class="popup">
<h2>Popup</h2>
<button type="button" id="b1">BUTTON</button>
<span class="popuptext" id="myPopup">A Simple Popup!</span>
</div>
</body>
</html>
I do not have proper knowledge of JS hence I have used CSS for the hover functionality.
CSS
/* Popup container - can be anything you want */
.popup {
position: relative;
display: inline-block;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* The actual popup */
.popup .popuptext {
visibility: hidden;
width: 160px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 8px 0;
position: absolute;
z-index: 1;
top: 225%;
left: 50%;
margin-left: -80px;
}
/* Popup arrow */
.popup .popuptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #555 transparent transparent transparent;
}
/* Toggle this class - hide and show the popup */
.popup .show {
visibility: visible;
-webkit-animation: fadeIn 1s;
animation: fadeIn 1s;
}
.popup:hover .popuptext {
visibility: visible;
opacity: 1;
}
/* Add animation (fade in the popup) */
@-webkit-keyframes fadeIn {
from {opacity: 0;}
to {opacity: 1;}
}
@keyframes fadeIn {
from {opacity: 0;}
to {opacity:1 ;}
}
Upvotes: 2
Views: 10834
Reputation: 13558
You need to use this selector, this will trigger popover only when #b1
is hovered.
.popup #b1:hover + .popuptext
Instead of this, this was triggering popover when .popup
or it's all child elements was hovering.
.popup:hover .popuptext
.popup {
position: relative;
display: inline-block;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* The actual popup */
.popup .popuptext {
visibility: hidden;
width: 160px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 8px 0;
position: absolute;
z-index: 1;
top: 225%;
left: 50%;
margin-left: -80px;
}
/* Popup arrow */
.popup .popuptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #555 transparent transparent transparent;
}
/* Toggle this class - hide and show the popup */
.popup .show {
visibility: visible;
-webkit-animation: fadeIn 1s;
animation: fadeIn 1s;
}
.popup #b1:hover + .popuptext {
visibility: visible;
opacity: 1;
}
/* Add animation (fade in the popup) */
@-webkit-keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
<body style="text-align:center">
<div class="popup">
<h2>Popup</h2>
<button type="button" id="b1">BUTTON</button>
<span class="popuptext" id="myPopup">A Simple Popup!</span>
</div>
</body>
Upvotes: 3