I am creating a little website and I can't get the drop-down menu, which comes out of the "gallery" button when I hover on it, to overlay the paragraph below the menu instead of pushing the whole content down. I've already tried every combination of position: relative; z-index: 9999;
. However, when I set position: absolute;
for the <ul>
element it actually works, but messes up all the layout of <ul>
items. Please help me to get this done because I'm starting to bump my head... Here's my code. You can see how the paragraph goes down when you hover on the "gallery" button. I wish the paragaph stayed where it is and the drop-down menu overlayed it.
Asked
Active
Viewed 4.8k times
8

Salivan
- 1,876
- 7
- 26
- 45
3 Answers
15
First position the parent element as relative
to make it establish a new containing block for absolutely positioned descendants:
.menu-item {
width: 33.33%;
float: left;
position: relative; /* <-- added declaration */
}
Then, position the nested <ul>
absolutely, add top
, left
offsets as well as width
:
.menu-item ul {
margin-top: 5px;
list-style-type: none;
height: 0px;
overflow: hidden;
position: absolute; /* <-- added declarations */
left: 0; top: 100%; /* here */
width: 100%; /* and here... */
transition: height 1s ease;
}

Hashem Qolami
- 97,268
- 26
- 150
- 164
4
You need to set your .menu-item a relative position
position: relative;
and now you can set your ul
to absolute
position without messing up your layout.
Here's an updated version of your fiddle

slashsharp
- 2,823
- 2
- 19
- 26
-
But I can clearly see a messed up layout. Are you sure about your solution? – Salivan Apr 19 '15 at 16:30
-
I'm not sure what do you mean, the layout is still the same except for the `ul`. You need to style it. – slashsharp Apr 19 '15 at 16:32
-
Yes, I mean that the layout of `
- ` messes up after setting the `position: absolute;` property. @Hashem Qolami just gave me what I really needed. Sorry, but his example was a little bit better but thank you too!
-
Yeah, and that was what did the whole trick. I would have been trying to style it for ages, because this is my second day working with CSS, but he just showed me the simplest and clearest way. – Salivan Apr 19 '15 at 16:40
` nicely layout under the `` element? From the first thought it seems that it should make the `
– Salivan Apr 19 '15 at 16:49` to appear right near the top edge of it's container, but thankfully it's not the case, because I need it just right under the `` element.