Yesterday, Glenn Maddern introduced a new concept for styling HTML websites : the Attributes Modules. His explanation being very thorough and well-written, you should read it first.
I used it today for a small pen about language learning card :
The HTML uses attributes (starting with am-attribute so I know it’s part of this module, it’s very much like Angular’s ng-stuff) instead of classes.
The code :
Here I have three modules :
- Card :
am-Cardto style Cards in general - Lang :
am-Langto make sure the user can recognize pinyin (latin-characters chinese) at a glance for example. - Question :
am-Question, which will hide and show blocks of text, so the user has to guess them.
The HTML template for my Card :
1 2 3 4 5 | |
The CSS I use :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
Pro’s and Con’s
- You never have to worry about two
.hiddenclasses with different behaviors. - You won’t have huge
Card__Question--hiddenclasses like BEM does, because each module has its own attribute (avoids Global Namespace !). - Since all your attributes have meaning, I think we can use the same attributes for CSS and JS. The attribute only shows an information, CSS and JS can do whatever they want afterwards.
- But it’s not very pretty to have
[am-Question~="shown"]everywhere in your css…
The full Pen :
See the Pen Hide some text by Pierre (@Marekje) on CodePen.