Class Link


@Marshalled(typeName="link") public class Link extends HtmlElementVoid<Link>
DTO for an HTML <link> element.

The link element specifies relationships between the current document and an external resource. It is most commonly used to link to stylesheets, but can also be used to establish site icons, prefetch resources, define alternate versions of the document, and more. Link elements are typically placed in the head section of an HTML document.

Examples:

// Stylesheet link Link stylesheet = link() .rel("stylesheet") .href("/css/styles.css"); // Favicon link Link favicon = link() .rel("icon") .type("image/x-icon") .href("/favicon.ico"); // Preload resource Link preload = link() .rel("preload") .href("/fonts/myfont.woff2") .as_("font") .type("font/woff2") .crossorigin("anonymous"); // Alternate language version Link alternate = link() .rel("alternate") .href("/es/page.html") .hreflang("es"); // Responsive stylesheet with media query Link print = link() .rel("stylesheet") .href("/css/print.css") .media("print"); // Canonical URL for SEO Link canonical = link() .rel("canonical") .href("https://example.com/page"); // DNS prefetch for performance Link dnsPrefetch = link() .rel("dns-prefetch") .href("https://cdn.example.com");

The following convenience methods are provided for constructing instances of this bean:

See Also:
  • Constructor Details

    • Link

      public Link()
      Creates an empty Link element.
    • Link

      public Link(Object href)
      Creates a Link element with the specified href(Object) attribute.
      Parameters:
      href - The href(Object) attribute. Can be null, in which case the attribute is stored with a null value (not removed).
  • Method Details

    • as_

      public Link as_(String value)
      as attribute.

      Specifies the type of content being loaded by a rel="preload" or rel="modulepreload" link. This allows the browser to prioritize resource loading appropriately.

      Common values:

      • "audio" - Audio file
      • "document" - HTML document
      • "fetch" - Resource fetched by fetch or XHR
      • "font" - Font file
      • "image" - Image file
      • "script" - JavaScript file
      • "style" - CSS stylesheet
      • "video" - Video file
      • "worker" - Web worker script
      Parameters:
      value - The type of content being loaded. Can be null to unset the attribute.
      Returns:
      This object.
    • crossorigin

      public Link crossorigin(String value)
      crossorigin attribute.

      Specifies how the element handles cross-origin requests for CORS (Cross-Origin Resource Sharing).

      Possible values:

      • "anonymous" - Cross-origin requests are made without credentials
      • "use-credentials" - Cross-origin requests include credentials
      Parameters:
      value - How to handle cross-origin requests. Can be null to unset the attribute.
      Returns:
      This object.
    • href

      public Link href(Object value)
      href attribute.

      Address of the hyperlink.

      The value can be of any of the following types: URI, URL, String. Strings must be valid URIs.

      URIs defined by UriResolver can be used for values.

      Parameters:
      value - The new value for this attribute. Typically a URL or String. Can be null, in which case the attribute is stored with a null value (not removed).
      Returns:
      This object.
    • hreflang

      public Link hreflang(String value)
      hreflang attribute.

      Specifies the language of the linked resource. Used for SEO and accessibility purposes.

      Examples:

      • "en" - English
      • "es" - Spanish
      • "fr" - French
      • "de" - German
      • "zh" - Chinese
      • "ja" - Japanese
      Parameters:
      value - The language code of the linked resource. Can be null to unset the attribute.
      Returns:
      This object.
    • media

      public Link media(String value)
      media attribute.

      Specifies which media types the linked resource applies to. Used primarily with stylesheets.

      Common values:

      • "all" - All media types (default)
      • "screen" - Computer screens
      • "print" - Printers and print preview
      • "handheld" - Handheld devices
      • "projection" - Projectors
      • "(max-width: 768px)" - Media queries
      Parameters:
      value - The media types the linked resource applies to. Can be null to unset the attribute.
      Returns:
      This object.
    • rel

      public Link rel(String value)
      rel attribute.

      Specifies the relationship between the current document and the linked resource.

      Common values:

      • "stylesheet" - External CSS stylesheet
      • "icon" - Favicon or site icon
      • "canonical" - Canonical URL for SEO
      • "alternate" - Alternative version of the page
      • "preload" - Resource to preload
      • "prefetch" - Resource to prefetch
      • "dns-prefetch" - DNS lookup to prefetch
      • "next" - Next page in a sequence
      • "prev" - Previous page in a sequence
      Parameters:
      value - The relationship between the document and linked resource. Can be null to unset the attribute.
      Returns:
      This object.
    • sizes

      public Link sizes(String value)
      sizes attribute.

      Specifies the sizes of icons for different device contexts. Used with rel="icon" or rel="apple-touch-icon".

      Common values:

      • "16x16" - Small favicon
      • "32x32" - Standard favicon
      • "180x180" - Apple touch icon
      • "192x192" - Android icon
      • "512x512" - Large icon
      • "any" - Any size
      Parameters:
      value - The sizes of the linked icon resource. Can be null to unset the attribute.
      Returns:
      This object.
    • type

      public Link type(String value)
      type attribute.

      Specifies the MIME type of the linked resource. Helps browsers determine how to handle the resource.

      Common values:

      • "text/css" - CSS stylesheet
      • "text/javascript" - JavaScript file
      • "application/json" - JSON data
      • "image/png" - PNG image
      • "image/jpeg" - JPEG image
      • "image/svg+xml" - SVG image
      • "font/woff2" - Web font
      Parameters:
      value - The MIME type of the linked resource. Can be null to unset the attribute.
      Returns:
      This object.