The dropdown menu was (edit: is -> was) overflowing out of the screen (width-wise), after fixing it I’d like to know how to control the position of it better and how to make it so that it’s position isn’t relative to the div(.top-menu), but the dropdown button.
I’m pretty much a beginner with CSS and HTML as a whole, basically I don’t understand the position/display values but that’s not the point. Basically I’ve been reading W3School’s documentation I guess? I saw the Horizontal navbar page, and decided to change how I did it before (using 1 big div element for the whole menu and then little div elements inside).
Afterwards, I tried to make a dropdown menu basically just copying how they did it and noticed a problem, as said before the dropdown menu is going out of the width of the screen and some other problems aswell.
CSS:
.top-menu {
height: 1lh;
width: 100%;
background-color: cadetblue;
position: fixed;
top: 0%;
left: 0%;
text-align: end;
line-height: 6;
}
.top-menu a {
padding-right: 25px;
font-family: sans-serif;
}
ul.navbar {
float: right;
list-style-type: none;
margin: 0;
padding-right: 10px;
width: auto;
}
li {
display: inline;
padding: 10px 12px;
background-color: #dddddd;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #BBBBBB;
min-width: 160px;
max-width: 100vw;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
padding: 12px;
z-index: 1;
right: 0;
top: 100%;
box-sizing: border-box;
transform: translateX(10%);
}
.dropdown:hover .dropdown-content {
display: block;
width: fit-content;
text-align: center;
}
HTML
<div id="top-menu" class="top-menu">
<ul class="navbar">
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
<li>e</li>
<li>
<div class="dropdown">
<span>Mouse over me</span>
<div class="dropdown-content">
<p>Hello World!</p>
</div>
</div>
</li>
</ul>
</div>
There’s also some JS code I copied from ChatGPT, but I don’t think it does anything so I didn’t include it.
The overflow problem is (kind of) fixed, basically the question is how would I control it’s position (except with translateX()
because I want it to be aligned), width/height.
Also I would like to know; I noticed the top: 0%;
, and saw that the menu is actually positioning itself relative to the whole top bar instead of the dropdown button. Is there any fix?
2
Answers
Change
display: inline-block;
todisplay: inline;
. This will causediv.dropdown
to take up only as much space as it’s children. So the dropdown will appear at the bottom edge of "Mouse over me".Two things to update:
<li>
elements is affecting the alignment/positioning of the.dropdown-content
element. The.dropdown-content
needs to consider this when being positioned..dropdown
display should beinline
.Working Example:
I only modified the
.dropdown
and.dropdown-content
blocks:A Better Approach:
It is better if you do not add padding to the
<li>
elements, but instead to the children within the<li>
elements. You will then have an easier time aligning the dropdown. See the example below.Note: I did update the css to use flex-box (more modern approach). You can just focus on how I changed where the padding is applied. It would still apply to your original code.