CSS选择器中的空格 [英] Whitespace in CSS selectors

查看:384
本文介绍了CSS选择器中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在选择器中,空格可以指示后代.但是,我看到空格也可以忽略.

In the selector a space can indicate a descendant. However, I see spaces can also be ignored.

根据我的测试,这是我认为的意思:

From my testing, here is what I think it means:

  • ,和上下文字符(例如+>)周围可以使用空格.
  • 不能在伪类和元素字符(:::)以及属性选择器([…])周围使用空格.
  • 空格不能与类和id字符(.#)一起使用.
  • 字符串"中的空格很重要.
  • 所有其他空格都将被忽略.这包括在属性选择器内填充.
  • Spaces may be used around , and contextual characters (such as + and >).
  • Spaces may not be used around pseudo class and element characters (: and ::) as well as attribute selectors ([…]).
  • Spaces may not be used with class and id characters (. and #).
  • Spaces inside a "string" are significant.
  • All other spaces are ignored. This includes padding inside an attribute selector.

问题是(a)这在官方上是正确的吗?和(b)覆盖范围是什么?

The question is (a) is this officially correct? and (b) where is this covered?

推荐答案

您的所有结论都是正确的.属性选择器中的空格存在细微差别,我对

All of your conclusions are correct. There are nuances with regard to whitespace in attribute selectors, covered in my answer to this question.

语法<.出于语法目的,您所指的上下文字符(例如+>)"被正式称为组合符. (术语上下文选择器"最初在CSS1中使用,但此后没有出现.)

All the exact rules of where whitespace may or may not appear are covered in the grammar. For the purposes of the grammar, the "contextual characters (such as + and >)" that you refer to are officially known as combinators. (The term "contextual selector" was first used in CSS1 but hasn't appeared since.)

还要记住,分隔两个简单选择器的任意数量的连续空格字符都被视为后代组合符,这实际上是为什么在伪元素,伪类,属性选择器,类选择器和ID选择器-因为它具有重要性,因此它的存在会改变选择器的含义.

Remember in addition that any number of contiguous whitespace characters that separate two simple selectors is considered a descendant combinator, which is in fact one reason why whitespace isn't "allowed" around the delimiters for pseudo-elements, pseudo-classes, attribute selectors, class selectors and ID selectors — because it has significance and therefore its presence alters the meaning of the selector.

这篇关于CSS选择器中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆