按含义或外观对 CSS 元素进行分类?

发布于 2024-10-14 17:00:11 字数 667 浏览 2 评论 0原文

为元素选择 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城半夏 2024-10-21 17:00:11

保持类名尽可能具有语义被认为是最佳实践,因此 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文