Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop horizontal scrolling with translate transform

Using Bootstrap 4, I have a sidebar that I want to show/hide when a button is clicked. I have this working perfectly on desktop, it hides the sidebar and the main content area takes up the rest of the space:

HTML:

<a class="sidebar-toggle" href="#"><i class="fa fa-bars"></i></a>

<div class="d-flex wrapper">
    <div class="sidebar sidebar-dark bg-dark">
        <ul class="list-unstyled">
            <li><a href="#"><i class="fa fa-fw fa-link"></i> Menu Item</a></li>
            <li><a href="#"><i class="fa fa-fw fa-link"></i> Menu Item</a></li>
            <li><a href="#"><i class="fa fa-fw fa-link"></i> Menu Item</a></li>
        </ul>
    </div>

    <div class="content m-4">
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently
        with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </div>
</div>

CSS:

.sidebar {
    min-width: 220px;
    max-width: 220px;
    min-height: calc(100vh - 56px);
}
.wrapper-toggled .sidebar {
    margin-left: -220px;
}

JS:

$(document).on('click', '.sidebar-toggle', function (event) {
    event.preventDefault();
    $('.wrapper').toggleClass('wrapper-toggled');
});

My problem is that I want slightly different behavior on mobile. By default, the sidebar should be hidden, and shown when the button is clicked. When the sidebar is shown, I want it to shift everything to the right. I have this working, but it is causing a horizontal scrollbar in order to show the rest of the cut off .content content.

Here is the code for that:

@media (max-width: 768px) {
    .sidebar {
        margin-left: -220px;
    }
    .wrapper-toggled {
        transform: translate(220px, 0);
    }
}

I've tried adding overflow-x: hidden to the body, and the .wrapper class, which did not work.

Also, it seems to screw up the total viewport height on mobile when the sidebar is shown. The page height grows when the sidebar is shown.

Is there a better way of doing this? How do I correct the issue?

like image 940
stardust4891 Avatar asked Oct 21 '25 14:10

stardust4891


1 Answers

Adding overflow-x: hidden to both the body AND html seems to have resolved the issue.

like image 61
stardust4891 Avatar answered Oct 23 '25 05:10

stardust4891



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!