dict2css

0.3.0.post1 · active · verified Mon Apr 13

dict2css is a μ-library for constructing cascading style sheets from Python dictionaries. It simplifies the process of programmatically generating CSS by mapping Python dictionary structures to CSS rules. The current version is 0.3.0.post1, with an irregular release cadence, having recent updates in late 2023.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a CSS structure using a Python dictionary and then convert it into a valid CSS string using the `make_css` function. It covers basic element styling, ID selectors, and nested properties.

from dict2css import make_css

# Define your CSS structure as a Python dictionary
style_dict = {
    "body": {
        "font-family": "'Arial', sans-serif",
        "margin": "0",
        "padding": "0",
        "background-color": "#f0f0f0"
    },
    "h1": {
        "color": "#333",
        "text-align": "center"
    },
    "p": {
        "font-size": "16px",
        "line-height": "1.5",
        "color": "#666"
    },
    "#main-content": {
        "width": "80%",
        "margin": "20px auto",
        "padding": "20px",
        "background": "#fff",
        "border-radius": "8px"
    }
}

# Convert the dictionary to a CSS string
css_output = make_css(style_dict)

print(css_output)

view raw JSON →