没有单位的CSS属性的回退 [英] Fallback for CSS attributes without unit

查看:104
本文介绍了没有单位的CSS属性的回退的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设CSS属性缺少单位(px,em,pt,%)的情况:

Consider a scenario where CSS attribute is missing the unit (px, em, pt, %):

<body>
    <div 
        style=  "width:170;
                border:1 dotted PaleGreen;
                background-color:MistyRose">
        The quick brown
    </div>
</body>

问题:


  1. 为什么会回退到px?像素是否始终是首选单位? W3C工作草案或建议中是否有任何规则?

  2. 是否有规则规定UA必须落实到首选单位?

  3. 给定上面的示例,以下是正确的行为:

    • Internet Explorer:在Quirks模式(IE6,5,4 ..)width和border-width用于回退到px。由于IE7(直到现在,IE10RP),它忽略整个规则,如果单位缺失。因此,这两个规则都被忽略。

    • Firefox 13:在上面的示例中,宽度回退到px,但是border-width被忽略。

    • Opera 12,Safari 5.1.2:宽度和边框宽度均回退到px。

  1. Why would it fallback to px? Is pixel always a preferred unit? Is there any rule defined in W3C working draft or recommendation?
  2. Is there a rule which makes it mandatory for a UA to fallback to a preferred unit?
  3. Given the above example, which of the following is a correct behavior:
    • Internet Explorer: In Quirks mode (IE6,5,4..) width and border-width used to fallback to px. Since IE7 (till present, IE10RP) it ignores the whole rule if unit is missing. Therefore both rules were ignored.
    • Firefox 13: In the above example width fallback to px, but border-width was ignored.
    • Chrome 19, Opera 12, Safari 5.1.2: Both width and border-width fallback to px.

注意:在Microsoft-Connect上,他们说:

Note: On Microsoft-Connect, they said:


您设计的问题是。 IE10在标准模式下忽略宽度或高度,没有符合CSS标准的单位。


推荐答案

在HTML中看到一个doctype声明,所以我只能假设你的测试页面是以怪异模式呈现的。

I don't see a doctype declaration in your HTML, so I can only assume your test page is being rendered in quirks mode.



为什么会回退到px?像素是否始终是首选单位? W3C工作草案或建议中是否有任何规则?

Why would it fallback to px? Is pixel always a preferred unit? Is there any rule defined in W3C working draft or recommendation?

它只会回到 px 在quirks模式(我相信只有某些属性)。是的, px 是首选的备用单元。这回到传统的HTML width height 属性,它们接受像素长度作为无单位数字。

It will only fall back to px in quirks mode (and I believe only for certain properties). And yes, px is the preferred fallback unit. This harks back to the legacy HTML width and height attributes which accepted pixel lengths as unitless numbers.


是否有一条规则强制要求UA退回到首选单位?

Is there a rule which makes it mandatory for a UA to fallback to a preferred unit?

否,因此您观察到的行为不一致。在标准模式下,UA需要忽略没有单位的长度值;该单位不是可选的,如Microsoft Connect中所述。

No, hence the inconsistent behavior you observe. In standards mode, though, a UA needs to ignore length values without units; the unit is not optional, as mentioned in Microsoft Connect which you quote.

在CSS2.1 中,所有非零长度值都必须包含单位。

In CSS2.1, all non-zero length values must have units.


给定上面的示例,以下是正确的行为:

Given the above example, which of the following is a correct behavior:


  • Internet Explorer:在Quirks模式..)width和border-width用于回退到px。由于IE7(直到现在,IE10RP),它忽略整个规则,如果单位缺失。因此,这两个规则都被忽略。

  • Firefox 13:在上面的示例中,宽度回退到px,但是border-width被忽略。

  • Opera 12,Safari 5.1.2:宽度和边框宽度都会覆盖到px。

,基于您的页面处于怪异模式的假设,我只能说,虽然规范提到古怪行为,这种古怪行为的具体细节没有在规范中定义(对于明显和不是 - 显然的原因)。

Again, based on the assumption that your page is in quirks mode, I can only say that while the specifications make a mention of quirky behavior, the specific details of such quirky behavior aren't defined in the specifications (for both obvious and not-so-obvious reasons).

我猜微软改变了IE7 +中的怪异模式行为,以反映无单位值的标准行为,因为怪异模式存在于 all浏览器(IE <6)除外,并且使用相同的不正确的doctype或缺少doctype触发。

I'm guessing Microsoft changed the quirks-mode behavior in IE7+ to reflect standards behavior for unitless values, as quirks mode exists in all browsers (except IE < 6) and is triggered with the same improper doctype or lack of a doctype. The behavior in standards mode has not changed, though, as far as I'm aware.

这篇关于没有单位的CSS属性的回退的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆