开发者

CSS Default Units

Can I change the default unit fr开发者_如何学编程om pixel to mm? For example I want when move an element left and top to be in mm not in pixel?


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


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.


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.


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


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.


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

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜