Home > Article > Web Front-end > Q: What Happens When CSS Attributes Lack Units in Quirks Mode Browsers?
CSS Attributes Without Unit: Fallback to PX
In CSS, it may seem convenient to omit units from attribute values. However, doing so can lead to unexpected fallback scenarios. In quirks mode, browsers may behave inconsistently when encountering unitless attributes. This article examines the fallback behavior, its implications, and inconsistencies across browsers.
Fallback to PX
When a CSS attribute lacks a unit, such as "px", "em", or "%", browsers may default to a fallback unit. In quirks mode, many browsers choose "px" as the fallback unit. This behavior aligns with legacy HTML attributes that accepted pixel lengths as unitless values.
However, this behavior is not consistent across browsers. Some browsers, such as Internet Explorer 7 and above in standards mode, ignore rule attributes that lack units, while other browsers, such as Chrome, Firefox, and Safari, continue to fallback to "px."
W3C Specification
The W3C specification for CSS states that all non-zero length values must have units. Therefore, unitless attributes are not valid. This specification applies to standards mode rendering. In quirks mode, browsers have more freedom to implement quirky behavior, which can explain the variations in fallback behavior.
Mandatory Fallback Rule
There is no specific rule that mandates browsers to fallback to a preferred unit. This decision is left to the discretion of the browser, and it can vary depending on the browser version, rendering mode, and specific properties involved.
Example Behavior
Internet Explorer behaves differently in quirks mode and standards mode. In quirks mode, it used to fallback width and border-width to "px." However, in standards mode, it ignores attribute rules that lack units.
Firefox differs from Internet Explorer in quirks mode. It falls back width to "px" but ignores the border-width attribute. Chrome, Opera, and Safari all fallback both width and border-width to "px" in quirks mode.
Conclusion
Using unitless attributes in CSS is discouraged. It can lead to unexpected results and browser inconsistencies, especially in quirks mode. Adhering to the W3C recommendation of specifying units for all length values will ensure consistent rendering across browsers.
The above is the detailed content of Q: What Happens When CSS Attributes Lack Units in Quirks Mode Browsers?. For more information, please follow other related articles on the PHP Chinese website!