Show Link URLs When Printing a Webpage

Learn how to show selected link URLs in print with CSS, avoid clutter and sensitive parameters, and check browser previews and exported PDFs.

Published
Reading time
4 min read
Show Link URLs When Printing a Webpage
On this page

Printed pages lose an important feature of the web: readers cannot click a link to see where it goes. You can show link URLs when printing a webpage with CSS, while choosing which destinations are useful enough to include.

Key Takeaways

  • Use a print media query to add destinations without changing the normal screen layout.
  • Opt in to printing selected URLs; CSS cannot reliably decide whether a destination is too long or useful on paper.
  • Keep link text meaningful on its own, and test the result in each target browser and exported PDF.
  • Do not expose private tokens or other sensitive query parameters in print.

Choose destinations worth printing

Showing every href can make a page harder to read. In-page anchors such as #contact are usually useful for navigation on screen but not as standalone addresses on paper. Long URLs can dominate a paragraph, and some query strings contain information that should not be published.

A practical approach is to opt in only links with a short, public destination. Put the destination in a separate attribute so you can leave other links alone. If a long destination is essential, consider publishing a short, stable URL you control; make sure it reaches the intended resource and remains available to print readers.

Advertisement

Add a print-only URL

This standalone HTML example uses fictional paths on the reserved example.com domain. Replace them with your own public destinations. No JavaScript or dependencies are required.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Printable resource</title>
  <style>
    @media print {
      a.print-url[href^="https://"][data-print-url]::after,
      a.print-url[href^="http://"][data-print-url]::after {
        content: " (" attr(data-print-url) ")";
        overflow-wrap: anywhere;
      }
    }
  </style>
</head>
<body>
  <p>
    Read the <a class="print-url"
      href="https://example.com/guide?view=print&amp;lang=en#setup"
      data-print-url="https://example.com/guide?view=print&amp;lang=en#setup">setup guide</a>.
  </p>
  <p><a href="#contact">Jump to contact details</a>.</p>
  <h2 id="contact">Contact details</h2>
  <p>Contact the team using the details supplied with this document.</p>
</body>
</html>

The @media print rule applies when the browser renders for print. The ::after pseudo-element adds text after the link, and attr(data-print-url) supplies the selected destination. In HTML attributes, &amp; represents the ampersand in the query string.

Only opted-in HTTP or HTTPS links receive an appended destination. The fragment-only link does not. An absolute URL pointing to a section of the same page could still match, so leave it without the class or data attribute if you do not want it printed.

Keep data-print-url in sync with href. The example duplicates the destination deliberately: that makes editorial selection simple, but a stale attribute can send print readers somewhere different from the clickable link. If you use a separate short URL, verify that it leads to the same intended resource.

This example has not been executed here, and current MDN documentation for CSS media queries and generated content could not be consulted in this run. Check that documentation and your supported browsers before relying on the technique.

Test print previews and PDFs

Save the example as an HTML file and open it in each browser you support. Repeat the checks on an actual page from your site, since your other styles can affect printing.

  1. Open print preview and confirm the selected destination appears beside readable link text. Confirm the in-page anchor does not gain an appended URL.
  2. Check a URL with both a query string and a fragment, as in the example. Make sure punctuation is visible and line wrapping does not obscure characters.
  3. Export or save as PDF using the browser's available print option. Open the PDF in a viewer and inspect the same details. If clickable PDF links matter to your readers, test them too; do not assume every browser or PDF viewer preserves them identically.
  4. Try a long URL, a narrow paper size, and a link near a page break. If the result is awkward, omit the destination or provide a suitable short public URL rather than relying on wrapping alone.
  5. Repeat with your target browsers, print settings, and, where relevant, a physical printer. Print behavior, PDF export, pagination, and generated content can vary.

Advertisement

Common Mistakes

  • Appending every href. Fragment-only links and lengthy addresses can add noise without helping the reader.
  • Printing secrets. Review query parameters before publishing. Never print access tokens, personal information, or other sensitive values.
  • Letting the displayed URL drift. Update the print attribute whenever the destination changes, or check both as part of your publishing process.
  • Assuming the PDF behaves like the webpage. A visible address and a clickable PDF link are different outcomes; verify whichever readers need.

Accessibility and usability

Use descriptive link text such as “setup guide,” not just “click here.” The text should identify the link's purpose even if generated content is unavailable or rendered differently by assistive technology. Do not rely on the appended URL as the only way to identify a link.

Where readers must know an address regardless of CSS or printing, include it as ordinary document text as well. Check that added addresses remain legible, do not overlap surrounding content, and can be distinguished from the link label.

Conclusion

A print-only pseudo-element can make selected links useful on paper without adding every destination to the page. Curate the URLs, keep them in sync with their links, and verify the printed and PDF results in your supported browsers.

What would your domain sell for?

A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.