Class Style<T extends Style<T>>

All Implemented Interfaces:
HeadElement

public class Style<T extends Style<T>> extends RawTextElement<T> implements HeadElement
Represents an HTML <style> element for embedding CSS styles within an HTML document.

This class provides a programmatic interface for creating and managing inline CSS styles in HTML documents. It supports both raw CSS strings and structured CssStyleSheet objects, offering flexibility in how styles are defined and applied.

Features:

  • Embeds CSS styles directly in HTML documents
  • Accepts raw CSS strings or CssStyleSheet objects
  • Supports media queries for responsive design
  • Allows specification of MIME type for the style content
  • Supports scoped styles (browser-dependent)

Usage:


 // Create with raw CSS string
 Style style = new Style("body { margin: 0; padding: 0; }");

 // Create with CssStyleSheet object
 CssStyleSheet sheet = new CssStyleSheet();
 // ... configure stylesheet
 Style style = new Style(sheet);

 // Set media query for responsive design
 Style mobileStyle = new Style();
 mobileStyle.setMedia("screen and (max-width: 600px)");
 mobileStyle.setContent(".mobile { display: block; }");

 // Set MIME type
 style.setType("text/css");
 
Since:
2007
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • Style

      public Style()
      Constructs an empty Style element.

      Creates a new <style> element with no initial CSS content. The element is configured to allow child elements (specifically Text elements containing CSS code).

    • Style

      public Style(String css)
      Constructs a Style element with the specified CSS content.

      Creates a new <style> element and immediately populates it with the provided CSS string.

      Parameters:
      css - the CSS code to embed in the style element
    • Style

      public Style(CssStyleSheet styleSheet)
      Constructs a Style element with the specified CSS stylesheet object.

      Creates a new <style> element and immediately populates it with the CSS generated from the provided CssStyleSheet object.

      Parameters:
      styleSheet - the CSS stylesheet object to convert and embed
  • Method Details

    • setMedia

      public final T setMedia(String mediaQuery)
      Sets the media query that this style applies to.

      Specifies the media condition under which the styles should be applied, enabling responsive design. Example: "screen and (max-width: 600px)".

      Parameters:
      mediaQuery - The media query string.
      Returns:
      This element for method chaining.
    • setType

      public final T setType(String type)
      Sets the MIME type of the style content.

      Specifies the styling language MIME type. The default and most common value is "text/css".

      Parameters:
      type - The MIME type of the style content.
      Returns:
      This element for method chaining.
    • setScoped

      public final T setScoped(boolean scoped)
      Sets whether this style element should be scoped.

      When true, the styles apply only to the parent element and its children, rather than the entire document. Note that browser support for this attribute varies and it has been deprecated in HTML5.

      Parameters:
      scoped - true if styles should be scoped to parent element, false otherwise
      Returns:
      this element for method chaining
    • setContent

      public final T setContent(String content)
      Sets the CSS content of this style element using a string.

      Replaces any existing content with the provided CSS code.

      Note: CSS code is output without HTML escaping since it is trusted developer content, not user input. Never pass unsanitized user input to this method.

      Parameters:
      content - the CSS code to embed in the style element
      Returns:
      this element for method chaining
    • setContent

      public final T setContent(CssStyleSheet styleSheet)
      Sets the CSS content of this style element using a stylesheet object.

      Replaces any existing content with the CSS generated from the provided CssStyleSheet object.

      Parameters:
      styleSheet - the CSS stylesheet object to convert and embed
      Returns:
      this element for method chaining