Flask页面加载时CSS不生效,Chrome开发者工具编辑后正常
Verify your HTML uses Flask's
url_forto link the CSS file correctly, and ensure the CSS is placed in the project'sstaticfolder. Example:<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">Open Chrome DevTools' Network tab to check if the CSS file loads with a 200 OK status. A 404 error means your file path is incorrect.
Perform a hard refresh (Ctrl+Shift+R on Windows/Linux, Cmd+Shift+R on Mac) to bypass residual browser cache that a regular clear might miss.
Inspect the
.hidden-boxelement in DevTools to check for conflicting styles. Look for higher-specificity rules (like ID selectors or inline styles) that could be overriding your class. Even with!important, inline styles take precedence unless you add!importantto them too.Confirm your Flask app runs in development mode with
app.run(debug=True)—this disables static file caching, which can cause old CSS to persist.Access the CSS file directly via its URL (e.g.,
http://localhost:5000/static/style.css) to ensure it serves the latest code. If it shows outdated content, double-check the file path and that you saved the CSS changes.Test the page in another browser (like Firefox) to rule out Chrome-specific bugs or interference from extensions (e.g., ad blockers, stylus tools).
If cache issues persist, add a unique query parameter to the CSS link to force the browser to fetch the latest version:
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}?v=1">
内容的提问来源于stack exchange,提问作者Jyoti

