In computer hypertext, a URI fragment is a string of characters that refers to a resource that is subordinate to another, primary resource. The primary resource is identified by a Uniform Resource Identifier, and the fragment identifier points to the subordinate resource.
The fragment identifier introduced by a hash mark
#is the optional last part of a URL for a document. It is typically used to identify a portion of that document. The generic syntax is specified in RFC 3986. The hash-mark separator in URIs is not part of the fragment identifier.
BasicsIn URIs, a hash mark
#introduces the optional fragment near the end of the URL. The generic RFC 3986 syntax for URIs also allows an optional query part introduced by a question mark
?. In URIs with a query and a fragment, the fragment follows the query. Query parts depend on the URI scheme and are evaluated by the server—e.g.,
http:supports queries unlike
ftp:. Fragments depend on the document MIME type and are evaluated by the client. Clients are not supposed to send URI fragments to servers when they retrieve a document, and without help from a local application fragments do not participate in HTTP redirections.
A URI ending with
#is permitted by the generic syntax and is a kind of empty fragment. In MIME document types such as
text/htmlor any XML type, empty identifiers to match this syntactically legal construct are not permitted. Web browsers typically display the top of the document for an empty fragment.
The fragment identifier functions differently to the rest of the URI: its processing is exclusively client-sided with no participation from the web server, though the server typically helps to determine the MIME type, and the MIME type determines the processing of fragments. When an agent requests a web resource from a web server, the agent sends the URI to the server, but does not send the fragment. Instead, the agent waits for the server to send the resource, and then the agent processes the resource according to the document type and fragment value.
- In URIs for MIME
text/htmlpages such as
the fragment refers to the element with
- * Graphical Web browsers typically scroll to position pages so that the top of the element identified by the fragment id is aligned with the top of the viewport; thus fragment identifiers are often used in tables of content and in permalinks.
- ** Chrome versions 80 and above implement W3C's Text Fragments, so
will cause the browser to search for
foo, highlight the matching text, and scroll to it. Besides the start and end, the snippet can also specify a context: text that must precede or follow
foobut won't be highlighted.
- * The appearance of the identified element can be changed through the
:targetCSS pseudoclass; Wikipedia uses this to highlight the selected reference. Notably CSS
display: blockcan be used to show content only if it is the target, and otherwise hidden by
- * The deprecated
nameattribute had a similar purpose in now obsolete browsers. If present
idmust be identical.
- In all XML document types including XHTML fragments corresponding to an
idattributes follow the
Name-syntax and begin with a letter, underscore, or colon. Notably they cannot begin with a digit or hyphen.
xml:idis one of the few generic XML attributes, e.g.,
xml:lang, which can be used without explicitly declaring a namespace. In XHTML
idhas to be used, because XHTML was specified before
- In XML applications, fragment identifiers in a certain syntax can be XPointers; for example, the fragment identifier in the URI
refers to all XML elements named "Rube" in the document identified by the URI
http://www.example.org/foo.xml#xpointer http://www.example.org/foo.xml. An XPointer processor, given that URI, would obtain a representation of the document and would return a representation of the document's "Rube" elements.
- In RDF vocabularies, such as RDFS, OWL, or SKOS, fragment identifiers are used to identify resources in the same XML Namespace, but are not necessarily corresponding to a specific part of a document. For example,
identifies the concept "broader" in SKOS Core vocabulary, but it does not refer to a specific part of the resource identified by
, a complete RDF file in which semantics of this specific concept is declared, along with other concepts in the same vocabulary.
- In URIs for MIME
text/plaindocuments RFC 5147 specifies a fragment identifier for the character and line positions and ranges within the document using the keywords "
char" and "
line". Browser support seems lacking. The following example identifies lines 11 through 20 of a text document:
- In URIs for MIME
text/csvdocuments, RFC 7111 specifies a fragment identifier as a selector for rows, columns, and cells using the keywords "
col", and "
cell", for example:
– Selects the 4th row.
– Selects 2nd column.
– Selects three consecutive rows starting with 5th row.
– Selects all rows starting with 5th row.
– Selects a region that starts at the 4th row and the 1st column and ends at the 6th row and the 2nd column.
- In URIs for MIME audio/*, image/*, video/* documents, very few have defined fragments or fragment semantics. The Media Fragments URI 1.0 syntax supports addressing a media resource along two dimensions using the keywords
xywh. Therefore, one can use the following media fragments URI in the
srcattribute of the
- * Other websites use the fragment part to pass some extra information to scripts running on them – for example, Google Video understands permalinks in the format of
#01h25m30sto start playing at the specified position, and YouTube uses similar code such as
- * For example, Gmail uses a single URL for almost every interface – mail boxes, individual mails, search results, settings – the fragment is used to make these interfaces directly linkable.
- In URIs for MIME
application/pdfdocuments PDF viewers recognize a number of fragment identifiers. For instance, a URL ending in
.pdf#page=35will cause most readers to open the PDF and scroll to page 35. Several other parameters are possible, including
#zoom=, etc. Multiple parameters can be combined with ampersands:
- In SVG, fragments are allowed to specify arguments such as
- As of September 2012 the Media Fragments URI 1.0 is a W3C Recommendation.
- The Python Package Index appends the MD5 hash of a file to the URL as a fragment identifier. If MD5 were unbroken, it could be used to ensure the integrity of the package.
- A hash-bang fragment is a fragment starting with an exclamation mark
!. It was used in a now-deprecated approach to index dynamic single-page applications. An exclamation mark is illegal in HTML4, XHTML, and XML identifiers, granting certain degree of separation from that functionality.
- * Between 2009 and 2015, Google Webmaster Central proposed and then recommended an "AJAX crawling scheme" using an initial exclamation mark in fragment identifiers for stateful AJAX pages:
- * In 2015, Google deprecated their hash-bang AJAX crawling proposal, recommending instead the use of progressive enhancement and HTML5's
- *Mozilla Foundation employee Gervase Markham has proposed a fragment identifier for searching, of the form
#!s!search terms. Adding a number after the s indicates that the browser should search for the nth occurrence of the search term. A negative number starts searching backwards from the end of the document. A Greasemonkey script is available to add this functionality to compatible browsers.
- Erik Wilde and Marcel Baschnagel of the ETH Zurich extend this to also identify fragments in plain text documents using regular expressions, with the keyword "
match". They also describe a prototype implementation as an extension for the Firefox browser. For example, the following would find the case-insensitive text "RFC" anywhere in the document:
- K. Yee of the Foresight Institute proposes "extended fragment identifiers" delimited with colons and a keyword to differentiate them from anchor identifiers. A text search fragment identifier with "fragment specification scheme" id "
words" is the first proposal in this scheme. The following example would search a document for the first occurrence of the string "some context for a search term" and then highlight the words "search term":
- The LiveURLs project proposed a fragment identifier format for referring to a region of text within a page, of the form
#FWS+C, where F is the length of the first word, W is the first word itself, S is the length of the selected text and C is a 32-bit CRC of the selected text. They implemented a variant of this scheme as an extension for the Firefox browser, using the form
#LFWS+C, where L is the length of the fragment itself, in two hex digits. Linking to the word "Fragment" using the implemented variant would yield:
- Up until Firefox 5, Firefox supported XPath links such as #xpath:/html/body/div which could be used in conjunction with a bookmarklet such as http://antimatter15.com/wp/2009/11/xpath-bookmark-bookmarklet/ to link within HTML documents that lacked proper IDs. This feature was removed as part of a code cleanup in https://bugzilla.mozilla.org/show_bug.cgi?id=457102
- In ePub electronic book format, the EPUB Canonical Fragment Identifier defines a W3C/IDPF-standardized method for referencing arbitrary content using fragment identifiers to locate non-anchored text ranges via document structure and pattern matching. These dynamic deep links assist in locating content after text is updated and are used, for example, in Apple Books.