serializing CSS

To serialize any stylesheet use:

print sheet.cssText

Also most other objects have a similar property which contains the text content of each object. Some use a slightly different name (e.g. selectorText) but all use the global serializer:

>>> sheet = cssutils.parseString('a, b { color: green }')
>>> print sheet.cssRules[0].cssText
a, b {
    color: green
>>> print sheet.cssRules[0].selectorText
a, b
>>> print sheet.cssRules[0].selectorList[1].selectorText


Quite a few preferences of the cssutils serializer may be tweaked.

To set a preference use:

cssutils.ser.prefs.PREFNAME = NEWVALUE

Preferences are always used globally, so for all stylesheets until preferences are set again.

class cssutils.serialize.Preferences(**initials)

Control output of CSSSerializer.

defaultAtKeyword = True

Should the literal @keyword from src CSS be used or the default form, e.g. if True: @import else: @i\mport

defaultPropertyName = True

Should the normalized propertyname be used or the one given in the src file, e.g. if True: color else: c\olor

Only used if keepAllProperties==False.

defaultPropertyPriority = True

Should the normalized or literal priority be used, e.g. !important or !Im\portant

importHrefFormat = None

Uses hreftype if None or format "URI" if 'string' or format url(URI) if 'uri'

indent = 4 * ‘ ‘

Indentation of e.g Properties inside a CSSStyleDeclaration

indentClosingBrace = True

Defines if closing brace of block is indented to match indentation of the block (default) oder match indentation of selector.

indentSpecificities = False (EXPERIMENTAL)

Indent rules with subset of Selectors and higher Specitivity

keepAllProperties = True

If True all properties set in the original CSSStylesheet are kept meaning even properties set twice with the exact same same name are kept!

keepComments = True

If False removes all CSSComments

keepEmptyRules = False

defines if empty rules like e.g. a {} are kept in the resulting serialized sheet

keepUnknownAtRules = True

defines if unknown @rules like e.g. @three-dee {} are kept in the serialized sheet

keepUsedNamespaceRulesOnly = False

if True only namespace rules which are actually used are kept

lineNumbers = False

Only used if a complete CSSStyleSheet is serialized.

lineSeparator = u’\n’

How to end a line. This may be set to e.g. u’’ for serializing of CSSStyleDeclarations usable in HTML style attribute.

listItemSpacer = u’ ‘

string which is used in css.SelectorList, css.CSSValue and stylesheets.MediaList after the comma

minimizeColorHash = True

defines if colorhash should be minimized from full size to shorthand e.g minimize #FFFFFF to #FFF

normalizedVarNames = True

defines if variable names should be serialized normalized (they are used as being normalized anyway)

omitLastSemicolon = True

If True omits ; after last property of CSSStyleDeclaration

omitLeadingZero = False

defines if values between -1 and 1 should omit the 0, like .5px

paranthesisSpacer = u’ ‘

string which is used before an opening paranthesis like in a css.CSSMediaRule or css.CSSStyleRule

propertyNameSpacer = u’ ‘

string which is used after a Property name colon

resolveVariables = True

if True all variable references are tried to resolved and all CSSVariablesRules are removed from the output. Any variable reference not resolvable is simply kept untouched.

selectorCombinatorSpacer = u’ ‘

string which is used before and after a Selector combinator like +, > or ~. CSSOM defines a single space for this which is also the default in cssutils.

spacer = u’ ‘

general spacer, used e.g. by CSSUnknownRule

validOnly = False

if True only valid (Properties) are output

A Property is valid if it is a known Property with a valid value.


Reset all preference options to their default value.


Set options resulting in a minified stylesheet.

You may want to set preferences with this convenience method and override specific settings you want adjusted afterwards.


There is a single global serializer used throughout the library. You may configure it by setting specific Preferences or completely replace it with your own.

A custom serializer must implement all methods the default one provides. Easiest would be to subclass cssutils.serialize.CSSSerializer.

To set a new serializer, use:


You may also set cssutils.ser directly but the above method is the preferred one.

For most cases adjusting the cssutils.ser.prefs of the default serializer should be sufficient though.

class cssutils.serialize.CSSSerializer(prefs=None)

Serialize a CSSStylesheet and its parts.

To use your own serializing method the easiest is to subclass CSS Serializer and overwrite the methods you like to customize.