CSS元素覆盖问题咨询:导航栏元素重叠异常
Hey there, let’s work through this overlapping issue with your flex-based navigation bar step by step:
1. Fix Conflicting Width Rules on the Logo
Your #logo has width: auto defined, but it’s also using the .element class which sets width: 100px—this creates a conflict that can force the logo to shrink awkwardly or push other elements into each other.
Quick fix:
Either remove the .element class from the logo image, or override the width specifically for the logo in your CSS:
#logo { height: 125px; width: auto; margin-right: 2em; /* Add the spacing here instead of relying on the .element class */ }
Then update your HTML to clean up the logo’s class:
<div id="banner" class="banner-header"> <a class="element" href="/">Home</a> <a class="element" href="/shop/">Shop</a> <a class="element" href="/our-story/">Our Story</a> <img id="logo" src="http://www.jamaicacannabisesta..." alt="Brand Logo"> </div>
2. Improve Flex Container Spacing & Behavior
If the banner container doesn’t have enough space or proper overflow handling, flex items can overlap. Update your #banner styles to fix this:
#banner { display: flex; justify-content: center; align-items: center; z-index: 99999; position: relative; /* Ensures z-index works correctly (needs positioned context) */ width: 100%; /* Makes the banner take full available width */ gap: 2em; /* Replaces margin-right on individual elements for cleaner, consistent spacing */ padding: 0 1em; /* Adds horizontal padding to prevent edge clipping on small screens */ overflow-x: auto; /* Lets users scroll horizontally if elements don’t fit on mobile */ }
Using gap instead of margin-right avoids the extra spacing on the last element and keeps your layout more predictable.
3. Double-Check Parent Container Constraints
If the parent element of #banner has a fixed width or overflow: hidden set, it might be squeezing the navigation elements into each other. Make sure the parent container allows enough space for the full navigation bar to display.
After applying these tweaks, your navigation elements should stop overlapping. Be sure to test it on different screen sizes to keep it responsive!
内容的提问来源于stack exchange,提问作者user979331

