应该在CSS中用空格括起来的属性

问题描述 投票:2回答:1

我正在从W3schools学习HTML和CSS。我遇到了一个令人困惑的指示。 HTML5 Style Guide of W3schools在“样式表”部分中说“如果值包含空格,则仅在值周围使用引号”。它给出了一个看起来像这样的示例:

body {
  background-color: lightgrey;
  font-family: "Arial Black", Helvetica, sans-serif;
  font-size: 16em;
  color: black;
}

font-family属性的引号为Arial Black。但是,当我执行W3schools CSS background Exercise时,以下代码并未将背景图片放在右上角位置

<head>
  <style>
    body {
      background-image: url("img_tree.png");
      background-position: "right top";
      background-repeat: no-repeat;
    }
  </style>
</head>

我删除了“右上角”周围的引号后,背景图片放置在右上角位置。经过一番研究,我从this post中了解到CSS属性通常不能被引用。

现在我很困惑。从W3schools样式指南中,似乎建议使用空格引用CSS属性。但这不起作用!我什么时候必须报价,什么时候应该报价(以推荐标准为准)?

css properties styles quote
1个回答
0
投票

什么时候必须引用,什么时候应该引用(按照推荐标准)?

没有标准,也没有建议。每个属性都有自己需要遵循的语法。就这么简单。

对于background-position和定义为in the specification:

<bg-position> =  [ left | center | right | top | bottom | <length-percentage> ]
|
  [ left | center | right | <length-percentage> ]
  [ top | center | bottom | <length-percentage> ]
|
  [ center | [ left | right ] <length-percentage>? ] &&
  [ center | [ top | bottom ] <length-percentage>? ]

您可以遵循语法来理解|&&等的含义,并且您会看到<string>(引用的值)不允许作为background-position的值

相关:Why are CSS named grid areas not in quotes?

© www.soinside.com 2019 - 2024. All rights reserved.