Implement a Left-Sliding Sidenav in MaterializeCSS 1.x
Add a MaterializeCSS sidenav that slides from the left, triggered by a button, with vanilla JavaScript initialization and verification steps.
09 Jun 2026, 17:21 UTC

Problem and useful takeaway
You need a navigation drawer that slides in from the left, but adding MaterializeCSS markup alone does not make it interactive. The panel stays hidden until JavaScript initializes it.
Use MaterializeCSS 1.0.0 or later with vanilla JavaScript. Call M.Sidenav.init() with { edge: 'left' } after the DOM loads. jQuery is not required for version 1.x.
Prerequisites
- A text editor and a modern browser.
- MaterializeCSS 1.0.0 CSS and JavaScript, loaded by CDN link or local file.
- Permission to edit the target HTML file.
Add the sidenav markup
Place this HTML where the trigger button should appear. The data-target value must match the sidenav element's id exactly.
<a href="#" data-target="slide-out" class="sidenav-trigger btn">Menu</a>
<ul id="slide-out" class="sidenav">
<li><a href="#dashboard">Dashboard</a></li>
<li><a href="#settings">Settings</a></li>
<li><a href="#profile">Profile</a></li>
</ul>
The sidenav-trigger class styles the button. data-target="slide-out" tells MaterializeCSS which panel to open. The sidenav class on the ul identifies the drawer.
Initialize the component
Add this script after the MaterializeCSS JavaScript reference and before the closing </body> tag. The M global must exist before your code runs.
<script>
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('.sidenav');
M.Sidenav.init(elems, { edge: 'left' });
});
</script>
DOMContentLoaded ensures the HTML is parsed before querySelectorAll runs. edge: 'left' makes the drawer slide in from the left. Stating it explicitly avoids reliance on default behavior.
Expected checks
- Open the page and click the trigger button. The sidenav should slide in from the left and cover part of the main content.
- Click the close icon inside the drawer. It should close.
- Click the shaded area outside the drawer. It should close.
- Press Escape. The drawer should close.
- Resize the viewport below 600 pixels. The drawer should still open and close.
Open browser developer tools, select ul#slide-out, and confirm it gains an open class while visible. When closed, that class should disappear.
Diagnostic decisions
| Symptom | Likely cause | Check |
|---|---|---|
| Clicking the trigger does nothing | JavaScript ran before the DOM loaded, or M is undefined | Confirm the script tag appears after MaterializeCSS JS and inside DOMContentLoaded. In the console, type M; it should not be undefined. |
| Trigger opens the wrong panel or none | data-target does not match the sidenav id | Verify the trigger's data-target="slide-out" exactly equals the sidenav's id="slide-out", including case. |
| Drawer appears with no styling | CSS not loaded or class conflict | Confirm the MaterializeCSS CSS link loads with HTTP 200 in the Network tab. Avoid adding your own .sidenav rules that override component styles. |
Console shows M.Sidenav.init is not a function | Using 0.x jQuery syntax with a 1.x release, or JS file failed to load | Check the MaterializeCSS version. Version 0.x uses $('.sidenav').sideNav(); version 1.x uses M.Sidenav.init. |
Limitations
- This implementation requires JavaScript. Users with JavaScript disabled will not get drawer behavior.
- The drawer overlays content; it does not push the main content aside. A push effect requires custom CSS.
- MaterializeCSS 1.x is no longer actively maintained. Evaluate alternatives for new projects.
Practical verification
Open the page in a browser, open developer tools, and run this snippet in the console to confirm the instance exists:
var elems = document.querySelectorAll('.sidenav');
var instance = M.Sidenav.getInstance(elems[0]);
console.log(instance.isOpen);
Click the trigger, then run the snippet again. isOpen should change from false to true. If it stays false or throws an error, revisit the diagnostic table above.
Recovery
This procedure only adds markup and script tags. If the sidenav breaks the layout, remove the <ul class="sidenav"> block and the initialization script, then reload the page to return to the previous state.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.