I have a div that can have various heights based on the size of the text within it. I use jquery to show it when a user hovers over an icon. I want the div to appear vertical center to that icon. I can’t use the normal method of:
height: 100px; top: 50%; margin-top: -50px;
because I can’t specify the height of the element due to the variations in height.
Any ideas on how I could do this? Thanks.
Here’s a very straightforward solution that should work for any element height.
HTML:
jquery:
We get the height of the element, then divide it in half. We then set the margin-top to the negative of half of the height of the div.