Introduction
Most HTML tooling stops at the tree. Aspose.HTML FOSS for Python goes one layer further: it implements the CSS object model, so a stylesheet is not just text you carry around — it participates in a real cascade, and every element can tell you the style it actually ends up with.
This deep dive covers the CSSOM layer: parsing stylesheets with CSSStyleSheet, editing declarations through CSSStyleDeclaration, and reading results back with Element.get_computed_style() — including how specificity, !important, and inheritance interact. The library is on PyPI as aspose-html-foss, MIT licensed, and pure Python (3.10+), which makes it practical for style verification in test suites and services where a headless browser would be overkill.
Key Features
Parse and Attach Stylesheets
CSSStyleSheet.replace_sync() parses stylesheet text in place; Document.attach_style_sheet() registers the sheet with the document so it participates in the cascade. Sheets can also be built rule-by-rule with insert_rule(), add_rule(), and remove_rule(), or created from text with from_text():
from aspose_html.dom import Document
from aspose_html.cssom import CSSStyleSheet
doc = Document()
sheet = CSSStyleSheet()
sheet.replace_sync("div { color: red }")
doc.attach_style_sheet(sheet)
Specificity Decides the Winner
When several rules target the same element, the computed value follows selector specificity. An ID selector (specificity 1,0,0) outranks a class selector (0,1,0):
from aspose_html.dom import Document
from aspose_html.cssom import CSSStyleSheet
doc = Document()
el = doc.create_element("div")
el.set_attribute("class", "foo")
el.set_attribute("id", "bar")
doc.append_child(el)
sheet = CSSStyleSheet()
sheet.replace_sync(".foo { color: red } #bar { color: blue }")
doc.attach_style_sheet(sheet)
print(el.get_computed_style().get_property_value("color"))
Inline Declarations and CSSStyleDeclaration
Each element’s style property is a live CSSStyleDeclaration. Declarations are edited with set_property() and inspected with get_property_value(), get_property_priority(), and item(). Inline declarations carry (1,0,0) specificity, so they beat type and class rules from author stylesheets:
from aspose_html.dom import Document
from aspose_html.cssom import CSSStyleSheet
doc = Document()
el = doc.create_element("div")
doc.append_child(el)
sheet = CSSStyleSheet()
sheet.replace_sync("div { color: red }")
doc.attach_style_sheet(sheet)
inline = el.style
inline.set_property("color", "blue")
print(el.get_computed_style().get_property_value("color"))
Importance Outranks Specificity
!important inverts the usual outcome: an author rule marked !important beats a non-important inline declaration, regardless of specificity. Priorities can also be set programmatically with set_property_priority():
from aspose_html.dom import Document
from aspose_html.cssom import CSSStyleSheet
doc = Document()
el = doc.create_element("div")
doc.append_child(el)
sheet = CSSStyleSheet()
sheet.replace_sync("div { color: red !important }")
doc.attach_style_sheet(sheet)
inline = el.style
inline.set_property("color", "blue")
print(el.get_computed_style().get_property_value("color"))
Inheritance and the inherit Keyword
Inheritable properties like color flow from parent to child unless the child has its own local value — a local rule always overrides an inherited one. The explicit inherit keyword copies the parent’s computed value, and on a root element with no parent it falls back to the property’s initial value:
from aspose_html.dom import Document
doc = Document()
el = doc.create_element("div")
doc.append_child(el)
inline = el.style
inline.set_property("color", "inherit")
print(el.get_computed_style().get_property_value("color"))
Quick Start
Install from PyPI:
pip install aspose-html-foss
Resolve a three-way conflict — class rule vs. ID rule vs. inline declaration:
from aspose_html.dom import Document
from aspose_html.cssom import CSSStyleSheet
doc = Document()
el = doc.create_element("div")
el.set_attribute("class", "foo")
el.set_attribute("id", "bar")
doc.append_child(el)
sheet = CSSStyleSheet()
sheet.replace_sync(".foo { color: red } #bar { color: green }")
doc.attach_style_sheet(sheet)
inline = el.style
inline.set_property("color", "blue")
print(el.get_computed_style().get_property_value("color"))
Open Source & Licensing
Aspose.HTML FOSS for Python is distributed under the MIT license — commercial use, modification, and redistribution are permitted. Source is on GitHub and releases ship to PyPI as aspose-html-foss.