为什么 CSS 使用冒号“:”在规则中?

发布于 2024-10-20 06:02:06 字数 133 浏览 3 评论 0原文

CSS 属性不使用空格,那么为什么规则中的第一个空格不用作属性和值之间的分隔符呢?

这是一个示例:

在此处输入图像描述

CSS properties use no spaces so why isn't the first space in the rule used as a separator between the property and the value?

Here's an example:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

蓝海 2024-10-27 06:02:06

我不确定是否可以给出比“可能是为了避免混淆”更好的答案。

编程和标记语言中的许多元素并不是 100% 有利于提高可读性。我个人觉得

background: red 1px url(images/test.jpg)

background red 1px url(images/test.jpg);

I am not sure whether a better answer can be given than "probably to avoid confusion".

Many elements in programming and markup languages are not 100% efficient in favour of improved readability. I personally find

background: red 1px url(images/test.jpg)

much, much clearer than

background red 1px url(images/test.jpg);
空城旧梦 2024-10-27 06:02:06

与缺失的冒号相比,意外的空格更容易插入,但更难发现。

An accidental space is easier to put in and harder to spot than a missing colon.

七色彩虹 2024-10-27 06:02:06

为什么不把大括号也去掉,让缩进决定结构呢?那会更干净。

然而,有时更好的语法并不是删除你能想象到的所有内容,你通常还需要保留一些东西来保持语法清晰。更清晰的语法比更清晰、更隐含的语法能更好地表达意图。使用显式语法更难犯错误,并且更容易发现这些错误。

在你的两个例子中,我发现第一个更容易理解,因为键和值是清楚、明确地分开的。

Why not remove the curly-braces as well and let indentation dictate the structure? That would be even cleaner.

However, sometimes better syntax is not about removing everything you can imagine, you often need to keep something to keep the syntax clear as well. A syntax that is more clear expresses the intention in a better way than a cleaner, more implicit syntax. It's harder to make mistakes with an explicit syntax, and easier to spot those mistakes.

In your two examples, I find the first one a lot easier to understand, because the key and the value are clearly, explicitly separated.

空心↖ 2024-10-27 06:02:06

它只是更容易使用。它更具语义性,并且如果他们以后更改规则,它也是面向未来的。它还使调试更加容易。

这只是可用性高于效率的一个例子。

It's just easier to use. It's more semantic and it's future proof if they change the rules at a later date. It also makes debugging easier.

It's just an instance of usability over efficiency.

苏别ゝ 2024-10-27 06:02:06

正如其他人所说,需要提供一种清晰的语法,该语法足够灵活以选择您的样式(在一行中编写规则?每行编写一个键:值?等等)。

让我举个例子。你可以这样写:

a {
  border: 1px
  solid black;
}

如果省略所有语法字符,这将变成:

a {
  border 1px
  solid black
}

这是不明确的,或者从另一个角度来看,语法太僵化。

As others stated, it is needed to give a clear syntax, which is flexible enough to choose your style (write rules in one line? write one key:value per line? etc.).

Let me give you an example. You could write this:

a {
  border: 1px
  solid black;
}

If you leave out all the syntax characters, this will become:

a {
  border 1px
  solid black
}

This is ambigous, or from the other point of view, the syntax is too rigid.

场罚期间 2024-10-27 06:02:06

许多语言使用 variableName: type 表示法(变量、参数等的声明)。我看到一个使用 variableName type (也许是 Go) 我对这个表情感到不安。即使明确,也“感觉不对”,也许是习惯,也许是因为缺乏“节奏”。英语句子中的标点符号并不总是必要的(有时是为了避免歧义),但缺乏标点符号会让读者感到不舒服。眼睛需要寻找标记。

同上 F# (及其父语言),只需省略括号和逗号函数参数。它看起来很奇怪(但也许人们会对这种表示法上瘾)。

与往常一样,需要在注释过多和注释过少之间取得平衡。在创新和打破习惯之间……创新固然很好,但它必须有一个目的。以牺牲可读性为代价来节省一些击键很少是目的。 :-)

Lot of languages use the variableName: type notation (declarations of variables, of parameters, etc.). I saw one using variableName type (perhaps it was Go) and I was disturbed by the look. Even if it is unambiguous, it "doesn't feel right", perhaps because of habits, perhaps because it is lacking "rhythm". Punctuation in an English sentence isn't always necessary (sometimes it is, to avoid ambiguity), but lacking it makes readers uncomfortable. The eye needs to find marks.

Idem for F# (and its parent language), just omitting parentheses and commas for function parameters. It looks odd (but perhaps one can get addicted to this notation).

As always, there is a balance to strike, between too much annotations and too little. And between innovating and breaking habits... Innovation is nice, but it must have a purpose. Saving some keystrokes at the cost of readability is rarely a purpose. :-)

你是我的挚爱i 2024-10-27 06:02:06

我敢打赌,大多数 CSS 解析器都有一些用 C 或 C++ 编写的历史,并且 strtok 函数用于解析文件,遵循解析 C/C++ 文件的类似规则。

其他人已经提到使用空格分隔符的歧义。

; 是 C 和 CSS 语法的另一个重要部分。

I'd be willing to bet that most CSS parsers have some history of being written in C or C++ and the strtok function being used to parse the file, along similar rules for parsing a C/C++ file.

Others have already mentioned the ambiguity of using a white space separator.

The ; is another important part of the syntax in both C and CSS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文