Why HTML Opens Without CSS or Images: Fixing Missing Local Assets
Troubleshoot relative paths, base URLs, file URLs, CORS, case sensitivity and self-contained export.
Why Local HTML Files Lose CSS and Images
You open an HTML file directly from your desktop. The browser displays it, but styles are absent, images show as broken icons, and the layout collapses. The HTML document loaded, but every referenced asset failed. This happens because browsers resolve file paths differently when loading from file:// URLs versus an HTTP server. The fix requires understanding how the browser locates linked resources in a local context.
Check Relative Paths Against the File’s Actual Directory
When a browser loads a page via file:///Users/name/project/page.html, it resolves every relative URL against the directory containing that file—not the system root, not a parent folder, and not any other location.
A reference like <img src="images/photo.jpg"> means the browser looks for a folder named images inside the same directory as page.html. If that folder is elsewhere, the asset fails silently.
Observable check: Open developer tools (F12), select the Network tab, and reload the page. Look for entries with status 404 or (failed). Click one and examine the full file:// path the browser attempted. Compare that path to where the file actually exists on disk. If they do not match, move the asset into the expected location or correct the src attribute.
Limitation: The Network tab only shows requests the browser attempted. If a <base> tag redirects all requests to a remote domain, local 404s may not appear. Check the Console tab for blocked or failed requests as well.
Remove Leading Slashes That Point to the File System Root
A path beginning with / instructs the browser to resolve from the root of the current origin. On a web server, that root is the domain’s public directory. On a local file, it is the file system root—C:\ on Windows or / on macOS and Linux.
<link rel="stylesheet" href="/css/style.css">
This tells the browser to look for file:///css/style.css, which almost certainly does not exist.
Observable check: In the Elements panel of developer tools, locate the <link> or <img> element. The browser may display a computed absolute URL in a tooltip or the Styles pane. If it starts with file:///C:/css/ or file:///css/, the leading slash is the problem.
Fix: Change /css/style.css to css/style.css or ./css/style.css. The same applies to src attributes on images, scripts, and iframes. Do not add a leading dot-slash to paths that already begin with a directory name—./images/photo.jpg and images/photo.jpg are equivalent.
Limitation: This fix only applies to local viewing. If the HTML must also work on a web server where assets live at the root, you need environment-conditional paths or a local server.
Inspect the <base> Element Before Debugging Any Other Path
A <base> tag in the document <head> overrides the base URL for every relative URL in the page. If present, all relative paths resolve against its href value instead of the file’s directory.
<base href="https://example.com/">
With this tag, <img src="logo.png"> resolves to https://example.com/logo.png, not the local file. The browser ignores your local directory structure entirely.
Observable check: View the page source (Ctrl+U) and search for <base. If found, note its href value. Then open the Console tab. If you see network errors attempting to fetch assets from a remote domain that match the href value, the base tag is redirecting all requests.
Fix: Remove the <base> tag for local use. If you must keep it, set href to the local directory using a file:// URL: <base href="file:///Users/name/project/">. This is fragile and platform-specific. Removal is the safer option.
The HTML specification states: “If the document has a base element that has an href attribute, then the base URL is the result of parsing that attribute’s value.” There is no fallback to the document’s location when a base URL is present (HTML spec — Base URLs).
Match File Name Casing Exactly to the Disk
Windows and macOS default file systems treat Photo.jpg and photo.jpg as the same file. Linux file systems (ext4, XFS, Btrfs) do not. An HTML file authored on Windows with mixed-case references will break when opened on Linux or deployed to a case-sensitive server.
Observable check: On macOS or Linux, run ls -1 in the asset directory and compare the exact output to every src and href value in the HTML. On Windows, open PowerShell and run Get-ChildItem -Name for the same comparison. Any difference in letter case is a future failure point.
Fix: Rename all asset files and directories to lowercase. Update all HTML references to match exactly. Do not rely on the file system to handle case insensitivity—it will not transfer across systems.
Limitation: This check only catches mismatches you can see. If assets are generated by a build tool that preserves original casing, the mismatch may be in the tool’s output configuration, not the HTML.
Recognize That CORS Blocks Remote Assets for Local Files
When a file:// page requests a resource from https://cdn.example.com/font.woff2, the browser sends an Origin: null header. Many CDNs and servers reject requests with a null origin. The Console shows an error like:
Access to fetch at 'https://cdn.example.com/font.woff2' from origin 'null' has been blocked by CORS policy
This is a browser security restriction. No HTML attribute, meta tag, or JavaScript call can override it from the client side.
Observable check: Filter the Console tab for “CORS” or “blocked.” If the error references Origin: null, the request came from a local file context.
Solutions (choose one):
- Download the remote asset and reference it locally with a relative path.
- Serve the HTML through a local HTTP server (
python3 -m http.server,npx serve, or similar), which provides a proper origin. - Use a browser launched with CORS-disabled flags for testing only. This is unsafe for general browsing and does not represent how other users will experience the file.
Limitation: Some servers intentionally allow null origins. If the asset loads despite a file:// origin, the server’s CORS policy permits it. Do not assume this will work across all resources.
Embed Assets to Eliminate Path Dependencies Entirely
A self-contained HTML file inlines all CSS, JavaScript, and images. Styles live inside <style> tags. Scripts live inside <script> tags. Images use data URIs:
<img src="data:image/png;base64,iVBORw0KGgo...">
No external files are referenced. Path resolution, case sensitivity, CORS, and base URL misconfiguration become irrelevant.
Observable check: Search the HTML source for http://, https://, and src=" or href=" values that point to external files. If none exist outside of data URIs, the file is self-contained.
Trade-off: Base64 encoding increases image size by roughly 33%. For pages with many large images, the resulting file may become impractically large. For text-heavy documents with small assets, the size increase is negligible.
Limitation: Self-contained files cannot reference externally hosted fonts unless those fonts are also embedded as base64 data URIs within a @font-face declaration. This requires extracting and encoding the font files manually or through a build tool.
Fix Asset Paths Before Any Conversion Step
If you feed an HTML file with broken asset references into a PDF, DOCX, or image converter, the output will also lack those assets. Converters resolve paths the same way browsers do—often with fewer fallbacks for missing resources.
Observable check: Open the HTML file in a browser and confirm every visual element renders correctly. Scroll the full page. Check the Console for any remaining 404 or CORS errors. Only after the page renders completely should you proceed with conversion.
Limitation: Some converters use headless browsers internally and may handle file:// paths differently than your desktop browser. If the converter produces missing assets despite correct local rendering, test with a local HTTP server to eliminate file://-specific behavior.