Here is my website: https://rustinpeace.io
In mobile view, I have a navigational link that has negative margins. It displays when touching a button.
My issue is this is causing my mobile view to horizontally scroll.
Is there a way I can keep the navigational bar with negative margins and stop horizontal scrolling?
This is the first time I’ve built a website
@media(max-width: 800px){
html{
overflow-x:auto !important;
}
body{
overflow-x:auto !important;
}
The above has no effect.
2
Answers
Instead of using
Just use overflow:hidden
this should work .
Leave the
overflow
on thebody
alone; just keep the default styles from the browser.Since the nav’s parent element is already
100vh
tall, the easiest solution is to simply control the overflow on that element. I have put together a simplified version of your code below as a demo.One thing to note: in the event that someone is using a keyboard to navigate your site on these smaller screen sizes, you’re going to run into trouble with this nav design. As soon as someone hits the tab key, the browser is going to yoink the off-screen link into the viewport, which will totally mess up your JS logic. The solution to that problem is:
<button>
elements around your open/close icons, so they can be reached via the tab key.tabindex="-1"
while they are hidden, so the only thing a keyboard user can get to initially is the button to open the menu. When you click the open button, remove thetabindex
attribute from the links, or update it totabindex="0"
. Then put it back totabindex="-1"
when you close the menu.