编写 CSS 元素/分区的正确方法?
我正在学习 CSS,只是想知道编写 CSS 时哪种方式最好:
div.divname
或 .divname
ul#menu
或 #menu ul
等等
I'm learning CSS and was just wondering which way is best when writing CSS:
div.divname
or .divname
ul#menu
or #menu ul
etc etc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您指定的每个元素之间都存在差异。
div.divname
将匹配类名为“divname”的所有分区。
.divname
将匹配类名为“divname”的所有元素(分区、段落、锚点、所有内容)。
ul#menu
将匹配具有 ID 'menu' 的无序列表。
#menu ul
将与 ID 为“menu”的元素中包含的无序列表进行匹配。
除了解释这些之外,我不知道你到底想做什么。我所能建议的就是尽可能避免指定可以与任何元素匹配的类名,并记住在单个文档中只有一个元素可以具有特定的 ID。从第一个示例来看,
div.divname
会更好。从第二个例子来看,它们完全不同。There's a difference between every element you've specified.
div.divname
Will match all divisions with the class name 'divname'.
.divname
Will match all elements (divisions, paragraphs, anchors, everything) with class name 'divname'.
ul#menu
Will match the unordered list that has the ID 'menu'.
#menu ul
Will match the unordered list that is contained within the element with the ID 'menu'.
Other than explaining these, I don't know exactly what you're trying to do. All I can recommend is avoid specifying class names that can match to any element as much as possible, and remember that only one element can have a specific ID inside a single document. From the first example,
div.divname
would be better. From the second example, they are both completely different.