DEV Community

Discussion on: data-attributes vs. BEM

Collapse
 
eriksen_dk profile image
Jakob E

👏 Attribute selectors are great. I once wrote a small post on using them to handle responsive styling (shameless self-promotion 😉)

One small thing regarding multiple modifiers. The syntax should be ~= to avoid value name collisions e.g.

[data-color*="lightgray"] { color: whitesmoke; }
[data-color*="gray"] { color: gray; }
Enter fullscreen mode Exit fullscreen mode
<input data-color="lightgray" />
<input data-color="gray" />
Enter fullscreen mode Exit fullscreen mode

Both input will turn out gray because gray is found in both lightgray and gray

If you use ~= (exact word match) you avoid this problem

[data-color~="lightgray"] { color: whitesmoke; }
[data-color~="gray"] { color: gray; }
Enter fullscreen mode Exit fullscreen mode

Thanks for sharing

Collapse
 
madsstoumann profile image
Mads Stoumann

Thanks! You're right about multiple modifiers, of course – I'll update the Pens.