Skip to content

[css-selectors] Substring Matching of Individual Class Names #2984

@simlawr

Description

@simlawr

It's currently possible to match substrings of attribute values, including the class attribute.

https://www.w3.org/TR/selectors-4/#attribute-substrings

It would also be useful to have the ability to match substrings of individual class names. Using the existing attribute selector syntax as a reference, this might look something like:

.classname /* A standard class selector */

.[^class] /* Matches any class name that begins with "class" */
.[$name]  /* Matches any class name that ends with "name" */
.[*ass]   /* Matches any class name that contains the substring "ass" */

Which would be compatible with the Selectors Level 4 case-sensitivity syntax:

.[^classname i] 
.[$classname i] 
.[*classname i]

Also, it could be modified to support substring matching in ID selectors:

#[^example]  #[^example i] 
#[$example]  #[$example i] 
#[*example]  #[*example i]

Such a selector would make it practical to use a naming convention whereby a .Module parent shares common styles with a .SubModule child because although this can be achieved by grouping the related classes, it becomes unwieldy as the number of children increases, for example:

.Module, .SubModule, .AnotherModule, .YetAnotherModule {…}

:matches(.Module, .SubModule, .AnotherModule, .YetAnotherModule).variant {…}

Whereas, this could be rewritten in a simpler form using a substring matching class selector, so that there is no need to manage lists of dependants and style sharing is automatic:

.[$Module] {…}         /* Equivalent to .Module */
.[$Module].variant {…} /* Equivalent to .Module.variant */

.SubModule {…}         /* Inherits styles from .Module */
.SubModule.variant {…} /* Inherits styles from .Module.variant */

.ModuleUnrelated {…} /* Not matched as name begins not ends with "Module" */

The effect is static inheritance without the use of a preprocessor e.g. @extend in Sass and Stylus.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions