I have a dropdown menu whose position should be depending on the height of the header. The height of the header varies by content, the font-size and the padding set by media queries in dependence of viewport height.
HTML:
<header>
<p>Example</p>
<nav>
<a>Link</a>
<a>Link</a>
<a>Link</a>
</nav>
</header>
CSS:
body {
background-color: white;
}
header {
background-color: grey;
padding: 1rem 0;
position: fixed;
width: 100%;
}
@media(max-height: 500px) {
header {
padding: 0;
}
}
nav {
background-color: #CCC;
position: absolute;
top: 82px;
width: 100%;
}
a {
border-bottom: 1px solid white;
display: block;
padding: 0.75rem;
}
Imagine nav is the folded out dropdown menu.
Fiddle: https://jsfiddle.net/mjufc63b/2/
If you decrease your window's height below 500 pixels it appears a white gap between header and nav, which should not be so. Also if you change your browser's font size.
Is there a solution to get the dropdown menu always below the header independent of the header's height and also above the other content (what absolute positioning does) without using javascript and wihout moving the nav outside header?
navshould automatically move to top and close the gap without the need to redefine the margin with thetopcss property in media query. In this example I could do it, yes, but I can't define thetopmargin if the headers height changes because of the font-size or height of content.