Question

Cette question a déjà une réponse ici:

recherche sur Google pour la réponse à cette question est avérée difficile, donc je quelqu'un ici figurée devrait savoir.

Dans CSS, je l'ai vu zéro pixels déclarés comme simplement '0' mais aussi comme '0px'.

mystyle { width: 0; }

anotherstyle { width: 0px; }

Le problème mineur avec « 0 » est que si vous modifiez à une valeur non nulle, vous pourriez oublier d'ajouter le « px ». Et en faisant une valeur « 0 », vous pouvez oublier de supprimer le « px ». Je veux la cohérence pure dans mon code et petites choses comme ce disque me noix.

Est-ce que cela vient vraiment à une préférence personnelle comme où tiret?

Ils ont tous deux semblent fonctionner, mais où est le mieux et pourquoi?

Merci-vous.


EDIT pour plus de clarté:

J'ai écrit "petites choses comme ce disque me nuts" .

Ne pas avoir un "px" pour 0 pas ce que je faisais référence.

Il me rend fou qu'il ya deux façons de faire la même chose simple.

Personnellement, malgré le problème mineur d'oublier d'ajouter / supprimer px 'le cas échéant, je vais continuer à utiliser « 0 » par lui-même en sachant qu'il est tout aussi acceptable que dans l'autre sens.

Était-ce utile?

La solution

They are identical.

Use width: 0, because it is shorter and more readable.

Especially when you have, for example:

padding: 0px 0px 9px 8px

vs

padding: 0 0 9px 8px

See the specs:

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.


The minor problem with '0' is that if you change it to some non-zero value, you might forget to add the 'px'. And when making a value '0', you may forget to remove the 'px'

This does not happen once you get into the habit of writing 0 without the unit identifier.

Autres conseils

I always use 0 (without units), unless I want to explicitly set the units, simply because it's shorter. The important thing is to be consistent.

I think 0 is 0 no matter what you have after it. 0% of something would be the same as 0 pixels of something. I think...at least that is how I think about things, and multiplication seems to agree with me.

If you are using several type of CSS units (%, em, inch…) in your stylesheet you'd better keep the intended one (in your case px).

On a side note, remember that the keyword none doesn't necessarily redirect to 0 (even if visually it has the same result).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top