{"id":5180,"library":"dict2css","title":"dict2css","description":"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.","status":"active","version":"0.3.0.post1","language":"en","source_language":"en","source_url":"https://github.com/sphinx-toolbox/dict2css","tags":["css","styling","frontend","dictionary","stylesheet"],"install":[{"cmd":"pip install dict2css","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"make_css","correct":"from dict2css import make_css"},{"note":"The class was directly under `dict2css` module from v0.2.0, no need to import from `dict2css.converter`.","wrong":"from dict2css.converter import DictionaryToCss","symbol":"DictionaryToCss","correct":"from dict2css import DictionaryToCss"}],"quickstart":{"code":"from dict2css import make_css\n\n# Define your CSS structure as a Python dictionary\nstyle_dict = {\n    \"body\": {\n        \"font-family\": \"'Arial', sans-serif\",\n        \"margin\": \"0\",\n        \"padding\": \"0\",\n        \"background-color\": \"#f0f0f0\"\n    },\n    \"h1\": {\n        \"color\": \"#333\",\n        \"text-align\": \"center\"\n    },\n    \"p\": {\n        \"font-size\": \"16px\",\n        \"line-height\": \"1.5\",\n        \"color\": \"#666\"\n    },\n    \"#main-content\": {\n        \"width\": \"80%\",\n        \"margin\": \"20px auto\",\n        \"padding\": \"20px\",\n        \"background\": \"#fff\",\n        \"border-radius\": \"8px\"\n    }\n}\n\n# Convert the dictionary to a CSS string\ncss_output = make_css(style_dict)\n\nprint(css_output)","lang":"python","description":"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."},"warnings":[{"fix":"Migrate existing calls from `DictionaryToCss().convert(data)` to `make_css(data)` for a more streamlined API.","message":"The `make_css` convenience function was introduced in `v0.3.0` as the primary, simpler interface for converting dictionaries. While `DictionaryToCss` class still works, new code should prefer `make_css`.","severity":"gotcha","affected_versions":"Before v0.3.0"},{"fix":"For advanced CSS features, consider generating them as separate strings, concatenating them, or using a more comprehensive CSS preprocessor or templating engine if dynamic generation is required.","message":"The library primarily supports basic element, class, and ID selectors with nested properties. It does not natively provide direct dictionary-based support for complex CSS features like pseudo-classes (`:hover`), pseudo-elements (`::before`), or media queries (`@media`) within the standard nested dictionary structure.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure that all dictionary values correspond to valid CSS property values according to CSS specifications to avoid generating malformed stylesheets.","message":"dict2css performs minimal validation on the *values* provided in the input dictionary. Supplying syntactically incorrect or semantically invalid CSS property values (e.g., specifying a non-existent color or unit) will result in invalid CSS being generated without explicit errors from the library itself.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}