How to name an element? #1753
Replies: 3 comments 4 replies
-
Personally, I use a class with a prefix of
I also generally follow a BEM-like naming convention for subcomponents (and modifiers when there are styles associated to these classes). Here's an example: <button type="button" class="c-button c-button--dark text-lg">
<span class="c-button-icon"><svg>...</svg></span>
<span class="c-button-text">Click me</span>
</button> Your |
Beta Was this translation helpful? Give feedback.
-
While there are different approaches to this, I think html comments are the easiest way so solve this. See the screenshot below. It's especially useful to name very similar elements like I have here. There is no way I can see what image in the svg code represent. In this particular example I'm using Electron + Tailwind + AlpineJS which works great. |
Beta Was this translation helpful? Give feedback.
-
Not a solution to your problem, but my approach: If I feel that I'm starting to loose components, I will add something to the project to make components easier, usually React. You can come with the best naming strategy in HTML, but sooner or later you'll be lost scrolling "where is that component", or reading every comment, while with the most simple component strategy you would just need to look for a file. If I were limited to just HTML, I'd create an HTML page for the components and render them like a list, so I could just open this in the browser and view them, like a shelf. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
One awesome thing with Tailwind is that we don't need to think of names for everything because we are using utility classes.
One downside of that is that it can sometimes be hard to quickly see which element is THAT button we are looking for among five others. That's because we don't have any names on them.
Which is the best approach to bring back names in cases when we need them?
My own thoughts
PHP comment
In case we use PHP (like I do), we can add a comment in PHP like:
It's a bit ugly but it would work and it would leave nothing in the HTML output which I think is a good thing.
HTML comment
Similar to the PHP comment but it's less ugly in my opinion. The downside is that it will get rendered on the output source.
HTML ID
A html ID would not take too much space. The downside is that it's not ment to show the name in the HTML, but instead provide a function for that element.
HTML data attributes or HTML classes
Same as with ids.
Your thoughts?
Which approach would you use and why?
Beta Was this translation helpful? Give feedback.
All reactions