This module provides utilities for manipulating CSS stylesheets dynamically within the browser environment. It includes a core Doh.css function for basic style injection and integrates a more powerful jQuery plugin ($.stylesheet) for advanced CSS rule management.
Doh.css(cssContent, [styleElement])This is the primary function for adding CSS rules to the document.
<style> element (if one doesn't exist or isn't provided) and appends CSS rules to it.cssContent (String): The CSS rules to add (e.g., "body { background-color: blue; }").styleElement (HTMLStyleElement, Optional): An existing <style> element to append the CSS to. If omitted, it uses a default, cached <style> element, creating it if necessary.<style> element that was created or updated.Example:
// Add a simple rule to the default style element
Doh.css(".my-class { color: red; }");
// Create a specific style element and add rules to it
const myStyle = document.createElement('style');
document.head.appendChild(myStyle);
Doh.css("p { font-size: 16px; }", myStyle);
Doh.css("a { text-decoration: none; }", myStyle);
$.stylesheet)The module also bundles a jQuery plugin ($.stylesheet) for more complex CSS manipulations.
Author: Vimal Aravindashan Version: 0.3.7 License: MIT
This plugin allows you to select CSS rules (potentially filtered by stylesheet ID or href) and then get, set, or delete their properties.
$.stylesheet(selector, [name], [value])This function acts as both a constructor for $.stylesheet objects and a shortcut for applying CSS changes immediately.
$.stylesheet object instance or directly modifies CSS rules.selector (String): A CSS selector string. It can optionally include a stylesheet filter prefix (e.g., '#mySheet { .my-rule }' or '* { .my-rule }').name (String | Array | Object, Optional): The CSS property name(s) to get or set.'color'), gets or sets a single property.['color', 'font-size']), gets multiple properties.{ color: 'blue', 'font-size': '14px' }), sets multiple properties.value (String, Optional): The value to set for the specified name. Used only when name is a string.$.stylesheet | String | Object)name and value are omitted: Returns a new $.stylesheet object instance representing the matched rules.name is a string and value is omitted: Returns the current value of the CSS property for the first matched rule.name is an array and value is omitted: Returns an object containing the current values of the specified properties.value (or an object for name) is provided: Returns the $.stylesheet instance for chaining.$.stylesheet.cssRules(selector): Returns an array of raw CSSStyleRule objects matching the selector (and optional stylesheet filter).$.stylesheet.camelCase(hyphenatedName): Converts a hyphenated CSS property name to camelCase (e.g., 'font-size' to 'fontSize').$.stylesheet.cssStyleName(name): Takes a CSS property name (hyphenated or camelCase) and returns the browser-specific version (including vendor prefixes if necessary) that can be used to access the property via the style object (e.g., 'border-radius' might return 'WebkitBorderRadius' on some browsers).$.stylesheet objects)rules(): Returns a copy of the array of CSSStyleRule objects matched by the selector when the $.stylesheet object was created.css(name, [value]): Gets or sets CSS properties for the matched rules. Behaves similarly to the factory function shortcut but operates on the existing set of matched rules.myStylesheet.css('color'): Gets the color.myStylesheet.css('color', 'red'): Sets the color.myStylesheet.css({ color: 'red', 'font-weight': 'bold' }): Sets multiple properties.myStylesheet.css(null): Deletes the matched CSS rules entirely.// Get the background color of all h1 elements in the '#main-styles' stylesheet
const bgColor = $.stylesheet('#main-styles { h1 }', 'background-color');
console.log('H1 Background:', bgColor);
// Set the font size and color for all elements with class 'highlight'
const highlightStyle = $.stylesheet('.highlight'); // Create instance
highlightStyle.css({
'font-size': '1.2em',
'color': 'purple'
});
// Add a new rule using the plugin's internal mechanism
// (Creates rule if it doesn't exist)
$.stylesheet('.new-rule', 'border', '1px solid green');
// Get multiple properties
const props = $.stylesheet('p').css(['margin-top', 'line-height']);
console.log('Paragraph styles:', props);
// Delete a rule
$.stylesheet('.old-rule').css(null);
#id { selector } or href { selector }) allows targeting specific stylesheets.