在 CSS 中指定 0 值时,我应该明确标记单位还是省略单位?

发布于 2024-12-12 07:35:01 字数 318 浏览 3 评论 0原文

这更多的是一个“哲学”论证,但我想知道这里推荐的做法是什么。我还没有将其设置为 Wiki,以防有“官方”答案。

显然,0px 和 0em 或其他什么之间没有区别,因此可以简单地指定 0 并且单位是多余的(请参阅 0 和 0em 之间的 CSS 差异)。一些回答该问题的人认为应该始终省略单位。

然而,在我看来,省略单位更容易出错,因为以后的更改可能会意外地省略单位。它与文档中其他地方的非零元素也不太一致。

This is more of a 'philosophy' argument, but I'd like to know what the recommended practice here. I'm not setting it up as a Wiki yet in case there is an 'official' answer.

Obviously, there is no difference between 0px and 0em or whatever, so one could simply specify 0 and the units are redundant (see CSS difference between 0 and 0em). Some of the folks who answered that question argued that one should always omit the units.

However, it seems to me that omitting the unit is more error-prone, since a later change may accidentally omit the unit. It is also less consistent with non-zero elements elsewhere in the document.

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

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

发布评论

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

评论(8

林空鹿饮溪 2024-12-19 07:35:01

我认为你也应该省略单位。

从程序员的角度来看,0 == null == none == false,其中仅0px == 0px

这意味着如果您指定 0 的边框宽度,则不会有边框,但如果您指定 0px 边框,则会创建 0 px 的边框(这就是其背后的想法,实际上 0px 给出的结果与 0 完全相同)。

进一步的点

  • 无单位 0 使其更易于阅读,因为它很容易与正常的单位值区分开来。
  • 删除这些单位是有意义的,因为它们在那里没有意义(0 可能意味着大小、颜色等)。

结论:省略 0 中的单位。它们不是必需的,而且会造成混淆。

I argue you should also omit the units.

From a programmer's perspective, 0 == null == none == false, where 0px == 0px only.

Which means that if you specify a border width of 0 then no border will be there, but if you specify a 0px border, then a border of 0 px will be created (that's the idea behind it, in reality 0px gives the exact same result like 0).

Further Points

  • unit-less 0 makes it easier to read as it is easily distinguishable from normal unit'ed values.
  • It makes sense to remove the units as they have no point in being there (0 could mean size, color, etc.).

Conclusion: Omit the units in 0. They're not needed and confusing.

弥枳 2024-12-19 07:35:01

作为这个问题的注释:无单位 0 作为长度在 calc() 和其他数学函数中不起作用。您必须0px。如果省略 0 的单位是从某个 css 变量中获取的,事情会更加麻烦。

规范

注意:由于始终被解释为,因此数学函数不支持“无单位 0”。即宽度:calc(0 + 5px);无效,因为它正在尝试添加一个,即使 width: 0;宽度:5px;有效。

.a, .b { border: 10px solid; height: 30px; }
.a { width: calc(300px + 0px); border-color: #f00; }
.b { width: calc(300px + 0); border-color: #330; }
<div class="a">width: calc(300px + 0px);</div>
<div class="b">width: calc(300px + 0);</div>

我建议在编写 CSS 变量时始终使用0px。因此,当您和其他人尝试在某些 calc() 函数中使用该变量时,它不会让您和其他人感到困惑。

As a note to this question: Unitless 0 as length won't work in calc() and other math functions. You have to write 0px. And things would be more buggy if the unit omitted 0 is taken from some css variable.

Specification:

Note: Because <number-token>s are always interpreted as <number>s or <integer>s, "unitless 0" <length>s aren’t supported in math functions. That is, width: calc(0 + 5px); is invalid, because it’s trying to add a <number> to a <length>, even though both width: 0; and width: 5px; are valid.

.a, .b { border: 10px solid; height: 30px; }
.a { width: calc(300px + 0px); border-color: #f00; }
.b { width: calc(300px + 0); border-color: #330; }
<div class="a">width: calc(300px + 0px);</div>
<div class="b">width: calc(300px + 0);</div>

I would suggest always use 0px when you are writing CSS variables. So it won't make you and others confuse when they are trying to use the variable in some calc() functions.

不知所踪 2024-12-19 07:35:01

CSS 规范 说(引用):

零长度之后,单位标识符是可选

我已经读过很多次建议省略该单位,我不记得在哪里了。

所以省略它们。

The CSS specification says (quote):

After a zero length, the unit identifier is optional.

And I've read many times that it is suggested to omit the unit, I can't remember where.

So omit them.

临走之时 2024-12-19 07:35:01

我还喜欢将单位放在零前面,因为...

  1. IDE 喜欢在那里保存值,这样它们就可以正确地递增它们,正如 Istvan 提到的那样。
  2. 如果我输入另一个值,我不必稍后输入单位。而且我不必尝试并记住如果单位为零则删除该单位,或者如果不是零则将其放回去。
  3. 一位发帖者表示,裸露的零更具可读性。我发现它不是。当有一个单元时,你就有了上下文,这使得它更具可读性。例如,如果我遇到百分号,我知道我使用它的方式与“px”单位不同,所以我立即知道它可能是什么。
  4. 另一个人,“红色”,有这个要说,看起来喜欢好的信息。这是一个片段...

    <块引用>

    但是,如果您打算更改级联中的先前值,您会注意到它。 '0em' 表示“将先前的值更改为 '0em”,但零可能只是表示“忽略此规则”并保持先前​​的规则有效。这可能根本不是你对赤裸裸的“0”的初衷。 原始文章链接

I also like to put units by my zeros because ...

  1. IDEs like to have the values there so they can increment them properly, as Istvan mentioned.
  2. I don't have to type the unit later if I put another value in. And I don't have to try and remember to remove the unit if it's zero or put it back if it's not.
  3. One poster said the naked zero was more readable. I find that it's not. When there is a unit, you have context, and that makes it more readable. For example, if I come across a percent sign, I know I use that in different ways than the "px" unit, so I instantly know some of the things it could be.
  4. Another guy, "red", had this to say, which looks like good info. Here's a snippet...

    However, you will notice it if you intended to change a prior value in the cascade. a '0em' says, 'change the prior value to '0em', but a zero may simply say, 'disregard this rule' and leave the prior rule in effect. This may not be at all what you intended with your naked '0'. original article link

你对谁都笑 2024-12-19 07:35:01

我说省略单位,这将有助于压缩 CSS 并提高性能。虽然不多,但每一点都有帮助。

I say omit the unit, this will help when you compress the CSS and increase performance. It isn't much, but every little bit helps.

计㈡愣 2024-12-19 07:35:01

在当前的浏览器开发人员工具中,您可以使用上/下光标键轻松调整实时值,但如果您指定不带单位的 0 ,那么它将不起作用,因为浏览器不知道什么单位你喜欢吗,设置 1, 2... 没有单位没有意义。

这就是为什么我现在也总是将单位指定为零值。任何 CSS 缩小器都会在缩小时将其更改为 0,因此您甚至不需要关心它。

In current browsers developer tools you can easily tweak the values live with the up/down cursor keys, but if you specify 0 without a unit, then it will not work, as the browser will not know what unit do you prefer, and setting 1, 2... without a unit has no meaning.

That is why I am always specifying the unit nowadays on zero values too. Any CSS minifier will change that to 0 on minification so you don't even need to care about it.

生来就爱笑 2024-12-19 07:35:01

我总是省略,因为它看起来更容易阅读,零会弹出,并且很容易与它周围的带单位值区分开来。

I always omit as it seems easier to read, the zero pops out and is easily distinguishable from the with-unit values around it.

丢了幸福的猪 2024-12-19 07:35:01

我逐渐开始相信零和单位。

我遵循风格指南,要求使用无单位零。我也觉得他们更好看。我同意这里所有支持无单位的论点。但在我的职业生涯中,有两次错误是由无单位零引起的。 (一个与calc有关,另一个与从0到某物的转换有关 - 但我不记得细节了。)

等式一侧的“导致错误”可以是任何无论美学上多么令人愉悦或合乎逻辑,支持无单位的论证都会相互竞争吗?


顺便说一句,我最初将无单位零样式归因于 Airbnb 的指南,但在重读之后,它实际上并没有明确说明,但很接近: https://stackoverflow.com/a/55391061/2836695

I'm gradually starting to believe in zeros with units.

I follow a style guide that says to use unitless zeros. I also think they look better. I agree with all the pro-unitless arguments on here. But twice in my career a bug has been caused by unitless zeros. (one having to do with calc and one having to do with a transition from 0 to something - but I can't remember the details.)

With "causes bugs" on one side of the equation can any pro-unitless argument compete no matter how aesthetically pleasing or logical?


BTW, I originally attributed the unitless zero style to Airbnb's guide, but after rereading it, it doesn't actually say that explicity, but comes close: https://stackoverflow.com/a/55391061/2836695

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