Currently, I am working on implementing a button with a popup that appears underneath it when the user hovers over it.
The specific requirements for this setup are:
- The size of the button should not affect the size of the popup
- The popup should always be a fixed distance from the bottom edge of the button
- The popup should not disrupt the layout of any other element (hence the absolute positioning)
- The popup should be centered relative to the button
To better visualize this concept, please refer to the image provided below before analyzing my current attempt at coding this functionality.
.button-container {
display : inline-flex;
height : 100%;
}
.popup-positioner {
display : inline-block;
position : relative;
right : 50%;
}
.button-container button {
background-color : rgba(40, 40, 40, 1);
border : none;
border-radius : 5px;
outline : 0px solid rgb(90, 90, 90);
font-weight : 900;
color : rgb(220, 220, 220);
padding : 8px 10px 8px 10px;
}
.button-container .popup {
display : none;
background-color : rgba(80, 80, 80, 1);
color : rgba(220, 220, 220, 1);
border-radius : 5px;
-webkit-transform : translate(-50%, 110%);
padding : 6px 10px 6px 10px;
}
.button-container:hover .popup {
position : absolute;
display : block;
animation : show_popup 0.5s;
}
@keyframes show_popup {
0% {
opacity: 0;
-webkit-transform: translate(-50%, 110%);
}
100% {
opacity: 1;
-webkit-transform: translate(-50%, 110%);
}
}
<div class="button-container">
<button>
this is a button with lots of text
</button>
<div class"popup-positioner">
<div class="popup">
this is a popup
</div>
</div>
</div>
At present, the code functions as described above. However, there is an issue where the popup's positioning is based on its own height.
.button-container .popup {
...
-webkit-transform: translate(-50%, 110%);
...
}
This results in undesirable behavior on smaller screens where the popup does not align correctly.
I am seeking assistance on adjusting the code so that the popup's position is relative to the height of the <button>
it is associated with.
Any guidance or suggestions to achieve this would be greatly appreciated.
Thank you - Oli