Skip to content

Overly specific CSS selectors #5473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mindplay-dk opened this issue Mar 10, 2025 · 0 comments
Open

Overly specific CSS selectors #5473

mindplay-dk opened this issue Mar 10, 2025 · 0 comments

Comments

@mindplay-dk
Copy link

it looks like you're using a BEM convention for the CSS class names?

and it looks like the class names are being applied to individual, specific elements using a BEM like pattern?

but then the SCSS is organized in deeply nested structures, yieling selectors with arbitrary specificity, such as:

.react-datepicker__time-container .react-datepicker__time .react-datepicker__time-box ul.react-datepicker__time-list li.react-datepicker__time-list-item--selected

it looks like the SCSS nesting directly mirrors the HTML element structure? for example:

.react-datepicker__time {
position: relative;
background: white;
border-bottom-right-radius: 0.3rem;
.react-datepicker__time-box {
width: 85px;
overflow-x: hidden;
margin: 0 auto;
text-align: center;
border-bottom-right-radius: 0.3rem;
ul.react-datepicker__time-list {
list-style: none;
margin: 0;
height: calc(195px + (#{$datepicker__item-size} / 2));
overflow-y: scroll;
padding-right: 0;
padding-left: 0;
width: 100%;
box-sizing: content-box;
li.react-datepicker__time-list-item {
height: 30px;
padding: 5px 10px;
white-space: nowrap;

what you probably meant to do is something like:

  .react-datepicker__time {
    // ...
    &-box {
      // ...
    }
    &-list {
      // ...
      &-item {
        // ...

which would yield flat BEM-style selectors like:

.react-datepicker__time-list-item--selected

this approach gives you an SCSS file that mirrors the BEM structure, rather than the HTML structure.

the point of this is, your emitted CSS file will be much, much smaller - the rules will all have the semi specificity, which is the point of using the BEM pattern, making it easy (possible) to override the standard CSS for the component by merely adding your own CSS after the imported standard CSS.

currently, you need to either write extremely long CSS selectors to override anything - or use !important everywhere, or contain the component's CSS in a cascade layer or something... either way, you're fighting the component's standard CSS for control.

I don't see why you would need very specific selectors? unless maybe you're fighting some other CSS framework (boostrap?) for control in the first place?

let me know if you're interested in solving this? maybe I can help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant