Home  >  Article  >  Web Front-end  >  How Do Browsers Handle Missing Units in CSS Attributes?

How Do Browsers Handle Missing Units in CSS Attributes?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-24 14:00:02336browse

How Do Browsers Handle Missing Units in CSS Attributes?

Fallback for CSS Attributes Without Unit

In CSS, when a unit (e.g., px, em, pt, %) is missing from an attribute, browsers handle the situation differently.

Quirks Mode Behavior

In quirks mode, some browsers may fall back to a preferred unit, typically px. This behavior is based on the legacy of unitless HTML attributes like width and height.

Standards Mode Behavior

In standards mode, as per CSS2.1 specifications, all non-zero length values require units. UAs are expected to ignore length values without units.

Browser Inconsistencies

Despite the standardized behavior in standards mode, browsers exhibit inconsistencies in quirks mode:

  • Internet Explorer: Ignored unitless rules in IE7 and later
  • Firefox: Fallback to px for width but ignored border-width
  • Chrome, Opera, Safari: Fallback to px for both width and border-width

W3C Requirements

The W3C does not dictate the behavior for unitless CSS attributes in quirks mode. The specifications only mandate that UAs ignore unitless length values in standards mode.

Specific Questions:

  1. Why px fallback in quirks mode?
    It's a quirky behavior based on legacy HTML attributes.
  2. Mandatory fallback to preferred unit?
    No, it's not mandatory in quirks mode.
  3. Correct quirks mode behavior for given example:
    There is no defined correct behavior as it depends on browser quirks. Chrome, Opera, and Safari's behavior is more consistent with ignoring unitless rules in standards mode.

The above is the detailed content of How Do Browsers Handle Missing Units in CSS Attributes?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn