Now, just to write the answer I wanted to read (and hopefully can help other people find out what just happened)
Quick fix
Removing a field from menuContainer and adding it to topContainer will “fix” the problem visually and “look” as you want, but the code is still bad, because the reason for this behavior still exists.
Why does the marker trigger behavior?
The problem (which we will discuss below) starts because the margin pushes the menu down, and when the content in top pressed, it will find the space in which it appears.
Problem
Part of the code causing this mess:
#navMenu a { float: left; }
The problem here is that element a got all the properties for no good reason. And here what happens is that it destroys the parent element. Collapse makes the whole element "invisible" and useless.
What the float does is that it removes the standard block -property, which usually makes up the behavior for the li elements, and this leads to the fact that the a elements are collected on the same line. This is the desired effect, but it also destroys the parent element li , which looks like this:
#navMenu li { display: inline; }
The collapse continues up to ul , where the fields are intentionally collapsed with the margin: 0px; attribute margin: 0px; . menuContainer collapses in all places, but on top. Elements a float on top of nothing. So for all the magenta, he also collapsed.
This chain reaction collapses all the menus in topContainer , where top now placed more or less on top of a elements but repelled by their attributes. Elements a are the only things that have not collapsed.
Imagine that everything that exists in the browser is free floating elements of a , and they do not have a field or addition to the parent div.
Here is an attempt to solve this problem:
#top { clear: left; }
However, what makes clear is that it checks the floating content at its specified position (in this case, to the left) and tells it to “stay away from it,” moving the edge of the border down, but a completely collapsed li , and all the other parents also collapsed, so there is no border line, but a folded bottom menu-container , where the top edge ends. As topContainer also collapses, the menuContainer field moves to where it can fit; outside topContainer .
So, the a elements do not work with anything, but inside the topContainer . The border made clear occurs where it would even be without it: at the top of the topContainer . This is why you can see the window for the top beginning of the content where the a elements start, allowing a "hello" to appear next to them.
Decision
The solution for the best version is as follows:
body { background-color: #c2b074; color: #40371c; margin: 0px; font-family: Calibri; } #topContainer { margin: 4em auto 0 auto; } #menuContainer { margin: 0 auto; width: 300px; } #navMenu { margin: 0px; padding: 0px; } #navMenu ul { margin: 0px; padding: 0px; list-style-type: none; } #navMenu li { width: 3.5em; text-align: center; display: inline-block; padding: 0.3em; margin: 0 0 0 1.5em; background-color: #d4cbab; border-radius: 10px 10px 0px 0px; } #navMenu a { text-decoration: none; color: #40371c; padding: 0; margin: 0; } #navMenu li:hover{ background-color: #efefef; color: #40371c; } #top { width: 350px; height: 100px; margin: 0 auto; background-color: #efefef; border-radius: 10px 10px 0px 0px; }
<div id="topContainer"> <div id="menuContainer"> <div id="navMenu"> <ul> <li> <a href="index.html" id="current">Home</a> </li> <li> <a href="#" id="contact">Contact</a> </li> <li> <a href="#" id="cart">Cart</a> </li> </ul> </div> </div> <div id="top"> Hello </div> </div>
I deleted the unnecessary code. Here you can see the best structure in which we use li elements as blocks that should be displayed in line :
#navMenu li { display: inline-block; }
This is the place of the soul where you need to determine that the list items (or menu buttons) should be displayed horizontally in one line.
And just for the sake of this, I left a margin on the menuContainer to show you that it does not matter if you put it there or where it should be; in topContainer .