CSS - Z-index is not working with positions relative and absolute - html

I've made dropdown menu using pure css3, but one property(z-index) is not working as I expected and that is a very big problem, because the dropdown list is dropping ABOVE the menu. Ideally it must be dropped UNDER the menu. I've tried to do something with that problem all day but unfortunately can't figure it out, so now asking for help...
I've made a different background-colors for problem items for good seeing what i'm trying to reach. The main aim is that sub-menu with red background must be under the blue background.
P.S. I've already tried to make this menu with jQuery slideDown/slideUp properties but they don't look like ideal slide effect(as in my example). They look more like stretching, and that's not what I want..
EXAMPLE ON JSFIDDLE
ul {
list-style-type: none;
}
.menu_wrapper {
position: relative;
z-index: 999;
/* IS NOT WORKING... O_o...*/
height: 70px;
width: 600px;
background-color: blue;
}
.menu li {
display: inline;
float: left;
display: table;
height: inherit;
margin: 3px;
margin-top: 10px;
}
.menu li a {
display: table-cell;
font-family: Verdana;
font-size: 16px;
color: gold;
text-align: center;
text-decoration: none;
padding-left: 10px;
padding-right: 10px;
vertical-align: middle;
background: #05487F;
transition: .2s ease-in-out;
}
.sub-menu {
position: absolute;
z-index: 1;
/* IS NOT WORKING... O_o...*/
margin-top: -200px;
margin-left: -132px;
padding: 15px;
padding-top: 20px;
background-color: red;
transition: all 1s ease-in-out;
}
.sub-menu li {
float: none;
}
.sub-menu li a {
padding: 5px 15px;
}
.menu li a:hover + .sub-menu {
margin-top: 40px;
}
.sub-menu:hover {
margin-top: 40px;
}
<nav class="menu_wrapper">
<ul class="menu">
<li>About
</li>
<li>News
</li>
<li>
PROBLEM HERE<br>(HOVER THIS)
<ul class="sub-menu">
<li>link 1
</li>
<li>link 2
</li>
<li>link 3
</li>
</ul>
</li>
<li>Something
</li>
<li>Contacts
</li>
</ul>
</nav>

The problem is that you're establishing a stacking context on .menu_wrapper when you set z-index: 999. When a stacking context is established, you cannot position a descendant element behind an ancestor.
Remove z-index: 999 from .menu_wrapper:
.menu_wrapper {
position: relative;
/* z-index: 999; << remove */
height: 70px;
width: 600px;
background-color: blue;
}
Then change the z-index on .sub-menu from 1 to a negative number such as -1:
Updated Example
.sub-menu {
position: absolute;
z-index: -1;
margin-top: -200px;
margin-left: -132px;
padding: 15px;
padding-top: 20px;
background-color: red;
transition: all 1s ease-in-out;
}

Here is the deal with z-index.
Z-index is relative to its parent (and ultimately relative to the Window object) in the normal DOM flow. However, Absolutely positioned objects are removed from the normal DOM flow and therefore, Z-Index is relative to itself - rather than the Window object.
In your case, you have not set position for your parent menu, so it will be automatically assigned position:static which CANNOT be z-indexed.
Adding the following to your parent menu should allow you to z-index the submenu to rest above it.
position:relative;
z-index:1;

Related

Can't Find a Way to Remove the Whitespace/ Border Around My List Items

My navbar becomes a dropdown menu for mobile devices. It's a CSS menu, However, the dropdown items have a sort of white space or border around them.
I tried all options listed here: How to remove the space between list items Unfortunately, these solutions didn't work for me. I also took the menu apart and could remedy some of the borders by moving #navigation a's margin-top to 0, reducing the opacity of background by 0.05, and by setting the height of the list item to 38px. Still, the "border" remains an eyesore on some devices, incl iPhone X.
This site is a site for my own music project, so I put it live, in case that helps: http://mynameisdidi.com/
#media screen and (max-width: 695px) {
.hamburger {
display: none;
}
.hamburger.showClass {
display: block;
position: fixed;
}
nav {
position: relative;
z-index: 500;
}
ul {
float: right;
width: 100%;
transition: .45s ease-in;
margin-top: -25px;
}
li {
width: 95%;
height: 38px;
}
#navigation a {
display: block;
width: 58.5%;
margin-right: 1px;
margin-top: 0;
float: right;
background-color: rgba(255, 255, 255, 0.75);
color: #b406c7;
/*magenta*/
text-align: center;
padding: 6px;
font-size: 19px;
text-decoration: none;
text-transform: uppercase;
font-weight: bold;
}
}
<div class="hamburgerIcon" onclick="toggleClass()">
<h2>☰</h2>
</div>
<nav id="navigation">
<ul class="hamburger">
<li>Home</li>
<li>Music</li>
<li>Bio</li>
<li>Booking</li>
</ul>
</nav>
Sorry if this post doesn't meet the posting criteria, my first post here. (Just let me know for the future. Thank you.)
If you mean spaces between li elements it causesheight: 38px;. Change it (remove) and it will be without spacing.
![how i see li spaces][1]
https://i.stack.imgur.com/v6Rhl.png

CSS Vertical Menu with submenu callouts overlapping

Its been a while - but I found myself a new exiting hobby in web development, and I am hoping that you can help me overcome the following issue:
I am working on a personal website. Content etc. is all preliminary. My main focus is the vertical menu on the left hand side. While I am pretty happy with the appearance so far, there are still some questions:
1) I had to set the transition as ease out to >0s, so that I have enough time between the main-menu hover and the hover over the sub-menu. Because of this, if I hover between main-menu items, they will briefly overlap due to the phase out. Is there a way to work around this?
(the focus selector might be an option, but that requires to click the menu item, and I prefer the hover)
2) The sub-menu is aligned to the top of the li of the main-menu. Because of that, the last item's sub (Testing) exceeds the boundaries of the page, giving whitespace below the page. Is there a way to make the sub-menu shift up if it presses against the edge of the overall body?
I probably could give it a different tag and design it differently, am I am hoping there is a more general solution.
3) Minor issue: When switching from main-menu hover to sub-menu hover, the main-icon "blinks" very briefly. Is that normal behavior? Certainly isn't pretty.
4) Last but not least: As this is my first time doing a website, I would be more than grateful to get your general feedback on inefficiencies in the code, so I can be more lean in the future.
(see updated fiddle below - full code removed to save space.)
Thanks for your help!
EDIT
THanks for your answers - issue #1 has been solved using the approaches you outlined!
However, the last submenu still gives me a bit a headache.
If I use the "last-child" selector, and the code added below, it will not change anything. However, if I give the last "bubble" a different tag, using the exact same code, the bubble will align to the bottom of the last main-menu item, but the containing UL remains defiant.
As you can see from the original CSS, the last "relative" is the main-menu IL, so shouldn't the submenu and the containing UL both align to the bottom of their respective parent/grandparent?
using a dedicated, different tag for the last-child "bubble_last
Using the last-child selector
and the accompanying new code (updated fiddle with full code: Heres the Fiddle ...use large screen for the result to see the alignment issue):
.navigation ul li:last-child {
border: solid orange;
}
.navigation .bubble_last{
position: absolute;
margin-top: 0%;
padding: 0px;
left: 60px;
bottom: 0px;
width: 400px;
height: 200px;
border: solid red;
}
.navigation .bubble_last ul{
position: absolute;
left: 0;
bottom: : 0px;
margin-top: 0px;
padding: 0px;
width: 20vw;
height: 100px;
font-size: 15px;
font-size: 1.2vw;
font-weight: 400;
border: solid blue;
margin-left: 28%;
display: block;
}
.navigation .bubble_last ul li {
list-style: none;
background-color: none;
border-radius: 3px;
height: 2vw;
line-height: 1.2vw;
width: 20vw;
position: relative;
padding-top: 0px;
margin: 0;
padding: 0;
}
Instead of adding a transition delay on .bubble, you just need to make the .bubble class wide enough (and start behind the main li trigger with a left: 60px) so that the hover will be continuous:
.navigation .bubble {
position: absolute;
margin-top: 0%;
padding: 0px;
left: 60px;
top: 0px;
width: 200px;
height: 100%;
background-color: none;
}
Then just add a margin-left to the submenu:
.navigation ul li ul {
margin-left: 20px;
}
To fix your second issue, simply add the following CSS to make the last submenu positioned relative to the bottom of the nav item:
ul li ul.subm:last-child {
top: auto;
bottom:0px;
}
Updated Demo: JSFiddle
How about this? Adjust your navigation to use the nav element, set it to use flex box, adjust that so it fits the height of your page, and then adjust the transition effects so they transition in and out properly.
I hope this helps.
#import url("http://fonts.googleapis.com/css?family=Roboto");
* {
border: none;
margin: 0;
padding: 0;
}
html,
body {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
font-family: Roboto Condensed;
box-sizing: border-box;
}
.landing {
display: table;
margin: auto;
width: 100%;
height: 100%;
background: url("https://imageshack.com/i/poXkyeIYj") 50% 50% no-repeat;
background-size: cover;
top: 0;
position: relative;
z-index: 1;
}
.landing .welcome {
margin: 10px;
width: 89%;
color: #FFF;
text-align: center;
display: table-cell;
vertical-align: middle;
position: absolute;
left: 10%;
top: 30%;
}
#mug {
background: url("https://imageshack.com/i/pmJaAuFkj") 50% 50% no-repeat;
background-size: cover;
border: solid 3px #FFF;
/*max-width: 20%;
max-height: 20%; */
width: 150px;
height: 150px;
margin: auto;
border-radius: 100%;
}
nav {
width: 50px;
height: 100%;
flex: 1;
display: flex;
justify-content: center;
flex-direction: column;
}
nav img {
max-width: 50px;
max-height: 50px;
}
.nav ul {
*zoom: 1;
list-style: none;
margin: 0;
padding: 0;
-ms-flex: 0 100px;
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
justify-content: space-between;
margin-top: 0px;
padding: 0px;
width: 11vw;
height: auto;
font-size: 15px;
font-size: 1.2vw;
font-weight: 400;
border-color: none;
}
nav ul li {
background-color: rgba(222, 225, 229, 0.8);
border-radius: 3px;
width: 100%;
font-size: 15px;
font-size: 1.2vw;
font-weight: 900;
}
.nav ul:before,
.nav ul:after {
content: "";
display: block;
}
.nav ul:after {
clear: both;
}
.nav ul>li {
position: relative;
}
.nav a {
display: block;
padding: 10px 20px;
line-height: 1.2em;
color: #fff;
border-left: 1px solid #595959;
text-decoration: none;
color: #FFF;
}
.nav a:hover {
text-decoration: none;
background-color: rgba(242, 93, 38, 0.8);
}
.nav li ul {
background: #273754;
}
.nav li ul li {
width: 200px;
}
.nav li ul a {
border: none;
}
.nav li ul a:hover {
background: rgba(0, 0, 0, 0.2);
}
.nav li ul {
position: absolute;
left: 11vw;
top: 0;
z-index: 1;
visibility: hidden;
opacity: 0;
filter: alpha(opacity=0);
-webkit-transition: 200ms ease;
-moz-transition: 200ms ease;
-o-transition: 200ms ease;
transition: 200ms ease;
}
.nav ul>li:hover ul {
visibility: visible;
opacity: 1;
filter: alpha(opacity=100);
}
span.caption {
display: none;
}
<body>
<div class="landing">
<div class="welcome">
<div id="mug">
</div>
<h1>Welcome to my personal Website!</h1>
<h2>On this site you will find a information about the person, the profession and other stuff about me. Enjoy!</h2>
</div>
<!-- cleaned up the navigation, nested it within the rest of the page, and adjusted the css -->
<nav class="nav">
<ul>
<li>
<img src="https://imageshack.com/i/potj2pVwp"> <span class="caption">Home</span>
</li>
<li>
<img src="https://imageshack.com/i/pntQ9nVMp"><span class="caption">About Me</span>
<ul>
<li>What I do</li>
<li>Motorcycling</li>
<li>Music</li>
</ul>
</li>
<li>
<img src="https://imageshack.com/i/po4WFq6Yp"><span class="caption">Professional</span>
<ul>
<li>Current Employment</li>
<li>Working Experience</li>
<li>Education</li>
</ul>
</li>
<li>
<img src="https://imageshack.com/i/pmcfm7Kbp"> <span class="caption">Projects</span>
<ul>
<li>Subnav Item</li>
<li>Subnav Item</li>
<li>Subnav Item</li>
</ul>
</li>
<li>
<img src="https://imageshack.com/i/pnM0Fmgrp"> <span class="caption">Misc</span>
<ul>
<li>Subnav Item</li>
<li>Subnav Item</li>
<li>Subnav Item</li>
</ul>
</li>
<li>
<img src="https://imageshack.com/i/poqRnk6ap"> <span class="caption">Testing</span>
<ul>
<li>Subnav Item</li>
<li>Subnav Item</li>
<li>Subnav Item</li>
</ul>
</li>
<li>
<img src="https://imageshack.com/i/pmc8tts9p"> <span class="caption">Contact</span>
</li>
</ul>
</nav>
</div>

Positioning a circle underneath text in CSS

I'm trying to position a 5px x 5px circle in the centre of the underneath of the links in a nav to indicate which page the user is currently on, but I'm not sure how I should be going about this.
Currently I have this: Image
I am trying to do this:
Image
This is the code:
<ul id="nav-menu">
<li class="nav-menu-item">
Our work
</li>
<li class="nav-menu-item">
What we do
</li>
<li class="nav-menu-item">
Blog
</li>
<li class="nav-menu-item">
Contact
</li>
</ul>
nav {
height: 70px;
background: #fff;
display: flex;
align-items: center;
padding: 0 75px;
}
#nav-logo-link {
flex: 1;
}
#nav-logo {
height: 35px;
}
#nav-menu {
list-style: none;
padding: 0;
white-space: nowrap;
}
#nav-menu > li {
display: inline;
margin: 0 10px;
}
#nav-menu > li > a {
text-decoration: none;
color: #000;
}
I have tried putting an <i> element within the <li> and then positioning it as absolute, and whilst I can put it down at the correct height (since the nav height is static), when I set it as left: 0;, it jumps to the left hand side of the entire nav. I tried putting a <div> within the <li> but that didn't do anything.
Any ideas?
You can use an after pseudo element to do this along with a UTF8 character, see the following fiddle.
Edit: I'm assuming you want this to happen when the user clicks a link and is then on that page hence why I've used a data attribute, the previous answer does it on hover.
I've made three amendments to your code here:
Added position: relative; to your anchors, this allows me to use position: absolute in the next css block:
nav-menu > li > a {
text-decoration: none;
color: #000;
position: relative;
}
Added the pseudo element absolutely positioned at 50% of the anchor width, I've also set it's width and then centred it within that by using a negative margin of half its width. This relies on you setting the selected item with a little bit of javascript, see point 3. You may want to adjust the colour:
nav-menu > li > a[data-selected=true]:after {
content: "\25CF";
position: absolute;
top: 1.1em;
left: 50%;
width: 10px;
margin-left: -5px;
color: cadetblue;
}
Added a data-selected="true" attribute to the selected anchor, you need to do this in javascript as a different anchor is selected. This allows the css in step 2 to select the right anchor.
This can be done by adding a nav-menu-item:after{ rule which adds a circle after the <li> tag then set the display for the #nav-menu > li { to inline-block and you should get the desired result on hover
nav {
height: 70px;
background: #fff;
display: flex;
align-items: center;
padding: 0 75px;
}
#nav-logo-link {
flex: 1;
}
#nav-logo {
height: 35px;
}
#nav-menu {
list-style: none;
padding: 0;
white-space: nowrap;
}
#nav-menu > li {
display: inline-block;
margin: 0 10px;
}
#nav-menu > li > a {
text-decoration: none;
color: #000;
}
.nav-menu-item:hover:after {
content: "\25CF";
display: block;
opacity: 1;
margin-left: auto;
margin-right: auto;
}
.nav-menu-item::after {
content: "\25CF";
opacity: 0;
color: #1ba9b3;
display: block;
margin-left: auto;
margin-right: auto;
width: 5px;
height: 5px;
}
<nav>
<ul id="nav-menu">
<li class="nav-menu-item">
Our work
</li>
<li class="nav-menu-item">
What we do
</li>
<li class="nav-menu-item">
Blog
</li>
<li class="nav-menu-item">
Contact
</li>
</ul>
</nav>
If you want to show the dot when selected you can use javascript to set the class to be <li class="nav-menu-item selected"> then in css you would change .nav-menu-item:hover:after { to .nav-menu-item.selected:after {
Hope this helps!

Align ::before pseudo element with list item contents

I have a side navigation I'm building, and it needs to be fully keyboard accessible as well as have the links themselves nested in list items. Everything else works fine, but I've been asked to produce a hover animation which is an animated pseudo element.
I've got most of that done, being the structure of the nav and the animation. However, the animated ::before element always seems to show up at the top of the list item, and will push the anchor down by whatever height I've set. I've tried both height:inherit and 100% for the ::before content, but that doesn't work.
The ideal behavior is that the content would animate in-line with the anchor from the bottom to the top, and simply occupy the height (100%) of the li element with my specified width.
Note: I can't use list-style:none because a screen reader will not identify how many items are in the list.
EDIT: took out the button element. Just focusing on the menu.
Here's a CodePen: https://codepen.io/anon/pen/EZbLMY
Feel free to fork it.
HTML:
<div id="mySidenav" class="sidenav" aria-label="Menu" aria-hidden="true">
<nav>
<ul role="navigation">
<li id="about">About</li>
<li id="services">Services</li>
<li id="clients">Clients</li>
<li id="contact">Contact</li>
</ul>
</nav>
</div>
CSS:
/* The side navigation menu */
.sidenav {
height: 100%;
width: 250px;
position: absolute;
z-index: 2;
top: 0;
background-color: #fff;
overflow-x: hidden;
padding-top: 60px;
}
.showOverlay{
display: block !important;
}
/* The navigation menu links */
.sidenav a{
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #000;
display: block;
width: inherit;
}
/*Clean up space between elements*/
.sidenav nav ul{
padding: 0;
margin: 0;
}
.sidenav nav ul li::before{
content:"";
width: 4px;
height: 0px;
display: block;
background-color: #000000;
}
.sidenav nav ul li:hover::before{
height: 20px;
transition: 0.3s ease-out;
padding-left: 2px;
}
Change the position of the ::before element to absolute. Also, to line things up neatly, add a position:relative to the li elements.
/* The side navigation menu */
.sidenav {
height: 100%;
width: 250px;
position: absolute;
z-index: 2;
top: 0;
background-color: #fff;
overflow-x: hidden;
padding-top: 60px;
}
.showOverlay {
display: block !important;
}
/* The navigation menu links */
.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #000;
display: block;
width: inherit;
}
/*Clean up space between elements*/
.sidenav nav ul {
padding: 0;
margin: 0;
}
/*Make li elements relative, so that the before elements are positioned correctly*/
.sidenav nav ul li {
position: relative;
}
.sidenav nav ul li::before {
content: "*";
width: 4px;
height: 0px;
display: block;
background-color: #000000;
/*Add absolute positioning*/
position: absolute;
top: 8px;
}
.sidenav nav ul li:hover::before {
height: 20px;
transition: 0.3s ease-out;
padding-left: 2px;
}
<div id="mySidenav" class="sidenav" aria-label="Menu" aria-hidden="true">
<nav>
<ul role="navigation">
<li id="about">About</li>
<li id="services">Services</li>
<li id="clients">Clients</li>
<li id="contact">Contact</li>
</ul>
</nav>
</div>

CSS Dropdown menu with images - Keep subitems from disappearing?

I have created a CSS dropdown menu based on four images. When I hover over the first one the three others appear using :hover on the first <li> which sets opacity of the three others to 1. However, I cannot accomplish that the three others remain present when I scroll down to them.
Here is the live site and this is the CSS and HTML:
ul {
list-style: none;
width: 200px;
padding-left: 30px;
margin: 0px;
border: none;
float: left;
margin-right: 5px;
z-index: 1;
position: relative;
}
li#dropdown {
height: 510px;
padding: 0px;
margin: 0px;
opacity: 0;
transition: all 0.5s;
}
li:hover + li#dropdown {
opacity: 1;
/* display the dropdown */
}
li#dropdown:hover li#dropdown {
opacity: 1;
/* display the dropdown */
}
<ul>
<li id="noten">
<img id="noten" src="http://www.placehold.it/100">
</li>
<li id="dropdown">
<img id="pitten" src="http://www.placehold.it/100" naptha_cursor="region">
<img id="muesli" src="http://www.placehold.it/100" naptha_cursor="text">
<img id="fruit" src="http://www.placehold.it/100" naptha_cursor="text">
</li>
</ul>
You need to place the hover state on the dropdown element using: li:hover + li#dropdown, li#dropdown:hover.
Also, here are two examples that prevent the menu being displayed unwanted when the mouse is hovered underneath your dropdown.
Working Examples
#1 - using pointer-events
Best thing - transition fade-in and fade-out
Limitation - supported in all major browsers and IE 11 - If you must have IE10 and below support, this is possible. Depending on your requirements, this may not be a limitation. Here are two questions that discuss various alternatives for IE 10 and below - Question One and Question two.
pointer-events: none on the ul prevents the dropdown activating when it is not visible. It is cancelled on hover with pointer-events: auto
Hover over the image and the images that appear underneath on hover
ul {
list-style: none;
width: 200px;
padding-left: 30px;
margin: 0px;
border: none;
float: left;
margin-right: 5px;
z-index: 1;
position: relative;
pointer-events: none;
}
li#dropdown {
height: 510px;
padding: 0px;
margin: 0px;
opacity: 0;
transition: all 0.5s;
position: absolute;
}
li#noten {
pointer-events: auto;
}
li:hover + li#dropdown, li#dropdown:hover {
opacity: 1;
pointer-events: auto;
/* display the dropdown */
}
<ul>
<li id="noten">
<img id="noten" src="http://www.placehold.it/100">
</li>
<li id="dropdown">
<img id="pitten" src="http://www.placehold.it/100" naptha_cursor="region">
<img id="muesli" src="http://www.placehold.it/100" naptha_cursor="text">
<img id="fruit" src="http://www.placehold.it/100" naptha_cursor="text">
</li>
</ul>
#2 Using translate to slide drop down up and then down on hover
Limitation - opacity does not fade out, only in
ul {
list-style: none;
width: 200px;
padding-left: 30px;
margin: 0px;
border: none;
float: left;
margin-right: 5px;
z-index: 1;
position: relative;
}
li#dropdown {
height: 510px;
padding: 0px;
margin: 0px;
opacity: 0;
transition: opacity 0.5s;
position: absolute;
transform: translateY(-100%);
}
li:hover + li#dropdown, li#dropdown:hover {
opacity: 1;
transform: translateY(0);
/* display the dropdown */
}
<ul>
<li id="noten">
<img id="noten" src="http://www.placehold.it/100">
</li>
<li id="dropdown">
<img id="pitten" src="http://www.placehold.it/100" naptha_cursor="region">
<img id="muesli" src="http://www.placehold.it/100" naptha_cursor="text">
<img id="fruit" src="http://www.placehold.it/100" naptha_cursor="text">
</li>
</ul>
Approximately so:
<div class="menu_img_container">
<img src="pic.jpg">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
<style>
.menu_img_container
{
display: inline;
position: relative;
}
.menu_img_container ul
{
display: none;
position: absolute;
top: 50px;
left: 0;
}
.menu_img_container:hover ul
{
display: block;
}
</style>
You can prolong the hover effect using transitions http://jsfiddle.net/zn6j5xvv/
HTML
<div id="a">a
<div id="b">
<img src="http://placekitten.com/300/301"/>
</div>
CSS
#a{
width:50px;
height:50px;
background-color:green;
transition:0s 100s;
}
#b{
width:200px;
height:200px;
opacity:0;
transition:0s 100s;
}
#a:hover #b{
opacity:1;
transition:0s;
}
this should do the trick, just update these three parts of your css
li#dropdown {
height: 510px;
padding: 0px;
margin:0px;
display:none;
transition: all 0.5s;
}
li:hover + li#dropdown {
display: block; /* display the dropdown */
}
li#dropdown:hover{
display:block;
}
It's a little tricky, because you want to have a nice opacity transition. It would be much simpler with just display: none. But we can still make it work with transition if you use combination of position: absolute for hiding items and opacity for transition.
Move :hover selector from li to entire ul
ul:hover > li#dropdown {
opacity: 1;
top: auto;
}
li#dropdown {
height: 510px;
padding: 0px;
margin: 0px;
opacity: 0;
transition: all 0.5s;
position: absolute; /* move items far off the screen */
top: -1000px;
}
And it should work. So the idea is that #dropdown menu is moved outside the screen with position absolute, which make it impossible to hover over "invisible" items, so they don't appear on white area hover. However on the first item hover you bring dropdown back with top: auto which allows hovering it again. Transition will also work.