Ilian
Ilian

Reputation: 297

CSS Default Units

Can I change the default unit from pixel to mm? For example I want when move an element left and top to be in mm not in pixel?

Upvotes: 14

Views: 15216

Answers (7)

Quentin
Quentin

Reputation: 943480

There is no 'default unit'. The CSS spec requires that a length (other than zero) that is missing a unit be treated as an error (and thus ignored).

In quirks mode (and you should almost never be using quirks mode, it makes life more difficult), most browsers will perform error recovery and assume you meant pixels (despite the spec forbidding this).

From the standard:

A <quirky-length> is syntactically identical to a <number-token>, and is interpreted as a 'px' length with the same value.

(In other words, Quirks Mode allows all 'px' lengths in the affected properties to be written without a unit, similar to unitless zero lengths.)

If you are working with screen media — avoid physical units. Most systems are not calibrated to calculate the DPI correctly (so when converting from physical units into something a monitor understands (pixels) they get it wrong).

Upvotes: 23

Abdullrahman Qutaiba
Abdullrahman Qutaiba

Reputation: 177

Yes, You can do this indirectly by assigning variable for unit:

--unit: 1mm;
--value: 10;
--variable: calc(var(--unit) * var(--value));

Upvotes: -1

Select0r
Select0r

Reputation: 12628

As there's no "default unit" and you need to provide the unit when positioning an element, you can just use "mm" instead of "px".

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038750

AFAIK if no explicit unit was specified (like 'em' or '%') then "px" is concatenated to the value and there is no way to override this default behavior.

Upvotes: -1

Tatu Ulmanen
Tatu Ulmanen

Reputation: 124768

No, you can not change the default unit, but it shouldn't be too much to just put the units there:

#foo {
    left: 22mm;
    top: 20mm;
}

You should always specify the unit nevertheless, because it is required and browsers might interpret unitless values differently.

Upvotes: 1

tDo
tDo

Reputation: 528

No, that's not possible since different screens might have different sizes while still having the same resolution. Ultimately leading to pixel or relative notations.

Upvotes: -2

dpq
dpq

Reputation: 9268

Specifying CSS units is a requirement for non-zero values. Browsers may try to guess what you meant, but it would still be a broken stylesheet according to the standard.

I.e. there is no "default unit" in CSS2, it's just that the browser may try to help you out, although it may as well just ignore your statement that doesn't specify units as an invalid one.

Upvotes: 18

Related Questions