'property: 0' 或 'property: 0px' 在 CSS 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4318471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'property: 0' or 'property: 0px' in CSS?
提问by Blender
I've seen this notation used a lot, and I was wondering, is there is any notable difference between these two notations?
我已经看到这个符号被大量使用,我想知道这两个符号之间有什么显着区别吗?
element#id
{
property: 0;
}
and
和
element#id
{
property: 0px;
}
I use property: 0px;
all the time, as I find it cleaner looking, but I'm not really sure if the browser interprets 0px
differently than 0
.
我用property: 0px;
所有的时间,因为我觉得它更干净外观,但我真的不知道,如果在浏览器解释0px
不同于0
。
Does anyone know which one is better or correct?
有谁知道哪个更好或更正确?
采纳答案by Blender
While the unit is optional when the value is 0
, I tend to leave it in, as I can then tweak the values with Chrome's Developer Tools by clicking on the value and pressing the up/down arrow keys. Without a unit, that isn't really possible.
虽然当值为 时单位是可选的0
,但我倾向于保留它,因为我可以通过单击该值并按向上/向下箭头键使用 Chrome 的开发人员工具调整这些值。没有单位,那是不可能的。
Also, CSS minifiers strip the units off of 0
values anyways, so it won't really matter in the end.
此外,CSS 缩小器0
无论如何都会从值中剥离单位,所以最终它并不重要。
回答by Chris Bier
Unit identifiers are optional, but there is no noted performance increase (although you aresaving two characters).
单位标识符是可选的,但是没有着名的性能提升(虽然你是节省了两个字符)。
CSS2 -From W3C CSS 2.1 Specification for Syntax and basic data types:
CSS2 -来自W3C CSS 2.1 语法和基本数据类型规范:
The format of a length value (denoted by <length> in this specification) is a <number> (with or without a decimal point) immediately followed by a unit identifier (e.g., px, em, etc.). After a zero length, the unit identifier is optional.
长度值的格式(在本规范中用<length>表示)是一个<number>(带或不带小数点)紧跟一个单位标识符(例如px、em等)。在零长度之后,单位标识符是可选的。
(Emphasis mine)
(强调我的)
CSS3 -From W3C CSS Values and Units Module Level 3(Currently in Candidate Recommendationat the time of this writing)
CSS3 -来自W3C CSS Values and Units Module Level 3(在撰写本文时目前处于候选推荐中)
For zero lengths the unit identifier is optional(i.e. can be syntactically represented as the 0).
对于零长度,单位标识符是可选的(即可以在语法上表示为 0)。
回答by Sebastian Patane Masuelli
They are the same. The browser interprets both as 0, so go with whatever is more readable for you.
他们是一样的。浏览器将两者都解释为 0,因此请选择对您来说更易读的内容。
回答by Chuck
Zero of anything is zero. 0px
= 0%
= 0em
= 0pt
= 0
任何东西的零都是零。0px
= 0%
= 0em
= 0pt
=0
Most people leave the unit off because it is simply unnecessary clutter.
大多数人不使用该设备,因为它只是不必要的混乱。
回答by AgentConundrum
As far as I'm aware there is no difference between them, since 0px = 0em = 0ex = 0% = 0
. It's purely up to you, as the developer, to decide what you like best (unless you have corporate coding standards that you need to follow, of course).
据我所知,它们之间没有区别,因为0px = 0em = 0ex = 0% = 0
. 完全由您作为开发人员来决定您最喜欢什么(当然,除非您有需要遵循的公司编码标准)。
From most of the code samples I've seen, most people use the unitless version. To me, it just looks cleaner. If you're pushing a significant amount of data (say, if you're Google), those two bytes can add up to a lot of bandwidth, especially since you're quite likely to repeat them multiple times in your stylesheet.
从我见过的大多数代码示例来看,大多数人都使用无单元版本。对我来说,它看起来更干净。如果您要推送大量数据(例如,如果您是 Google),那么这两个字节可能会增加大量带宽,尤其是因为您很可能在样式表中多次重复它们。
回答by meder omuraliev
Zero pixels is equal to zero inches and zero meters and so forth. 0
is all you need.
零像素等于零英寸和零米等等。0
是你所需要的全部。
回答by Amir Raminfar
I personally find 0
cleaner than 0px
. That's two extra characters that can add up. Why add extra bytes when you don't need to. I have see padding: 0px 0px 0px 0px
which can easily be expressed as padding: 0
way too often.
我个人觉得0
比0px
. 这是可以加起来的两个额外字符。为什么在不需要时添加额外的字节。我已经看到padding: 0px 0px 0px 0px
哪些可以很容易地表达为padding: 0
过于频繁。
回答by Chris Halcrow
You can use either - my best advice is not to worry too much but be consistent in doing it either one way or the other. I personally prefer to specify '0px' for the following reasons:
你可以使用任何一种——我最好的建议是不要太担心,而是要以一种或另一种方式一致地去做。我个人更喜欢指定“0px”,原因如下:
- Using 0px makes things more consistent with all of the other 'px' sizes you've specified
- It's also more verbose and makes it very clear that you're setting a zero length rather than a 'switch this off' flag
- It's slightly easier to tweak a '0px' value to make it another value if required
- 使用 0px 使事情与您指定的所有其他“px”尺寸更加一致
- 它也更加冗长,并且非常清楚地表明您正在设置零长度而不是“关闭此功能”标志
- 如果需要,调整“0px”值使其成为另一个值会更容易一些
回答by danjah
As the others say, it doesn't really matter if its 0, though I choose to add the measurements to all of my values so anyone else looking at my CSS files can gauge what measurements they're likely to deal with elsewhere.
正如其他人所说,它是否为 0 并不重要,尽管我选择将测量值添加到我的所有值中,以便其他查看我的 CSS 文件的人可以衡量他们可能在其他地方处理的测量值。