A few utility functions which may help when working with CSS stylesheets.



Retrieve all url(urlstring) values (in e.g. cssutils.css.CSSImportRule or cssutils.css.CSSValue objects of given sheet.


sheetcssutils.css.CSSStyleSheet object whose URLs are yielded

This function is a generator. The generated URL values exclude url( and ) and surrounding single or double quotes.


cssutils.replaceUrls(sheet, replacer, ignoreImportRules=False)#
cssutils.replaceUrls(style: CSSStyleDeclaration, replacer, ignoreImportRules=False)

Replace all URLs in cssutils.css.CSSImportRule or cssutils.css.CSSValue objects of given sheet.

  • sheet – a cssutils.css.CSSStyleSheet to be modified in place.

  • replacer – a function which is called with a single argument url which is the current value of each url() excluding url(, ) and surrounding (single or double) quotes.

  • ignoreImportRules – if True does not call replacer with URLs from @import rules.


cssutils.resolveImports(sheet, target=None)#

Recursively combine all rules in given sheet into a target sheet. Attempts to wrap @import rules that use media information into @media rules, keeping the media information. This approach may not work in all instances (e.g. if an @import rule itself contains an @import rule with different media infos or if it contains rules that may not be used inside an @media block like @namespace rules). In these cases, the @import rule from the original sheet takes precedence and a WARNING is issued.


cssutils.css.CSSStyleSheet with imports resolved.