Skip to content

CSS image() function #153

@jsnkuhn

Description

@jsnkuhn

Description

The image() function allows an author to:

use [media fragments](https://www.w3.org/TR/media-frags/) to clip out a portion of an image

use a solid color as an image

fallback to a solid-color image, when the image at the specified url can’t be downloaded or decoded

automatically respect the image orientation specified in the image’s metadata"

Rationale

spec fixes many long standing issues with CSS images:

  1. a simplified syntax for image spriting that doesn't require 3 different CSS properties: background-image: image('sprites.svg#xywh=40,0,20,20');. This also opens up the use of background-size and background-position for actual sizing and positioning of these images instead of dealing with the spriting.

  2. ability to create a solid color image like this: background-image: image(red); instead of hacking a gradient like this: linear-gradient(red, red). Is good for devs because it's less repetition in the code and for browser vendors i'm guessing this would allow a path to a separate algorithm for just rendering a solid colored image.

  3. Proper image fall-backs which url() can't do. When the image at the specified url can’t be downloaded or decoded, fallback to another image or color. This could lead to less background colors that are never seen being painted behind images.

  4. automatically respect the image orientation specified in the image’s metadata.

FYI: the above are all from images-4 but the CSSWG is already looking at expanding image() in images-5 for things like background video and @image-manipulation

Firefox bug: https://bugzilla.mozilla.org/show_bug.cgi?id=703217
chrome bug: https://bugs.chromium.org/p/chromium/issues/detail?id=254753#c_ts1639769079
webkit bug: https://bugs.webkit.org/show_bug.cgi?id=72584

(looks like in webkit and firefox a considerable amount of work was done but then was just abandoned?)

Specification

https://drafts.csswg.org/css-images-4/#image-notation

Tests

https://wpt.fyi/results/css/css-images?label=experimental&label=master&aligned&view=subtest

no tests yet for "Image Fragments" or "Bidi-sensitive Images"

only 5 tests right now all seems to be about "Solid-color Images" and "Image Fallbacks"

css-image-fallbacks-and-annotations.html
background: image("green.png", green);

css-image-fallbacks-and-annotations002.html
background-color: red;
background-image: image("support/1x1-green.png");

css-image-fallbacks-and-annotations003.html
background-image: image("1x1-green.svg", "support/1x1-green.png","support/1x1-green.gif");

css-image-fallbacks-and-annotations004.html
background-image: image("1x1-green.svg", "1x1-green.png", "support/1x1-green.gif");

css-image-fallbacks-and-annotations005.html
background-image: image(rgba(0,0,255,0.5)), url("support/1x1-green.png");

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions