sodcalculator

10 Tech How-To

View HTML Source Code in Safari: Step-by-Step

Guide 10-51247 · Updated · Step by step

Viewing the HTML source code of a webpage in Safari lets you see the underlying structure and markup that makes up the page. This is useful for learning how websites are built, debugging your own pages, or simply satisfying curiosity. You don’t need any special tools—just the Safari browser on your Mac or iPhone.

What You’ll Need

  • A Mac running Safari (any recent version) or an iPhone/iPad with Safari.
  • The webpage you want to inspect open in Safari.
  • For the Mac, the Safari menu bar and keyboard are helpful but not required.

Step-by-Step

On a Mac

  1. Open Safari and navigate to the webpage whose source code you want to view.
  2. Enable the Develop menu (first time only):
    1. Click Safari in the top-left menu bar, then select Settings (or Preferences in older versions).
    2. Go to the Advanced tab.
    3. Check the box next to Show features for web developers at the bottom. This adds a Develop menu to the menu bar.
  3. View the source using one of these methods:
    • Click Develop in the menu bar, then select Show Page Source.
    • Or press Option+Command+U on your keyboard.
    • Or right-click (or Control-click) on the page and choose Show Page Source from the context menu (if the Develop menu is enabled).
  4. A new tab or window will open showing the raw HTML source code. You can scroll through it, search within it (Command+F), or copy it.

On iPhone or iPad

  1. Open Safari and go to the webpage you want to inspect.
  2. Enable the Develop menu (first time only):
    1. Go to Settings > Safari.
    2. Scroll down to the Advanced section and tap it.
    3. Toggle on Web Inspector. This allows you to view source code when connected to a Mac.
  3. Connect to a Mac (required for viewing source on iOS):
    • Use a USB cable to connect your iPhone/iPad to your Mac.
    • On the Mac, open Safari and enable the Develop menu as described above.
    • In the Mac’s Safari menu bar, click Develop and you’ll see your device listed. Hover over it, then select the webpage you want to inspect.
  4. View the source: The Web Inspector will open on the Mac, showing the HTML source of the page as it appears on your iPhone/iPad. You can also use the Elements tab to see the live DOM.

Common Mistakes

  • Forgetting to enable the Develop menu: Without it, the right-click option and keyboard shortcut won’t work. You must enable it once in Safari’s settings.
  • Confusing source code with the rendered page: The source is the raw HTML, not the visual page. If you’re looking for styles or scripts, they may be in separate files linked in the source.
  • Expecting to see everything: Many modern websites use JavaScript to generate content dynamically. The initial source may not contain the final HTML you see on screen. Use the Web Inspector’s Elements tab to see the live DOM.
  • On iPhone, trying to view source directly: Safari on iOS doesn’t have a built-in “view source” button. You must use the Web Inspector via a Mac.

Tips

  • Use the keyboard shortcut Option+Command+U on Mac for quick access.
  • Search within the source using Command+F to find specific tags, classes, or text.
  • Save the source by pressing Command+S while viewing it, or copy and paste it into a text editor.
  • For dynamic content, use the Web Inspector’s Elements tab instead of the source view. Right-click an element on the page and select “Inspect Element” to jump to its HTML in the inspector.
  • Check for external files: The source often contains links to CSS and JavaScript files. Open those links to view their code as well.

Troubleshooting

  • “Show Page Source” is grayed out or missing: This usually means the Develop menu isn’t enabled. Go to Safari > Settings > Advanced and check “Show features for web developers.” Then try again.
  • Keyboard shortcut doesn’t work: Make sure you’re pressing Option+Command+U (not Control+U, which is for other browsers). Also, ensure the Develop menu is enabled.
  • Right-click doesn’t show “Show Page Source”: This option only appears when the Develop menu is enabled. If it’s still missing, try using the Develop menu directly.
  • Source looks incomplete or different from the page: The page likely uses JavaScript to modify the DOM. Use the Web Inspector (Develop > Show Web Inspector) and go to the Elements tab to see the current HTML structure.
  • On iPhone, device not showing in Develop menu: Make sure your iPhone is unlocked and trust the computer if prompted. Also, ensure Web Inspector is enabled in Settings > Safari > Advanced on the iPhone.

Frequently Asked Questions

Can I view HTML source on iPhone without a Mac?

No, Safari on iOS doesn’t have a built-in source viewer. You can use third-party apps or bookmarklets, but the official method requires connecting to a Mac and using Web Inspector.

Why does the source code look different from the page I see?

Modern websites often use JavaScript to dynamically generate content after the initial page load. The source shows the original HTML, while the rendered page may have been modified by scripts. Use the Web Inspector’s Elements tab to see the live DOM.

How do I view CSS and JavaScript files?

In the source code, look for <link> tags for CSS and <script> tags for JavaScript. Click on the file paths to open them in Safari. Alternatively, use the Web Inspector’s Resources tab to see all loaded files.

Can I edit the source code in Safari?

No, Safari’s source viewer is read-only. To edit and test changes, use the Web Inspector’s Elements tab, which allows you to modify HTML and CSS live. For permanent changes, you’d need a code editor and a web server.

Is viewing source code legal?

Yes, viewing the source code of a public webpage is legal. However, copying and using that code without permission may violate copyright or terms of service. Always respect intellectual property rights.

What if the source code is minified or hard to read?

Minified code has extra whitespace removed to reduce file size. You can use a beautifier tool online to format it for readability. The Web Inspector also formats code nicely in its panels.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →