8000 csswg-drafts/css-images/Overview.bs at 425954e5a71e19ef8de9504c869c0df71e54589e · w3c/csswg-drafts · GitHub
Skip to content

Latest commit

 

History

History
2939 lines (2361 loc) · 133 KB

File metadata and controls

2939 lines (2361 loc) · 133 KB
<h1>CSS Image Values and Replaced Content Module Level 4</h1>
<pre class='metadata'>
Status: ED
Shortname: css-images
Level: 4
Group: csswg
ED: http://dev.w3.org/csswg/css-images-4/
TR: http://www.w3.org/TR/css4-images/
Editor: Tab Atkins Jr., Google, http://xanthir.com/contact/
Editor: fantasai, Mozilla, http://fantasai.inkedblade.net/contact
Abstract: This module contains the features of CSS level 4 relating to the <<image>> type and replaced elements. It includes and extends the functionality of CSS Image Values and Replaced Content Module Level 3. The main extensions compared to level&nbsp;3 are several additions to the <<image>> type, additions to the <<gradient>> type, extentions to the ''image()'' function, definitions for interpolating several <<image>> types, and several properties controlling the interaction of replaced elements and CSS's layout models.
!Issue Tracking: <a href="http://www.w3.org/Style/CSS/Tracker/products/27">http://www.w3.org/Style/CSS/Tracker/products/27</a>
Previous Version: http://www.w3.org/TR/2012/WD-css4-images-20120911/
Ignored Terms: <offset>, background positioning area, border image area, <meetorslice>, <ending-shape>, Map
Link Defaults: css21 (property) display
</pre>
<h2 id="intro">
Introduction</h2>
<em>This section is not normative.</em>
In CSS Levels 1 and 2,
image values, such as those used in the 'background-image' property,
could only be given by a single URL value.
This module introduces additional ways of representing 2D images,
for example as <a href="#image-notation">a list of URIs denoting fallbacks</a>,
as <a href="#element-image-type">a reference to an element</a>,
or as <a href="#gradients">a gradient</a>.
This module also defines several properties for <a href="#image-processing">manipulating raster images</a>
and for <a href="#object-fit">sizing</a>
or <a href="#object-position">positioning</a>
replaced elements such as images within the box determined by the CSS layout algorithms.
It also defines in a generic way CSS's <a href="#sizing">sizing algorithm</a> for images and other replaced elements.
<h3 id="placement">
Module Interactions</h3>
This module defines and extends the <<image>> value type defined in [[!CSS3VAL]].
Furthermore it replaces the <<url>> type
in the 'background-image' and 'list-style-image' definitions in CSS1 and CSS2
and adds <<image>> as an alternative to <<url>> in the 'content' property's value.
It is presumed that CSS specifications beyond CSS2.1 will use the <<image>> notation
in place of <<url>> where 2D images are expected. (See e.g. [[CSS3BG]].)
Of the properties defined in this module,
only 'image-resolution'
applies to <code>::first-line</code> and <code>::first-letter</code>.
<h3 id="values">
Values</h3>
This specification follows the <a href="http://www.w3.org/TR/CSS21/about.html#property-defs">CSS property definition conventions</a> from [[!CSS21]].
Value types not defined in this specification
are defined in CSS Level 2 Revision 1 [[!CSS21]].
Other CSS modules may expand the definitions of these value types:
for example [[CSS3COLOR]],
when combined with this module,
expands the definition of the <<color>> value type as used in this specification.
In addition to the property-specific values listed in their definitions,
all properties defined in this specification also accept the <a href="http://www.w3.org/TR/css3-values/">global keywords</a> as their property value.
For readability they has not been repeated explicitly.
<h2 id='planned-features'>
Planned Features</h2>
<ol>
<li>Resolution tagging in image()
<li>Logical keywords for specifying linear gradient direction.
<li>More interpolation modes for gradients: <a href="http://lists.w3.org/Archives/Public/www-style/2012Jan/0635.html">http://lists.w3.org/Archives/Public/www-style/2012Jan/0635.html</a>
<li>Ability to reference an SVG paint server via url (maybe as element(<<string>>)?)
</ol>
<!--
██ ████ ██ ██ ███ ██████ ████████ ██
██ ██ ███ ███ ██ ██ ██ ██ ██ ██
██ ██ ████ ████ ██ ██ ██ ██ ██
██ ██ ██ ███ ██ ██ ██ ██ ████ ██████ ██
██ ██ ██ ██ █████████ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ████ ██ ██ ██ ██ ██████ ████████ ██
-->
<h2 id="image-values">
Image Values: the <<image>> type</h2>
The <<image>> value type denotes a 2D image. It can be a
<a href="#url-notation">url reference</a>,
<a href="#image-notation">image notation</a>,
<!-- <a href="#element-image">element reference</a>, -->
or <a href="#gradients">gradient notation</a>.
Its syntax is:
<pre class="prod"><dfn>&lt;image></dfn> = <<url>> | <<image()>> | <<image-set()>> | <<element()>> | <<cross-fade()>> | <<gradient>></pre>
An <<image>> can be used in many CSS properties,
including the 'background-image', 'list-style-image', 'cursor' properties [[!CSS21]]
(where it replaces the <<url>> component in the property's value).
In some cases, an image is invalid,
such as a <<url>> pointing to a resource that is not a valid image format.
An <dfn export>invalid image</dfn> is rendered as a solid-color ''transparent'' image with no intrinsic dimensions.
However, <a>invalid images</a> have special behavior in some contexts,
such as the ''image()'' notation.
<!--
< 7392 div class="react-code-text react-code-line-contents" style="min-height:auto">
██ ██ ██ ████████ ██ ██
██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██
██ ██ ██ ████████ ██ ██
██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██
██ ███████ ██ ██ ████████ ██
-->
<h3 id="url-notation">
Image References and Image Slices: the <<url>> type and ''url()'' notation</h3>
The simplest way to indicate an image is to reference an image file by URL.
This can be done with the <a href="http://www.w3.org/TR/CSS21/syndata.html#uri">>url()'' notation</a>, defined in [[!CSS21]].
<div class="example">
In the example below, a background image is specified with ''url()''syntax:
<pre>background-image: url(wavy.png);</pre>
</div>
If the UA cannot download, parse, or otherwise successfully display the contents at the URL as an image,
it must be treated as an <a>invalid image</a>.
<!--
████ ██ ██ ███ ██████ ████████ ███ ███
██ ███ ███ ██ ██ ██ ██ ██ ██ ██
██ ████ ████ ██ ██ ██ ██ ██ ██
██ ██ ███ ██ ██ ██ ██ ████ ██████ ██ ██
██ ██ ██ █████████ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
████ ██ ██ ██ ██ ██████ ████████ ███ ███
-->
<h3 id="image-notation">
Image Fallbacks and Annotations: the ''image()'' notation</h3>
The ''image()'' function allows an author to:
<ul>
<li>
use <a href="http://www.w3.org/TR/media-frags/">media fragments</a> to clip out a portion of an image
<li>
specify fallback images in case the preferred image can't be decoded or is a type that the browser doesn't recognize
<li>
use a solid color as an image
<li>
automatically respect the image orientation specified in the image's metadata
</ul>
The ''image()'' notation is defined as:
<pre class='prod'>
<dfn>image()</dfn> = image( <<image-tags>>? [ <<image-src>> , ]* [ <<image-src>> | <<color>> ] )
<dfn>&lt;image-tags></dfn> = [ ltr | rtl ]
<dfn>&lt;image-src></dfn> = [ <<url>> | <<string>> ]
</pre>
Each <<string>> or <<url>> inside ''image()'' represents an image,
just as if the <a href="#url-notation">>url()'' notation</a> had been used.
As usual for URLs in CSS,
relative URLs are resolved to an absolute URL
(as described in Values &amp; Units [[!CSS3VAL]])
when a specified ''image()'' value is computed.
If the image has an orientation specified in its metadata,
such as EXIF,
the UA must rotate or flip the image to correctly orient it
as the metadata specifies.
<h4 id="image-fragments">
Image Fragments</h4>
When a URL specified in ''image()'' represents a portion of a resource
(e.g. by the use of <a href="http://www.w3.org/TR/media-frags/#naming-space">media fragment identifiers</a>)
that portion is clipped out of its context and used as a standalone image.
<div class="example">
For example, given the following image and CSS:
<a href="images/sprites.svg">
<img src="images/sprites.svg" height="20" width="180" alt="[9 circles, with 0 to 8 eighths filled in]">
</a>
<pre>background-image: image('sprites.svg#xywh=40,0,20,20')</pre>
...the background of the element will be the portion of the image that starts at (40px,0px) and is 20px wide and tall,
which is just the circle with a quarter filled in.
</div>
So that authors can take advantage of CSS's forwards-compatible parsing rules to provide a fallback for image slices,
implementations that support the ''image()'' notation
<em>must</em> support the <code>xywh=#,#,#,#</code> form of media fragment identifiers
for images specified via ''image()''. [[!MEDIA-FRAGS]]
<div class='example'>
Note that image fragments can also be used with the ''url()'' notation.
However, a legacy UA that doesn't understand the media fragments notation
will ignore the fragment and simply display the entirety of the image.
Since 982E the ''image()'' notation requires UAs to support media fragments,
authors can take advantage of CSS's forward-compatible parsing rules
to provide a fallback when using an image fragment URL:
<pre>
background-image: url('swirl.png'); /* old UAs */
background-image: image('sprites.png#xywh=10,30,60,20'); /* new UAs */
</pre>
</div>
<h4 id="image-fallbacks">
Image Fallbacks</h4>
Multiple <<image-src>>s can be given separated by commas,
in which case the function represents the first image that's not an <a>invalid image</a>.
The final argument can specify a <<color>> to serve as an ultimate fallback;
this can be used, e.g. for 'background-image',
to ensure adequate contrast if none of the preceding <<image-src>>s can be used.
If the final argument is a <<color>>,
it represents a solid-color image of the given color with no <a>intrinsic dimensions</a>.
If all of the provided <<image-src>>s are <a>invalid images</a>
and a fallback color was not provided as the last argument,
the entire ''image()'' function must be treated as an <a>invalid image</a>.
<div class="example">
The rule below would tell the UA to load "wavy.svg" if it can;
failing that to load "wavy.png";
and failing that to display "wavy.gif".
For example, the browser might not understand how to render SVG images,
and the PNG may be temporarily 404
(returning an HTML 404 page, which the browser can't decode as an image)
due to a server move,
so the GIF is used until one of the previous problems corrects itself.
<pre>background-image: image("wavy.svg", "wavy.png" , "wavy.gif");</pre>
</div>
<div class='example'>
The fallback color can be used to ensure that text is still readable
even when the image fails to load.
For example, the following code works fine if the image is rectangular and has no transparency:
<pre>
body { color: black; background: white; }
p.special { color: white; background: url("dark.png") black; }
</pre>
When the image doesn't load,
the background color is still there to ensure that the white text is readable.
However, if the image has some transparency,
the black will be visible behind it,
which is probably not desired.
The ''image()'' function addresses this:
<pre>
body { color: black; background: white; }
p.special { color: white; background: image("dark.png", black); }
</pre>
Now, the black won't show at all if the image loads,
but if for whatever reason the image fails,
it'll pop in and prevent the white text from being set against a white background.
</div>
If a URL uses a fragment identifier syntax that the implementation does not understand,
or does not consider valid for that type of image,
the URL must be treated as representing an <a>invalid image</a>.
<span class='note'>This error-handling is limited to image(),
and not in the definition of URL,
for legacy compat reasons.</span>
<div class='example'>
For example, if a future specification defined a way to refer to a specific frame of an animated GIF with a fragment identifier,
an author could write the following to get newer browsers to use the GIF's frame,
and older browsers to instead download the fallback image:
<pre>background-image: image("cat_meme.gif#frame=5", "lolcat.png");</pre>
</div>
<!-- XXX SVG fragment example with spriting / fallbacks (once SVG can provide this) -->
<h4 id="color-images">
Solid-color Images</h4>
<div class='example'>
At times, one may need a solid-color image for a property or function that does not accept the <<color>> type directly.
The ''image()'' function can be used for this:
by specifying <em>only</em> a color without any URLs,
the function immediately falls back to representing a solid-color image of the chosen color.
<pre>background-image: image(rgba(0,0,255,.5)), url("bg-image.png");</pre>
In the above, the background is the image "bg-image.png",
overlaid with partially-transparent blue.
</div>
<h4 id='bidi-images'>
Bidi-sensitive Images</h4>
Before listing any <code>&lt;image-src>s</code>,
the author may specify a directionality for the image,
similar to adding a <code>dir</code> attribute to an element in HTML.
If a directional image is used on or in an element with opposite <a href="http://www.w3.org/TR/CSS21/visuren.html#propdef-direction">direction</a>,
the image must be flipped in the inline direction
(as if it was transformed by, e.g., <code>scaleX(-1)</code>, if the inline direction is the X axis).
Note: Absent this declaration,
images default to no directionality at all,
and thus don't care about the directionality of the surrounding element.
<div class='example'>
A list may use an arrow for a bullet that points into the content.
If the list can contain both LTR and RTL text,
though, the bullet may be on the left or the right,
and an image designed to point into the text on one side will point out of the text on the other side.
This can be fixed with code like:
<pre>
&lt;ul style="list-style-image: image(ltr 'arrow.png');">
&lt;li dir='ltr'>My bullet is on the left!&lt;/li>
&lt;li dir='rtl'>MY BULLET IS ON THE RIGHT!&lt;/li>
&lt;/ul>
</pre>
This should render something like:
<pre>
&#8658; My bullet is on the left!
!THGIR EHT NO SI TELLUB YM &#8656;
</pre>
In LTR list items, the image will be used as-is.
In the RTL list items, however,
it will be flipped in the inline direction,
so it still points into the content.
</div>
<!--
████ ██ ██ ███ ██████ ████████ ██████ ████████ ████████ ███ ███
██ ███ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ████ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ███ ██ ██ ██ ██ ████ ██████ ███████ ██████ ██████ ██ ██ ██
██ ██ ██ █████████ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
████ ██ ██ ██ ██ ██████ ████████ ██████ ████████ ██ ███ ███
-->
<h3 id='image-set-notation'>
Resolution Negotiation: the ''image-set()'' notation</h3>
Delivering the most appropriate image resolution for a user's device can be a difficult task.
Ideally, images should be in the same resolution as the device they're being viewed in,
which can vary between users.
However, other factors can factor into the decision of which image to send;
for example, if the user is on a slow mobile connection,
they may prefer to receive lower-res images
rather than waiting for a large proper-res image to load.
The ''image-set()'' function allows an author to ignore most of these issues,
simply providing multiple resolutions of an image
and letting the UA decide which is most appropriate in a given situation.
<p class='issue'>
This solution assumes that resolution is a proxy for filesize,
and therefore doesn't appropriately handle multi-resolution sets of vector images,
or mixing vector images with raster ones (e.g. for icons).
For example, use a vector for high-res,
pixel-optimized bitmap for low-res,
and same vector again for low-bandwidth (because it's much smaller, even though it's higher resolution).
The syntax for ''image-set()'' is:
<pre class='prod'>
<dfn>image-set()</dfn> = image-set( [ <<image-set-decl>>, ]* [ <<image-set-decl>> | <<color>>] )
<dfn>&lt;image-set-decl></dfn> = [ <<image>> | <<string>> ] <<resolution>></pre>
The ''image-set()'' function can not be nested inside of itself,
either directly
or indirectly
(as an argument to another <<image>> type).
<p class='issue'>
WebKit's experimental implementation has an ''x'' unit
which is identical to ''dppx''.
Should we add it?
Each <<string>> or <<url>> inside ''image-set()'' represents an image,
just like in ''image()''.
Every <<image-set-decl>> in a given ''image-set()'' must have a different <<resolution>>,
or else the function is invalid.
The value that a given ''image-set()'' represents is determined by
the UA first ordering the set of <<image-set-decl>>s
in a UA-determined order
(which should be based on the <<resolution>> of each <<image-set-decl>>,
but may include other factors that the UA deems relevant,
such as connection speed).
The function then represents the first such image that is not an <a>invalid image</a>.
If a fallback color was provided,
the UA may decide at any point that the remaining <<image-set-decl>>s are unsuitable,
and choose to use the fallback color instead,
in which case the function represents a solid-color image of the given color with no <a>intrinsic dimensions</a>.
If <em>all</em> of the <<image-set-decl>>s represent <a>invalid images</a>,
then if a fallback color was provided,
the function represents a solid-color image of the given color with no <a>intrinsic dimensions</a>;
otherwise, the function represents an <a>invalid image</a>.
<p class='issue'>
HTML currently doesnt' do fallback if the first-chosen image doesn't load.
We should harmonize with HTML,
either by convincing them to do fallback,
or by removing our own fallback.
If an <<image-set-decl>> is chosen and successfully loaded,
the associated <<resolution>> is the image's <a>intrinsic resolution</a>.
<p class='issue'>
It's been suggested that we don't need a full <<resolution>> here,
and should instead just use the ''x'' unit by itself.
This seems silly to me,
since the ''x'' unit is just an easier synonym for ''dppx'',
but thoughts?
Beyond theoretical purity considerations,
it seems that ''dpi'' can be useful for use-cases involving printing,
where dpi is actually used to talk about images
and having an integer correlation between dots and pixels
is either unnecessary or strictly non-sensical
(as the screening resolution can depend on the colors being used).
<div class='example'>
This example shows how to use ''image-set()'' to provide an image in three versions:
a "normal" version,
a "high-res" version,
and an extra-high resolution version for use in high-quality printing
(as printers can have <em>extremely</em> high resolution):
<pre>
background-image: image-set( "foo.png" 1x,
"foo-2x.png" 2x,
"foo-print.png" 600dpi );
</pre>
</ CFC div>
<!--
████████ ██ ████████ ██ ██ ████████ ██ ██ ████████ ███ ███
██ ██ ██ ███ ███ ██ ███ ██ ██ ██ ██
██ ██ ██ ████ ████ ██ ████ ██ ██ ██ ██
██████ ██ ██████ ██ ███ ██ ██████ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██
████████ ████████ ████████ ██ ██ ████████ ██ ██ ██ ███ ███
-->
<h3 id='element-notation'>
Using Elements as Images: the ''element()'' notation</h3>
The ''element()'' function allows an author to use an element in the document as an image.
As the referenced element changes appearance,
the image changes as well.
This can be used, for example,
to create live previews of the next/previous slide in a slideshow,
or to reference a canvas element for a fancy generated gradient or even an animated background.
Note: The ''element()'' function only reproduces the <em>appearance</em> of the referenced element,
not the actual content and its structure.
Authors should only use this for decorative purposes,
and must not use ''element()'' to reproduce an element with significant content across the page.
Instead, just insert multiple copies of the element into the document.
The syntax for ''element()'' is:
<pre class=prod><dfn>element()</dfn> = element( <<id-selector>> )</pre>
where <<id-selector>> is an ID selector [[!SELECT]].
<p class='issue'>
Do we need to be able to refer to elements in external documents
(such as SVG paint servers)?
Or is it enough to just use url() for this?
<p class='issue'>
This name conflicts with a somewhat similar function in GCPM.
This needs to be resolved somehow.
<p class='issue'>
Want the ability to do "reflections" of an element,
either as a background-image on the element or in a pseudo-element.
This needs to be specially-handled to avoid triggering the cycle-detection.
<p class='issue'>
When we have overflow:paged,
how can we address a single page in the view?
The ''element()'' function references the element matched by its argument.
The ID is first looked up in the <a idl>elementSources</a> map,
as described in that section.
If it's not found,
it's then matched against the document.
If multiple elements are matched,
the function references the first such element.
The image represented by the ''element()'' function can vary based on whether the element is visible in the document:
<dl>
<dt>
an <a title="element-not-rendered">element that is rendered</a>,
is not a descendant of a replaced element,
and generates a <a spec=css21>stacking context</a>
<dd>
The function represents an image with its intrinsic size equal to the <dfn export>decorated bounding box</dfn> of the referenced element:
<ul>
<li>
for an element rendered using a CSS rendering model,
the <a>decorated bounding box</a> is the smallest axis-aligned rectangle
that contains the <a href="http://www.w3.org/TR/2011/CR-css3-background-20110215/#border-image-area">border image areas</a> of all the fragments of the principal box
<li>
for an element rendered using the SVG rendering model,
<a href="http://www.w3.org/TR/SVGTiny12/intro.html#TermDecoratedBoundingBox">the decorated bounding box is defined by SVG</a>
</ul>
Note: Because images clip anything outside their bounds by default,
this means that decorations that extend outside the <a>decorated bounding box</a>,
like box shadows,
may be clipped.
The image is constructed by rendering the referenced element and its descendants
(at the same size that they would be in the document)
over an infinite ''transparent'' canvas,
positioned so that the edges of the <a>decorated bounding box</a> are flush with the edges of the image.
<p class='issue'>
Requiring some degree of stacking context on the element appears to be required for an efficient implementation.
Do we need a full stacking context, or just a pseudo-stacking context?
Should it need to be a stacking context normally,
or can we just render it as a stacking context when rendering it to element()?
If the referenced element has a transform applied to it or an ancestor,
the transform must be ignored when rendering the element as an image. [[!CSS3-TRANSFORMS]]
If the referenced element is broken across pages,
the element is displayed as if the page content areas were joined flush in the pagination direction,
with pages' edges corresponding to the initial containing block's start edge aligned.
<span class='note'>Elements broken across lines or columns are just rendered with their <a>decorated bounding box</a>.</span>
Implementations may either re-use existing bitmap data generated for the referenced element
or regenerate the display of the element to maximize quality at the image's size
(for example, if the implementation detects that the referenced element is an SVG fragment);
in the latter case, the layout of the referenced element in the image must not be changed by the regeneration process.
That is, the image must look identical to the referenced element,
modulo rasterization quality.
<div class='example'>
As a somewhat silly example, a <code>&lt;p></code> element can be reused as a background elsewhere in the document:
<pre>
&lt;style>
#src { color: white; background: lime; width: 300px; height: 40px; }
#dst { color: black; background: element(#src); padding: 20px; margin: 20px 0; }
&lt;/style>
&lt;p id='src'>I'm an ordinary element!&lt;/p>
&lt;p id='dst'>I'm using the previous element as my background!&lt;/p>
</pre>
<img src="images/element-function.png" alt="">
</div>
<dt>an <a title='element-not-rendered'>element that is not rendered</a>, but which provides a <a>paint source</a>
<dd>
The function represents an image with the intrinsic size and appearance of the <a>paint source</a>.
The host language defines the size and appearance of paint sources.
<div class='example'>
For example, the ''element()'' function can reference an SVG <code>&lt;pattern></code> element in an HTML document:
<pre>
&lt;!DOCTYPE html>
&lt;svg>
&lt;defs>
&lt;pattern id='pattern1'>
&lt;path d='...'>
&lt;/pattern>
&lt;/defs>
&lt;/svg>
&lt;p style="background: element(#pattern1)">
I'm using the pattern as a background!
If the pattern is changed or animated,
my background will be updated too!
&lt;/p>
</pre>
HTML also defines that a handful of elements,
such as <code>&lt;canvas></code>, <code>&lt;img></code>, and <code>&lt;video></code>,
provide a paint source.
This means that CSS can, for example,
reference a canvas that's being drawn into,
but not displayed in the page:
<pre>
&lt;!DOCTYPE html>
&lt;script>
var canvas = document.querySelector('#animated-bullet');
canvas.width = 20; canvas.height = 20;
drawAnimation(canvas);
&lt;/script>
&lt;canvas id='animated-bullet' style='display:none'>&lt;/canvas>
&lt;ul style="list-style-image: element(#animated-bullet);">
&lt;li>I'm using the canvas as a bullet!&lt;/li>
&lt;li>So am I!&lt;/li>
&lt;li>As the canvas is changed over time with Javascript,
we'll all update our bullet image with it!&lt;/li>
&lt;/ul>
</pre>
</div>
<dt>anything else
<dd>
The function represents an <a>invalid image</a>.
<div class='example'>
For example, all of the following ''element()'' uses will result in a transparent background:
<pre>
&lt;!DOCTYPE html>
&lt;p id='one' style="display:none;">one&lt;/p>
&lt;iframe src="http://example.com">
&lt;p id='two'>I'm fallback content!&lt;/p>
&lt;/iframe>
&lt;ul>
&lt;li style="background: element(#one);">
A display:none element isn't rendered, and a P element
doesn't provide a paint source.
&lt;/li>
&lt;li style="background: element(#two);">
The descendants of a replaced element like an IFRAME
can't be used in element() either.
&lt;/li>
&lt;li style="background: element(#three);">
There's no element with an id of "three", so this also
gets rendered as a transparent image.
&lt;/li>
&lt;/ul>
</pre>
</div>
</dl>
An element is <dfn export id='element-not-rendered' title='element-not-rendered'>not rendered</dfn> if it does not have an associated box.
This can happen, for example,
if the element or an ancestor is ''display:none''.
Host languages may define additional ways in which an element can be considered not rendered;
for example, in SVG,
any descendant of a <code>&lt;defs></code> element is considered to be not rendered.
<div class='example'>
The ''element()'' function can be put to many uses.
For example, it can be used to show a preview of the previous or next slide in a slideshow:
<pre>
&lt;!DOCTYPE html>
&lt;script>
function navigateSlides() {
var currentSlide = ...;
document.querySelector('#prev-slide').id = '';
document.querySelector('#next-slide').id = '';
currentSlide.previousElementSibling.id = 'prev-slide';
currentSlide.nextElementSibling.id = 'next-slide';
}
&lt;/script>
&lt;style>
#prev-preview, #next-preview {
position: fixed;
...
}
#prev-preview { background: element(#prev-slide); }
#next-preview { background: element(#next-slide); }
&lt;/style>
&lt;a id='prev-preview'>Previous Slide&lt;/a>
&lt;a id='next-preview'>Next Slide&lt;/a>
&lt;section class='slide'>...&lt;/section>
&lt;section class='slide current-slide'>...&lt;/section>
...
</pre>
In this example, the <code>navigateSlides</code> function updates the ids of the next and previous slides,
which are then displayed CFC in small floating boxes alongside the slides.
Since you can't interact with the slides through the ''element()'' function (it's just an image),
you could even use <code>click</code> handlers on the preview boxes to help navigate through the page.
</div>
<h4 id='paint-sources'>
Paint Sources</h4>
Host languages may define that some elements provide a <dfn export>paint source</dfn>.
Paint sources have an intrinsic width, height, and appearance,
separate from the process of rendering,
and so may be used as images even when they're <a title='element-not-rendered'>not rendered</a>.
In HTML, the <code>&lt;img></code>, <code>&lt;video></code>, and <code>&lt;canvas></code> elements provide paint sources
(defined in each element's section in <a href='http://www.whatwg.org/specs/web-apps/current-work/multipage/'>HTML5</a>).
In SVG, any element that provides a <a href='http://www.w3.org/TR/SVG/pservers.html'>paint server</a> provides a paint source.
<span class='note'>Note: In SVG1.1,
the <code>&lt;linearGradient></code>,
<code>&lt;radialGradient></code>,
and <code>&lt;pattern></code> elements
provide paint sources.</span>
They are drawn as described in the spec,
with the coordinate systems defined as follows:
<dl>
<dt>objectBoundingBox
<dd>
The coordinate system has its origin at the top left corner of the rectangle defined by the <a>concrete object size</a> that it's being drawn into,
and the same width and height as the <a>concrete object size</a>.
A single <a href="http://www.w3.org/TR/SVG/coords.html#Units">user coordinate</a> is the width and height of the <a>concrete object size</a>.
<dt>userSpaceOnUse
<dd>
The coordinate system has its origin at the top left corner of the rectangle defined by the <a>concrete object size</a> that it's being drawn into,
and the same width and height as the <a>concrete object size</a>.
<a href="http://www.w3.org/TR/SVG/coords.html#Units">User coordinates</a> are sized equivalently to the CSS ''px'' unit.
</dl>
Note: It is expected that a future version of this module will define ways to refer to paint sources in external documents,
or ones that are created solely by script and never inserted into a document at all.
<h4 id='elementsources'>
Using Out-Of-Document Sources: the <code>ElementSources</code> interface</h4>
The ''element()'' function normally selects elements within a document,
but elements that provide a <a>paint source</a> don't necessarily need to be in-document.
For example, an HTML <code>&lt;canvas></code> element can be created, maintained, and drawn into entirely in script,
with no need for it to be inserted into the document directly.
All that's needed is a way to refer to the element,
as an ID selector cannot select elements outside of the document.
The <a idl>elementSources</a> Map object provides this.
<pre class='idl'>
partial interface CSS {
[SameObject] readonly attribute Map elementSources;
};
</pre>
Any entries in the <a idl>elementSources</a> map with a string key
and a value that is an object providing a <a>paint source</a>
are made available to the ''element()'' function.
Whenever ''element()'' uses an <<id-selector>>,
the ID's value (without the leading <code>#</code> character)
is first looked up in the <a idl>elementSources</a> map:
<ul>
<li>
If it's found,
and the object associated with it provides a <a>paint source</a>,
the ''element()'' function represents that paint source.
<li>
If it's found,
but the object associated with it <em>doesn't</em> provide a <a>paint source</a>,
the ''element()'' function represent an <a>invalid image</a>.
<li>
If the ID isn't found in the map at all,
it's then looked for in the document as normal.
</ul>
<p class='issue'>
This reuse of the ID selector matches Moz behavior.
I'm trying to avoid slapping a <<custom-ident>> right in the beginning of the grammar,
as that eats too much syntax-space.
Another possibility, though, is to start the value with a language-defined keyword
<em>followed by</em> a <<custom-ident>>,
like ''element(external fancy)'' or something.
Naming suggestions welcome.
<div class='example'>
For example, fancy animating backgrounds can be done with an external canvas:
<pre>
&lt;script>
var bg = document.createElement('canvas');
bg.height = 200;
bg.width = 1000;
drawFancyBackground(bg);
CSS.elementSources.set('fancy', bg);
&lt;/script>
&lt;style>
h1 {
background-image: element(#fancy);
}
&lt;/style>
</pre>
As the "fancy" canvas is drawn into and animated,
the backgrounds of all the H1 elements will automatically update in tandem.
Note that the <a idl>elementSources</a> map is consulted <em>before</em> the document
to match the ID selector,
so even if there's an element in the document that would match ''#fancy'',
the backgrounds will still predictably come from the <a idl>elementSources</a> value instead.
</div>
<h4 id='element-cycles'>
Cycle Detection</h4>
The ''element()'' function can produce nonsensical circular relationships,
such as an element using itself as its own background.
These relationships can be easily and reliably detected and resolved, however,
by keeping track of a dependency graph and using common cycle-detection algorithms.
The dependency graph consists of edges such that:
<ul>
<li>
every element depends on its children
<li>
for any element A with a property using the ''element()'' function pointing to an element B,
A depends on B
<li>
if a host language defines a way for elements to refer to the rendering of other elements,
the referencing element depends on the referenced element.
For example, in SVG,
a <code>&lt;use></code> element depends on the element it referenced.
</ul>
If the graph contains a cycle,
any ''element()'' functions participating in the cycle are <a>invalid images</a>.
<!--
██████ ████████ ███████ ██████ ██████ ████████ ███ ████████ ████████ ███ ███
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ████████ ██ ██ ██████ ██████ ███████ ██████ ██ ██ ██ ██ ██████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ █████████ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██ ██ ███████ ██████ ██████ ██ ██ ██ ████████ ████████ ███ ███
-->
<h3 id='cross-fade-function'>
Combining images: the ''cross-fade()'' notation</h3>
5E11
When transitioning between images,
CSS requires a way to explicitly refer to the intermediate image
that is a combination of the start and end images.
This is accomplished with the ''cross-fade()'' function,
which indicates the two images to be combined
and how far along in the transition the combination is.
Note: Authors can also use the ''cross-fade()'' function for many simple image manipulations,
such as tinting an image with a solid color
or highlighting a particular area of the page by combining an image with a radial gradient.
The syntax for ''cross-fade()'' is defined as:
<pre class=prod><dfn>cross-fade()</dfn> = cross-fade( [ <<percentage>>? && <<image>> ] [, <<image>> | <<color>> ]? )</pre>
The function represents an image generated by
combining two images.
The <<percentage>> represents how much of the first image is retained
when it is blended with the second image.
The <<percentage>> must be between ''0%'' and ''100%'' inclusive;
any other value is invalid.
If omitted,
it defaults to the value ''50%''.
If the last argument is a <<color>>,
it represents a solid-color image with the same intrinsic dimensions as the first image.
If omitted,
it defaults to the color ''transparent''.
More precisely,
given ''cross-fade(<var>p</var> <var>A</var>, <var>B</var>)'',
where <var>A</var> and <var>B</var> are images
and <var>p</var> is a percentage between 0% and 100%,
the function represents an image
with width equal to <code>width<sub>A</sub> &times; <var>p</var> + width<sub>B</sub> &times; (1-<var>p</var>)</code>
and height equal to <code>height<sub>A</sub> &times; <var>p</var> + height<sub>B</sub> &times; (1-<var>p</var>)</code>.
The contents of the image must be constructed by
first scaling <var>A</var> and <var>B</var> to the size of the generated image,
then applying <code>dissolve(<var>A</var>,<var>p</var>) plus dissolve(<var>B</var>,1-<var>p</var>)</code>.
The "dissolve()" function and "plus" compositing operator are defined in the literature by Porter-Duff. [[PORTERDUFF]]
<!--
██████ ████████ ███ ████████ ████ ████████ ██ ██ ████████ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██
██ ████ ████████ ██ ██ ██ ██ ██ ██████ ██ ██ ██ ██ ██████
██ ██ ██ ██ █████████ ██ ██ ██ ██ ██ ████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██
██████ ██ ██ ██ ██ ████████ ████ ████████ ██ ██ ██ ██████
-->
<h2 id="gradients">
Gradients</h2>
A gradient is an image that smoothly fades from one color to another.
These are commonly used for subtle shading in background images, buttons, and many other things.
The gradient notations described in this section allow an author to specify such an image in a terse syntax,
so that the UA can generate the image automatically when rendering the page.
The syntax of a <<gradient>> is:
<pre class=prod>
<dfn>&lt;gradient></dfn> = [
<<linear-gradient()>> | <<repeating-linear-gradient()>> |
<<radial-gradient()>> | <<repeating-radial-gradient()>> |
<<conic-gradient()>> | <<repeating-conic-gradient()>> ]
</pre>
<div class=example>
As with the other <<image>> types defined in this specification,
gradients can be used in any property that accepts images.
For example:
<ul>
<li><code>background: linear-gradient(white, gray);</code>
<li><code>list-style-image: radial-gradient(circle, #006, #00a 90%, #0000af 100%, white 100%)</code>
</ul>
</div>
A gradient is drawn into a box with the dimensions of the <a>concrete object size</a>,
referred to as the <dfn export>gradient box</dfn>.
However, the gradient itself has n 4D47 o <a>intrinsic dimensions</a>.
<div class='example'>
For example, if you use a gradient as a background,
by default the gradient will draw into a <a>gradient box</a> the size of the element's padding box.
If 'background-size' is explicitly set to a value such as ''100px 200px'',
then the <a>gradient box</a> will be 100px wide and 200px tall.
Similarly, for a gradient used as a 'list-style-image',
the box would be a 1em square,
which is the <a>default object size</a> for that property.
</div>
Gradients are specified by defining the <dfn>starting point</dfn> and <dfn>ending point</dfn>
of a <dfn export>gradient line</dfn>
(which, depending on the type of gradient,
may be technically a line, or a ray, or a spiral),
and then specifying colors at points along this line.
The colors are smoothly blended to fill in the rest of the line,
and then each type of gradient defines how to use the color of the <a>gradient line</a> to produce the actual gradient.
<!--
██ ████ ██ ██ ████████ ███ ████████
██ ██ ███ ██ ██ ██ ██ ██ ██
██ ██ ████ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██████ ██ ██ ████████
██ ██ ██ ████ ██ █████████ ██ ██
██ ██ ██ ███ ██ ██ ██ ██ ██