简体   繁体   English

具有嵌套CSS的类与ID

[英]Class vs ID with nested CSS

Suppose a HTML like this: 假设这样的HTML:

<div id="header">
  <span class="title">Title</span>
  <!-- more spans and other things here -->
</div>

This would work together with a nested CSS: 这将与嵌套的CSS一起使用:

#header .title { /* CSS */ }

This works of course, but I don't like the usage of class here. 这当然有效,但我不喜欢这里的class用法。 As I need the style title only once, I would like to use an id. 由于我只需要一次样式title ,我想使用id。 But then the name would have to be something like header_title (since I might have other titles in the HTML), resulting in a CSS 但是这个名称必须是header_title (因为我可能在HTML中有其他标题),导致CSS

#header #header_title { /* CSS */ }

Now this seems to defeat the purpose of nested CSS, then I could just drop the first #header completely. 现在这似乎打败了嵌套CSS的目的,然后我可以完全删除第一个#header

I can't really figure out a way to do this "right". 我无法找到一种“正确”的方法。 Am I missing something, or do I just have to live with some "dirty" code here? 我错过了什么,或者我只需要在这里使用一些“脏”代码?

Using #id .class {style:rules;} is not "dirty". 使用#id .class {style:rules;}不是“脏”。 It is the correct way of doing it. 这是正确的做法。 Also, if you "might have other titles in the HTML", it would be even more correct to use classes rather than have 15 id based rules. 此外,如果您“可能在HTML中有其他标题”,那么使用类而不是基于15个id的规则会更加正确。

It actually doesn't really matter. 它实际上并不重要。

What matters about your markup is that it's readable ; 标记的重要之处在于它的可读性 ; HTML is about being semantic , so that your markup represents your content. HTML是关于语义的 ,因此您的标记代表您的内容。 By doing so, if you come back to your HTML a few months later without touching it, you should be able to quickly pick up on what on earth you wrote :) 通过这样做,如果你几个月后回到你的HTML而不触及它,你应该能够快速了解​​你写的地球:)

Semantically, #header .title makes a lot more sense to me over #header #header_title , for two reasons; 在语义上, #header .title使得很多更有意义,我过#header #header_title ,原因有两个; one, since it's easier to read, and two, since the purpose of ids is, well, to identify! 一,因为它更容易阅读,两个,因为ids的目的是,确定! You could use #header_title by itself, but it's much cleaner to limit the amount of ids you have. 您可以#header_title使用#header_title ,但是限制您拥有的ID数量会更加清晰。

How about using header tags 如何使用标头标签

#title h1{/* CSS */}

<div id="header">
    <h1>Title</h1>
</div>

I guess that's what the tags are there for :) 我猜这就是那里的标签:)

Use an ID if there is only one element you want to style since IDs cannot be duplicated. 如果只有一个要设置样式的元素,请使用ID,因为ID不能重复。 If there is a possibility that there could be multiple elements with the class title (both in and outside of the #header div), then stick with your first CSS example as this will ensure that only elements with the title class that are inside of the #header element will be styled. 如果有可能有多个带有类title元素(在#header div的内部和外部),那么请坚持使用您的第一个CSS示例,因为这将确保只有标题类的元素位于#header元素将被设置样式。

It really depends on what the rest of your HTML looks like and what you are trying to style. 这实际上取决于HTML的其余部分以及您要设计的样式。

Seems like you're more worried about naming convention. 好像你更担心命名约定。

It's totally okay to just use 只使用它是完全可以的

 #header #title {/** css **/}

In your case the best method is probably to use the "dirty" method. 在您的情况下,最好的方法可能是使用“脏”方法。

I'm not sure why you would choose to use span over h1, h2 because I would just do 我不确定为什么你会选择使用跨越h1,h2,因为我会这样做

 #header h1 {/** css **/}

since you most likely will only have one h1 tag within your #header 因为你很可能只在#header中有一个h1标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM