本文介绍了何时离开空间,何时不在CSS中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是确定的(没有空格):

This is ok(without space):

li.highlight{
    background:#FF9900 none repeat scroll 0 0;
}

这不起作用(有空格):

This will not work(with space):

li .highlight{
    background:#FF9900 none repeat scroll 0 0;
}

为什么?

推荐答案

后面的选择器将不工作,因为空间意味着选择器之间的关系(在这种情况下是后代)。

The latter selector won't work because the space implies the relationship (in this case a descendant) between the selectors.

li.highlight /* defines an element of 'li' with a classname of 'highlight' */

li .highlight /* defines an element of class 'highlight' that's contained within an li element */

li > .highlight /* as pointed out by Neil (in comments), this would select an element of class highlight that is an immediate child/descendant of an li */

我应该解释我使用的短语不会工作。显然,我使用了你的短语,我这样做的错误。

I should explain my use of the phrase "won't work." Clearly I used your phrasing, and I did so in error.

它会工作,它只是它会选择 - 评论 - 您在标记中没有的元素。

It will work, it's just that it will select -as explained in the comment- an element that you don't have in your markup.

这篇关于何时离开空间,何时不在CSS中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 11:50