Documentation

linkElement
Question mark
Go to source

Links to a URL or a location in the document.

By default, links do not look any different from normal text. However, you can easily apply a style of your choice with a show rule.

Example

#show link: underline

https://example.com \

#link("https://example.com") \
#link("https://example.com")[
  See example.com
]

Syntax

This function also has dedicated syntax: Text that starts with http:// or https:// is automatically turned into a link.

Hyphenation

If you enable hyphenation or justification, by default, it will not apply to links to prevent unwanted hyphenation in URLs. You can opt out of this default via show link: set text(hyphenate: true).

Accessibility

The destination of a link should be clear from the link text itself, or at least from the text immediately surrounding it. In PDF export, Typst will automatically generate a tooltip description for links based on their destination. For links to URLs, the URL itself will be used as the tooltip.

In HTML export, a link to a label or location will be turned into a fragment link to a named anchor point. To support this, targets without an existing ID will automatically receive an ID in the DOM. How this works varies by which kind of HTML node(s) the link target turned into:

If you rely on a specific DOM structure, you should ensure that the link target turns into one or multiple elements, as the compiler makes no guarantees on the precise segmentation of text into text nodes.

If present, the automatic ID generation tries to reuse the link target’s label to create a human-readable ID. A label can be reused if:

These rules ensure that the label is both a valid CSS identifier and a valid URL fragment for linking.

As IDs must be unique in the DOM, duplicate labels might need disambiguation when reusing them as IDs. The precise rules for this are as follows:

In bundle export, linking still works as usual. For instance, if you attach a label to an element in one document, links in other documents can reference that label. In addition, documents and assets are also directly linkable. To link to a full document or asset, you can attach a label to it or query for it and extract its location.

#document("index.html")[
  // Link to document.
  #link(<appendix>)[To appendix]

  // Link into document.
  See the #link(<glossary>)[Glossary]
  for more information.
]

#document("appendix.html")[
  = Definitions
  ...

  = Glossary <glossary>
  ...
] <appendix>

Cross-document links are emitted as relative paths (potentially with fragments). Typst automatically assigns anchor names per document based on the same rules as in HTML export. In HTML and SVG documents, these are emitted as id attributes on elements. In PDF documents, they are emitted as named destinations. PNG documents do not support linking.

Note that links always use full relative paths. In some scenarios (primarily for multi-page websites), this may not be desirable. For instance, you may want to generate a /blog/index.html document while wanting to link to it as just /blog. Furthermore, your web server might treat /blog and /blog/ as interchangeable and serve /blog/index.html for both. If a user then navigates to /blog, relative links to other pages generated by Typst will no longer work. Currently, Typst does not have a way to directly hook into the built-in link handling. That said, in HTML export, depending on your use case, it may be possible to adjust the built-in link handling with a show rule on html.elem.where(tag: "a").

Parameters

dest
str or label or location or dictionary
RequiredPositional
Question mark

The destination the link points to.

ExpandView example
= Introduction <intro>
#link("mailto:hello@typst.app") \
#link(<intro>)[Go to intro] \
#link((page: 1, x: 0pt, y: 0pt))[
  Go to top
]

body
content
RequiredPositional
Question mark

The content that should become a link.

If dest is an URL string, the parameter can be omitted. In this case, the URL will be shown as the link.