Reputation: 561
I am a dropdown menu with two links, currently i want the menu to stick to the top right part of the section, however I styled it in a way that the container is supposed to be relatively positioned but it the ul inside is absolute so it drops down correctly. How do I now fix this in place? I tried adding position fixed to the button but obviously on scrolling down the ul doesn't appear even thought the button is fixed.
.contact {
position: relative;
top: 5vh;
right: 4vw;
font-family: Helvetica, sans-serif;
height: 20vh;
letter-spacing: 3px;
}
.contactbtn {
color: white;
text-transform: uppercase;
font-size: 1.5rem;
font-weight: bold;
background: none;
border: none;
text-decoration: none;
cursor: pointer;
outline: none;
position: fixed;
/* only fixes the button */
}
.contactbtn:hover {
opacity: 0.6;
}
.contact ul,
.contact-a ul {
position: absolute;
left: -10vw;
margin-top: 4vh;
color: black;
width: 300px;
height: 150px;
font-size: 1.5rem;
font-weight: bold;
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: flex-end;
list-style: none;
text-transform: uppercase;
font-size: 1rem;
opacity: 0;
transform: translateY(-10px);
transition: all 0.4s ease;
-webkit-transform: translateY(-10px);
-webkit-transition: all 0.4s ease;
-moz-transform: translateY(-10px);
-moz-transition: all 0.4s ease;
}
.contact a,
.contact-a a {
text-decoration: none;
}
.contact a:hover {
background-color: var(--grCol3);
}
.menu-container:hover .menu {
opacity: 1;
pointer-events: all;
transform: translateY(0);
-moz-transform: translateY(0);
-webkit-transform: translateY(0);
outline: none;
}
<div class="contact menu-container">
<button class="contactbtn" style="color:black;">Contact</button>
<ul class="menu">
<li>
<p>For Any Enquiries→</p>
</li>
<li><a id="mail" style="color:black;" href="mailto:[email protected]" target="_blank">[email protected]</a></li>
<li>
<p>+44 5555 070158</p>
</li><br>
<li><a id="ig" href="https://www.instagram.com/leung/?hl=en" target="_blank" style="color:black;">Instagram</a></li>
</ul>
</div>
Upvotes: 0
Views: 583
Reputation: 3841
Your example is not very clear since there's no vertical scrolling to be seen (not enough content to overfill the snippet example) BUT, adding some filler content, I see where this is going:
In order for your dropdown menu, which appears on the :hover of the button, to be relative to the button, regardless of your scroll position, it needs to be a child of the button. For that, the parent of the drop-down menu must be position:relative but, since the button is fixed, we need the following structure:
Which looks like this:
And then the nav area itself looks like this:
and finally, your css could be something like:
.nav-area {
position: fixed;
left: 20px;
top: 20px;
display: block;
width: 100px;
height: 50px;
overflow: visible;
}
.nav-wrapper {
display: block;
width: 100%;
height: 100%;
position: relative;
overflow: visible;
}
.button {
display: block;
width: 100px;
height: 50px;
}
.drop-down-menu {
display: none;
position: absolute;
left: 0;
top: 50px;
}
On hover, make the next sibling element show (your dropdown menu)
.button:hover + .drop-down-menu {
display: block;
}
So, your nav-area is fixed to your screen, the nav-wrapper is inside the nav-area and has position:relative
to allow position:absolute;
elements inside it to target it. However, you don't really need position:absolute
anymore.
Give it a try and let us know how it went.
Upvotes: 1