Html 如果html显示<div id="two words",我该如何在样式表中编写CSS选择器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/249878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:37:13  来源:igfitidea点击:

If the html says <div id="two words", how do I write the CSS selector in the style sheet?

htmlcss

提问by Corey Trager

If it said "oneword", then I could write "#oneword", but what do I write when there is a space in the word?

如果它说“oneword”,那么我可以写“#oneword”,但是当单词中有空格时我该怎么写?

回答by tvanfosson

If it contains spaces, it is not legal HTML. You shouldn't expect this to work. Here is the relevant section of the HTML 4.01specification.

如果它包含空格,则它不是合法的 HTML。你不应该期望这会奏效。这是HTML 4.01规范的相关部分。

[EDIT] As others have noted, you can get around this by assigning one or more class names to the div and using a class name to do the selection.

[编辑] 正如其他人所指出的,您可以通过将一个或多个类名分配给 div 并使用类名进行选择来解决此问题。

回答by Philip Morton

You can't have multiple words for the id, but you can for class.

id 不能有多个单词,但可以用于 class。

<p class="one two">lalala</p>

.one {
    color: black;
}

.two {
    font-weight: bold;
}    

回答by gauravgupta

Write Like this

像这样写

div[id='two words']{

div[id='两个字']{

}

}

this will recognize the id as combination of both 'two' and 'words'.

这会将 id 识别为“两个”和“单词”的组合。

回答by Aron Rotteveel

You should not use whitespace in the ID element, as whitespace is generally accepted as a selector combinator.

不应在 ID 元素中使用空格,因为空格通常被接受为选择器组合符

回答by Ryan Rodemoyer

Change it to two-words. Like others said, you cannot use spaces for the id, but you can for the class.

改成两个字。就像其他人说的那样,您不能为 id 使用空格,但可以为班级使用空格。

#two-words { font-family: arial; }
.center { text-align: center; }
.bold { font-weight: bold; }

<div id="two-words" class="center bold">STUFF HERE</div>

回答by Nuc134rB0t

If you make websites with IDEs like Artisteer, you can see a working id="two words"which becomes a class

如果您使用 Artisteer 之id="two words"类的IDE 制作网站,您会看到一个工作变成了一个类

.two{
}

.words{
}

I don't know if this is a good practice, but it works.

我不知道这是否是一个好的做法,但它有效。