Class Style<T extends Style<T>>
- All Implemented Interfaces:
HeadElement
<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
CssStyleSheetobjects - 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 Summary
ConstructorsConstructorDescriptionStyle()Constructs an empty Style element.Style(CssStyleSheet styleSheet) Constructs a Style element with the specified CSS stylesheet object.Constructs a Style element with the specified CSS content. -
Method Summary
Modifier and TypeMethodDescriptionfinal TsetContent(CssStyleSheet styleSheet) Sets the CSS content of this style element using a stylesheet object.final TsetContent(String content) Sets the CSS content of this style element using a string.final TSets the media query that this style applies to.final TsetScoped(boolean scoped) Sets whether this style element should be scoped.final TSets the MIME type of the style content.Methods inherited from class com.oorian.html.RawTextElement
addText, addText, setTextMethods inherited from class com.oorian.html.Element
addAttribute, addAttribute, addAttribute, addAttribute, addElement, addLineBreak, addLineOfText, addLineOfText, addLineOfText, addParagraph, addParagraph, addSpacer, addText, addText, assignId, containsElement, containsElement, create, dispatchEvent, dispatchEvent, dispatchEvent, dispatchEvent, equals, excludeId, executeJs, executeJs, executeJs, getAccept, getAllElements, getAncestor, getAttribute, getAttributes, getComponent, getDir, getElement, getElementById, getElementCount, getElementCount, getElements, getElements, getElementsByComponentName, getElementsByTagName, getHtml, getHtml, getId, getInnerHtml, getInnerHtml, getIs, getItemId, getItemProp, getItemRef, getItemType, getLang, getNextSibling, getPage, getParent, getPart, getPrevSibling, getSlot, getTagName, getTextContent, getUrl, hasAttribute, hasElements, hidden, initialize, insertElement, isChildOf, isClosedTag, isCreated, isDescendantOf, isDescendantOf, isInitialized, isItemScope, isTranslate, onCallback, onCreated, onElementAdded, onElementRemoved, onHashChange, onHidden, onInitialized, onJsReturn, onPageLoaded, onPageUnloaded, onRefresh, onRemovedFromPage, onShown, onUpdated, onUserEvent, prewrite, recreate, refresh, refresh, registerAddition, registerListener, registerListener, registerListener, registerListener, registerListener, registerListener, registerListener, registerSubtraction, registerUpdate, removeAllElements, removeAttribute, removeAttribute, removeElement, removeElement, removeFromParent, requestCallback, requestCallback, requestCallback, requestCallback, resetId, scrollTo, scrollToBottom, scrollToTop, self, sendCommand, sendUpdate, setAccept, setComponent, setDir, setDir, setElement, setId, setIs, setItemId, setItemProp, setItemRef, setItemScope, setItemType, setLang, setOnError, setOnLoad, setPage, setParent, setPart, setSlot, setTagName, setText, setText, setText, setTranslate, shown, toString, unregisterListener, update, updateAttributes
-
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 (specificallyTextelements containing CSS code). -
Style
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
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 providedCssStyleSheetobject.- Parameters:
styleSheet- the CSS stylesheet object to convert and embed
-
-
Method Details
-
setMedia
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
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
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
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
Sets the CSS content of this style element using a stylesheet object.Replaces any existing content with the CSS generated from the provided
CssStyleSheetobject.- Parameters:
styleSheet- the CSS stylesheet object to convert and embed- Returns:
- this element for method chaining
-