'property: 0' or 'property: 0px' in CSS?

后端 未结 9 1685
无人及你
无人及你 2020-11-28 10:38

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;
}


        
相关标签:
9条回答
  • 2020-11-28 10:38

    Zero of anything is zero. 0px = 0% = 0em = 0pt = 0

    Most people leave the unit off because it is simply unnecessary clutter.

    0 讨论(0)
  • 2020-11-28 10:40

    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).

    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.

    0 讨论(0)
  • 2020-11-28 10:41

    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:

    • 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
    0 讨论(0)
  • 2020-11-28 10:47

    They are the same. The browser interprets both as 0, so go with whatever is more readable for you.

    0 讨论(0)
  • 2020-11-28 10:52

    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 讨论(0)
  • 2020-11-28 10:54

    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 讨论(0)
提交回复
热议问题