Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the standard way to deal with unsupported CSS expressions?

Tags:

css

What does the CSS standard say about unsupported expressions? How should a browser deal with them? How do actual browser implementations deal with them?

I'm implementing a CSS property optimizer (for a minifier project), and we want to leave CSS fallbacks intact. Our goal is to optimize the CSS as much as possible but in such a way that it should render exactly the same as the original.
This is why it's essential for me to understand how these things work.

Simple properties

For simple properties, it's really easy.
Let's say we have this:

div {
    color: #f00;
    color: rgba(1,2,3,.4);
}

In this case, if the browser doesn't support rgba then the first declaration #f00 wins. There is no question here.

Shorthands

However, how does it work with shorthand properties?
Here's some code:

div {
    background: #f00;
    background: rgba(1,2,3,.4);
}

How does the browser render this if it doesn't understand rgba? As you know, the syntax of background is: background: <color> <image> <repeat> <attachment> <position>; and such a shorthand declaration overrides any of the 5 fine-grained declarations that came before it; so the difficulty lies in which one of the 5 fine-grained properties the browser tries to assign the unknown token to. I have several possibilities in mind:

  • the browser decides it doesn't understand the latter declaration at all and drops it entirely
  • the browser thinks that rgba(...) represents a background-image and even though it doesn't know what to do with it, clears out the previous background-color as well
  • the browser thinks that rgba(...) represents a background-color and since it doesn't understand it, falls back to using #f00 instead

Let's make it even more interesting, say we have this:

div {
    background: #fff url(...) no-repeat;
    background: rgba(1,2,3,.4) linear-gradient(...) repeat-y;
}

How does a browser interpret this CSS snippet, ...

  • if the browser doesn't understand rgba?
  • if the browser doesn't understand linear-gradient?
  • if the browser doesn't understand repeat-y?
  • if the browser doesn't understand any two of the three?
  • if the browser doesn't understand any of the three?
like image 297
Venemo Avatar asked Feb 12 '14 09:02

Venemo


Video Answer


1 Answers

The parsing rules in section 4.2 of the CSS2.1 spec speaks in terms of declarations, which refer to entire property-value pairs, regardless of whether the properties are shorthand or not:

  • Illegal values. User agents must ignore a declaration with an illegal value. For example:

    img { float: left }       /* correct CSS 2.1 */
    img { float: left here }  /* "here" is not a value of 'float' */
    img { background: "red" } /* keywords cannot be quoted */
    img { border-width: 3 }   /* a unit must be specified for length values */
    

    A CSS 2.1 parser would honor the first rule and ignore the rest, as if the style sheet had been:

    img { float: left }
    img { }
    img { }
    img { }
    

    A user agent conforming to a future CSS specification may accept one or more of the other rules as well.

Notice that the third example shows the use of an illegal value for the background shorthand property resulting in the entire declaration being ignored.

Although the spec speaks of illegal values, as far as an implementation is concerned an unrecognized value and an illegal value are the same thing, since either way the implementation doesn't know what to do with such a value.

So the answer to the first part of your question is

  • the browser decides it doesn't understand the latter declaration at all and drops it entirely

And the answers to the second part are all the same: the latter declaration is ignored entirely.

like image 57
BoltClock Avatar answered Sep 29 '22 10:09

BoltClock