按含义或外观对 CSS 元素进行分类?
为元素选择 CSS 类(名称)的最佳实践是什么?
a) 我应该按照它的本质(本例中的预订链接)命名它,并在 CSS 中将所有具有相同外观的类分组吗?
<a href="booking.html" class="bookingLink">Book this course!</a>
使用CSS:
a.bookingLink, a.otherLink, a.someLink { color: red; font-weight: bold }
b)或者我应该按照它的样子来命名它?
<a href="booking.html" class="boldLink">Book this course!</a>
使用CSS:
a.boldLink { color: red; font-weight: bold }
我目前发现自己为链接添加了越来越多的类,这些类看起来应该相同,但具有不同的含义。当然,这也需要对 CSS 进行更改。
另一方面,如果我想更改特定类型链接的外观,我可以稍后通过 CSS 更灵活地更改外观。 (仅以预订链接为例)
您喜欢哪种方式?
Whats the best practice for choosing a CSS class (name) for an element?
a) Should I name it after what it is (a booking link in this example) and group all classes with the same looks in CSS?
<a href="booking.html" class="bookingLink">Book this course!</a>
with CSS:
a.bookingLink, a.otherLink, a.someLink { color: red; font-weight: bold }
b) Or should I rather name it after what it looks like?
<a href="booking.html" class="boldLink">Book this course!</a>
with CSS:
a.boldLink { color: red; font-weight: bold }
I currently find myself adding more and more classes for links that should look the same, but have a different meaning. Of course this makes changes in CSS necessary, too.
On the other hand, I can change the looks more flexible via CSS later if I want to change the look of a specific type of link. (only the booking links for example)
What method do you prefer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
保持类名尽可能具有语义被认为是最佳实践,因此 bookingLink.
我可以看到你来自哪里,因为它确实增加了你必须编写的代码量。
这是 W3c 的一篇关于此问题的文章,在大多数情况下我尝试遵循他们的指南。
http://www.w3.org/QA/Tips/goodclassnames
只要你保持你的代码可读、一致和可维护我认为一点你自己的风格不会有什么坏处。
It's considered best practise to keep class names as semantic as possible, so bookingLink.
I can see where you are coming from though as it does add to the amount of code you have to write.
Here's an article from W3c about this issue, I try to keep to their guidelines in most cases.
http://www.w3.org/QA/Tips/goodclassnames
As long as you keep your code readable, consistent and maintainable I think a little bit of your own style can't hurt.