基于MaterializeCSS的网站仅窗口调整时响应,移动设备加载无响应求助
Hey there, let's troubleshoot why your MaterializeCSS site only behaves responsively when resizing the browser, but not on initial load across mobile devices, tablets, or dev tool simulators. This is a frustrating quirk, but here are the most likely fixes to resolve it:
Materialize relies entirely on this tag to tell mobile devices how to render your page correctly. If it's missing or misconfigured, devices will treat your site like a desktop-width page and scale it down—breaking your column layout.
Make sure this exact tag is placed inside your <head> section:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Without it, your .m4, .s12, etc., classes won't trigger their media queries on initial load.
Materialize's layout logic (and many components) require JavaScript to calculate column sizes. If your initialization code runs before the DOM is ready, it can't properly apply responsive rules to elements that haven't loaded yet.
Use one of these methods to delay initialization:
- Vanilla JS approach:
document.addEventListener('DOMContentLoaded', function() { // Auto-initialize all Materialize components M.AutoInit(); // Or target grid-specific initialization if needed // M.Grid.init(document.querySelectorAll('.row')); }); - jQuery approach (if you're using jQuery):
$(document).ready(function(){ M.AutoInit(); });
Pro tip: Make sure jQuery (if used) loads before Materialize's JS file.
If your custom CSS loads before Materialize's core CSS, your styles might accidentally override Materialize's responsive media queries for columns.
Stick to this load order in your <head>:
- Materialize CSS file
- Your custom CSS file
Old cached CSS/JS files can prevent new responsive rules from applying on initial load. Try:
- For desktop browsers: Force a hard refresh with
Ctrl+Shift+R(Windows/Linux) orCmd+Shift+R(Mac) - For mobile devices: Clear your browser's cache and site data entirely before reloading
If you have custom JS that modifies container/column widths, hides elements, or manipulates styles on page load, it might interfere with Materialize's initial layout calculation.
Temporarily remove your custom JS code and test the site—if the responsive behavior works, gradually reintroduce your code to find the conflicting part.
Mismatched CSS and JS versions can cause unexpected layout bugs. Ensure you're using the same version for both files (e.g., if you're using Materialize v1.0.0 CSS, use the v1.0.0 JS file too).
If none of these fix the issue, open your browser's dev tools (F12) and check:
- Are the
.m4/.s12classes correctly applied to your column elements? - Do the media queries for these classes show up as active in the "Styles" tab when using a mobile/tablet simulator?
内容的提问来源于stack exchange,提问作者Petr

