CSS Specificity Calculator
A Python library for calculating the CSS specificity of CSS selectors.
What is CSS specificity?
CSS specificity determines which style rule is applied by browsers when multiple rules could apply to a given element. It is generally represented as a tuple (a, b, c) or a,b,c (IDs, classes/attributes/pseudo-classes, element/pseudo-elements).
✨ Features
-
Computes specificity for a single CSS selector.
-
Supports all modern selector types:
- classes (
.foo), IDs (#bar), element types (div), attributes ([attr]) - pseudo-classes (
:hover), pseudo-elements (::after,:before) - functional pseudo-classes with correct specificity:
(
:is(),:not(),:has(),:where(), per Selectors Level 4) - custom pseudo-elements (e.g.
::part(),::slotted())
- classes (
-
Written in modern Python with dataclass support.
-
No dependencies except tinycss2.
-
Fast, simple, and reusable as a module.
🚀 Getting Started
Installation
pip install css_specificity
Or clone this repo and use poetry install if developing locally:
git clone https://gitlab.com/devlink42/css_specificity.git
cd css_specificity
poetry install
💭Usage
from css_specificity import SpecificityCalculator
spec = SpecificityCalculator.calculate("body .foo#bar > a:hover::after")
print(spec) # Output: 1,2,3
# a = ID count, b = class/attr/pseudo-class, c = type/pseudo-element
print(spec.a, spec.b, spec.c) # Output: 1 2 3
Handling advanced selectors:
print(
SpecificityCalculator.calculate(
".foo:is(#id, .bar)"
)
) # Output: 1,1,0 (because `#id` is the most specific within :is)
🫧 Features in depth and coming soon
✅ Already supported
-
Selectors Supported:
- Universal (
*), type (div), class (.foo), ID (#id) - Attribute (
[href],[type="text"]) - Pseudo-classes (
:hover,:focus, etc.) - Functional pseudo-classes per CSS4 with "max" specificity for
:is,:not,:has, and for:where()replaced by zero - Pseudo-elements (both
::before,::after, etc. and legacy:before, etc.) - Custom pseudo-elements (Web Components:
::part(),::slotted())
- Universal (
-
Multiple selectors: The API currently works for a single selector string. If you have a selector list (
a, b, c), split and process individually.
🔜 Coming soon
- Pseudo-classes function
:nth-child()and:nth-last-child()=> the specificity of the pseudo class itself (counting as one pseudo-class selector) plus the specificity of the most specific complex selector in its selector list argument (if any). - Better handle complex nested parentheses for pseudo-classes function
- Test and coverage
- CLI tool
- CI/CD Pipeline for testing and potentially publishing to PyPI automatically
🚥 API
SpecificityCalculator.calculate(selector: str) -> Specificity
Returns an instance for the given selector: Specificity
a: ID selectors count (#foo)b: class selectors, attribute selectors, and pseudo-classes (.foo,[type],:hover)c: type selectors and pseudo-elements (div,::before)
Specificity
A simple dataclass with fields .a, .b, .c (all integers), string representation, and comparison operators.
🪛 CLI Usage
(Not yet supported, but planned for a future release. Contributions welcome!)
🫂 Contributing
Contributions, bug reports, and feature requests are welcome!
Development setup:
- Clone the repo and use Poetry for virtualenv and tool management.
- For code formatting, use Black. (Pipeline coming soon to better check)
- For linting, use Flake8. (Pipeline coming soon to better check)
- For testing, use Pytest. (Pipeline coming soon to better check)
- For commit messages, use Conventional Commits.
Testing:
Tests are planned. MRs with test cases welcome!
📜 License
This project is licensed under MIT License.
Metadata
Release files for css_specificity 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| css_specificity-0.1.3.tar.gz | 6.0 kB | Without provenance | Details for css_specificity-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| css_specificity-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for css_specificity-0.1.3-py3-none-any.whl |
Total release size: 12.6 kB
Release files / css_specificity-0.1.3.tar.gz
| Download URL | css_specificity-0.1.3.tar.gz |
|---|---|
| Size | 6.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e2228ca023fcc41d424f55952d738b2c5e0019d52594e4e101ff74fbaf77b0a6
|
|
BLAKE2b-256 checksum How to use checksums |
85cf9155f57a511adfaa03e0030bb94cabf97d49218c9c5ab35d21a64a02961b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.2 CPython/3.12.3 Windows/11
|
Release files / css_specificity-0.1.3-py3-none-any.whl
| Download URL | css_specificity-0.1.3-py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ec45ad91361371bd7f65b0fe2aea28b38ab3fef99111f151b358d6190e996b4e
|
|
BLAKE2b-256 checksum How to use checksums |
523240edd77cac0a7569333cf21260183eb7a3282de35ad50e42524e7852c287
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.2 CPython/3.12.3 Windows/11
|
Security for css_specificity 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.