Quickstart

Quickstart

Five minutes from install to a styled document: this page builds a small DOM, applies a stylesheet, and reads the resolved style back.


Prerequisites

RequirementDetail
Python3.10 or later
Packageaspose-html-foss installed (Installation)

1. Create a document and elements

Document is the factory for every node type. Create an element, give it attributes, and attach it to the tree:

from aspose_html.dom import Document

doc = Document()
el = doc.create_element("div")
el.set_attribute("class", "foo")
el.set_attribute("id", "bar")
doc.append_child(el)

2. Attach a stylesheet

Parse CSS text with CSSStyleSheet.replace_sync() and register the sheet with the document so it participates in the cascade:

from aspose_html.cssom import CSSStyleSheet

sheet = CSSStyleSheet()
sheet.replace_sync(".foo { color: red } #bar { color: blue }")
doc.attach_style_sheet(sheet)

3. Read the computed style

Element.get_computed_style() resolves specificity, importance, and inheritance. Here the ID selector outranks the class selector:

style = el.get_computed_style()
print(style.get_property_value("color"))

4. Try inline styles

Inline declarations set through the element’s style declaration take priority over author stylesheet rules:

inline = el.style
inline.set_property("color", "green")
print(el.get_computed_style().get_property_value("color"))

Next Steps

See Also