Status: ED Work Status: Exploring Shortname: css-images Level: 4 Group: csswg ED: https://drafts.csswg.org/css-images-4/ TR: https://www.w3.org/TR/css4-images/ Editor: Tab Atkins Jr., Google, http://xanthir.com/contact/ Editor: Elika J. Etemad / fantasai, Invited Expert, http://fantasai.inkedblade.net/contact, w3cid 35400 Editor: Lea Verou, Invited Expert, http://lea.verou.me/about, w3cid 52258 Abstract: This is a delta spec over Images 3; when next published, it will be a real spec with everything filled in, but for now removing duplication is more important than having a complete spec. Issue Tracking: Tracker http://www.w3.org/Style/CSS/Tracker/products/27 Previous Version: https://www.w3.org/TR/2012/WD-css4-images-20120911/ Ignored Terms:Images Values: the <, background positioning area, border image area, , , Map, , invalid image, invalid images, concrete object size, linear-gradient(), radial-gradient(), intrinsic dimensions, default object size, CSS Ignored Vars: H, P Link Defaults: css21 (property) display, css21 (dfn) stacking context, css-backgrounds-3 (type) /center
image() = image( <### Bidi-sensitive Images ### {#bidi-images} Before listing any>? [ < >? , < >? ]! ) <image-tags> = [ ltr | rtl ] <image-src> = [ < > | < > ]
<image-src>s,
the author may specify a directionality for the image,
similar to adding a dir attribute to an element in HTML.
If a directional image is used on or in an element with opposite direction,
the image must be flipped in the inline direction
(as if it was transformed by, e.g., scaleX(-1), 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.
<ul style="list-style-image: image(ltr 'arrow.png');"> <li dir='ltr'>My bullet is on the left!</li> <li dir='rtl'>MY BULLET IS ON THE RIGHT!</li> </ul>This should render something like:
⇒ My bullet is on the left! !THGIR EHT NO SI TELLUB YM ⇐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.
element() = element( <where <> )
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?
This name conflicts with a somewhat similar function in GCPM. This needs to be resolved somehow.
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.
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 elementSources 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:
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. Elements broken across lines or columns are just rendered with their decorated bounding box. 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.
<p> element can be reused as a background elsewhere in the document:
<style>
#src { color: white; background: lime; width: 300px; height: 40px; position: relative; }
#dst { color: black; background: element(#src); padding: 20px; margin: 20px 0; }
</style>
<p id='src'>I'm an ordinary element!</p>
<p id='dst'>I'm using the previous element as my background!</p>
<pattern> element in an HTML document:
<!DOCTYPE html> <svg> <defs> <pattern id='pattern1'> <path d='...'> </pattern> </defs> </svg> <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! </p>HTML also defines that a handful of elements, such as
<canvas>, <img>, and <video>,
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:
<!DOCTYPE html>
<script>
var canvas = document.querySelector('#animated-bullet');
canvas.width = 20; canvas.height = 20;
drawAnimation(canvas);
</script>
<canvas id='animated-bullet' style='display:none'></canvas>
<ul style="list-style-image: element(#animated-bullet);">
<li>I'm using the canvas as a bullet!</li>
<li>So am I!</li>
<li>As the canvas is changed over time with Javascript,
we'll all update our bullet image with it!</li>
</ul>
<!DOCTYPE html> <p id='one' style="display:none; position: relative;">one</p> <iframe src="http://example.com"> <p id='two' style="position: relative;">I'm fallback content!</p> </iframe> <ul> <li style="background: element(#one);"> A display:none element isn't rendered, and a P element doesn't provide a paint source. </li> <li style="background: element(#two);"> The descendants of a replaced element like an IFRAME can't be used in element() either. </li> <li style="background: element(#three);"> There's no element with an id of "three", so this also gets rendered as a transparent image. </li> </ul>
<defs> element is considered to be not rendered.
<!DOCTYPE html>
<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';
}
</script>
<style>
.slide {
/* Need to be a stacking context to be element()-able. */
position: relative;
}
#prev-preview, #next-preview {
position: fixed;
...
}
#prev-preview { background: element(#prev-slide); }
#next-preview { background: element(#next-slide); }
</style>
<a id='prev-preview'>Previous Slide</a>
<a id='next-preview'>Next Slide</a>
<section class='slide'>...</section>
<section class='slide current-slide'>...</section>
...
In this example, the navigateSlides function updates the ids of the next and previous slides,
which are then displayed 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 click handlers on the preview boxes to help navigate through the page.
<img>, <video>, and <canvas> elements provide paint sources
(defined in each element's section in HTML5).
In SVG, any element that provides a paint server provides a paint source.
Note: In SVG1.1,
the <linearGradient>,
<radialGradient>,
and <pattern> elements
provide paint sources.
They are drawn as described in the spec,
with the coordinate systems defined as follows:
ElementSources interface<canvas> 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 elementSources Map object provides this.
partial interface CSS {
[SameObject] readonly attribute Map elementSources;
};
Any entries in the elementSources map with a string key
and a value that is an object providing a paint source
are made available to the ''element()'' function.
Whenever ''element()'' uses an <# character)
is first looked up in the elementSources map:
This reuse of the ID selector matches Moz behavior.
I'm trying to avoid slapping a <
Anything else that might be useful? Defining the shape of the gradient as elliptical, perhaps?
Are lengths useful in <
This is past the complexity point where it can be easily understood with just prose.
Add a diagram illustrating the possibilities,
preferably for all three kinds of gradients
(to show off the three shapes of gradient lines).
The colors in gradients are specified using color stops.
A color stop is a combination of a color and one or two positions.
(Depending on the type of gradient, that position can be a length, angle, or percentage.)
While every color stop conceptually has at least one position,
the position can be omitted in the syntax.
(It gets automatically filled in by the user agent; see below for details.)
Between two color stops there can be a color interpolation hint,
which specifies how the colors of the two color stops on either side
should be interpolated in the space between them
(by default, they interpolate linearly).
There can only be at most one color interpolation hint between any two given color stops;
using more than that makes the function invalid.
Color stops are organized into a color stop list,
which is a list of one or more color stops.
The first and last color stops in the list
must have a color
(though their position can be omitted).
Color stops and color hints are placed on a gradient line,
which defines the colors at every point of a gradient.
The gradient function defines the shape and length of the gradient line,
along with its starting point and ending point.
Color stops and color hints must be specified in order.
Percentages refer to the length of the gradient line between the starting point and ending point,
with 0% being at the starting point
and 100% being at the ending point.
Lengths are measured from the starting point in the direction of the ending point along the gradient line.
Angles are measured with 0deg pointing up,
and positive angles corresponding to clockwise rotations from there.
Color stops and color hints are usually placed between the starting point and ending point,
but that's not required;
the gradient line extends infinitely in both directions,
and a color stop or color hint can be placed at any position on the gradient line.
A color stop with two locations is mostly equivalent
to specifying two color stops with the same color,
one for each position.
Specifying two locations makes it easier to create solid-color "stripes" in a gradient,
without having to repeat the color twice.
The position of a color stop can be omitted.
This causes the color stop to position itself automatically
between the two surrounding stops.
If multiple stops in a row lack a position,
they space themselves out equally.
The following steps must be applied in order to process the <
This requires us to wait until after layout to do fix-up,
because implied-position stops (set by step 3)
may depend on stops that need layout information to place,
and which may be corrected by step 2.
Swapping steps 2 and 3 would let us interpolate color stops purely at computed-value time,
which is a nice plus,
at the cost of changing behavior from level 3 for some edge cases that triggered fixup.
Make sure this is handled well in the serialization rules.
At each color stop position,
the line is the color of the color stop.
Between two color stops,
the line's color is interpolated between the colors of the two color stops,
with the interpolation taking place in premultiplied RGBA space.
By default,
this interpolation is linear--
at 25%, 50%, or 75% of the distance between two color stops,
the color is a 25%, 50%, or 75% blend of the colors of the two stops.
However, if a color hint was provided between two color stops,
the interpolation is non-linear,
and controlled by the hint:
<script>
var bg = document.createElement('canvas');
bg.height = 200;
bg.width = 1000;
drawFancyBackground(bg);
CSS.elementSources.set('fancy', bg);
</script>
<style>
h1 {
background-image: element(#fancy);
}
</style>
As the "fancy" canvas is drawn into and animated,
the backgrounds of all the H1 elements will automatically update in tandem.
Note that the elementSources map is consulted before 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 elementSources value instead.
Cycle Detection
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:
If the graph contains a cycle,
any ''element()'' functions participating in the cycle are invalid images.
<use> element depends on the element it referenced.
Gradients
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> = [
<
background: linear-gradient(white, gray);
list-style-image: radial-gradient(circle, #006, #00a 90%, #0000af 100%, white 100%)
Conic Gradients: the ''conic-gradient()'' notation
A conic gradient starts by specifying the center of a circle,
similar to radial gradients,
except that conic gradient color-stops are placed around the circumference of the circle,
rather than on a line emerging from the center,
causing the color to smoothly transition as you spin around the center,
rather than as you progress outward from the center.
A conic gradient is specified by indicating a rotation angle, the center of the gradient,
and then specifying a list of color-stops.
Unlike linear and radial gradients,
whose color-stops are placed by specifying a <
''conic-gradient()'' Syntax
The syntax for a conic gradient is:
conic-gradient() = conic-gradient(
[ from <
The arguments are defined as follows:
Placing Color Stops
Color stops are placed on a gradient line that curves around the center in a circle,
with both the 0% and 100% locations at 0deg.
Just like linear gradients,
0deg points to the top of the page,
and increasing angles correspond to clockwise movement around the circle.
Note: It may be more helpful to think of the gradient line as forming a spiral,
where only the segment from 0deg to 360deg is rendered.
This avoids any confusion about "overlap" when you have angles outside of the rendered region.
A color-stop can be placed at a location before 0% or after 100%;
though these regions are never directly consulted for rendering,
color stops placed there can affect the color of color-stops within the rendered region
through interpolation or repetition (see repeating gradients).
For example, ''conic-gradient(red -50%, yellow 150%)'' produces a conic gradient
that starts with a reddish-orange color at 0deg (specifically, #f50),
and transitions to an orangish-yellow color at 360deg (specifically, #fa0).
The color of the gradient at any point is determined by first finding the unique ray
anchored at the center of the gradient that passes through the given point.
The point's color is then the color of the gradient line at the location where this ray intersects it.
Conic Gradient Examples
All of the following ''conic-gradient()'' examples are presumed to be applied to a box that is 300px wide and 200px tall, unless otherwise specified.
background: conic-gradient(#f06, gold);
background: conic-gradient(at 50% 50%, #f06, gold);
background: conic-gradient(from 0deg, #f06, gold);
background: conic-gradient(from 0deg at center, #f06, gold);
background: conic-gradient(#f06 0%, gold 100%);
background: conic-gradient(#f06 0deg, gold 1turn);
background: conic-gradient(white -50%, black 150%);
background: conic-gradient(white -180deg, black 540deg);
background: conic-gradient(hsl(0,0%,75%), hsl(0,0%,25%));
background: conic-gradient(from 45deg, white, black, white);
background: conic-gradient(hsl(0,0%,87.5%), white 45deg, black 225deg, hsl(0,0%,87.5%));
Note that offsetting every color stop by the rotation angle instead would not work and produces an entirely different gradient:
background: conic-gradient(white 45deg, black 225deg, white 405deg);
background: radial-gradient(gray, transparent),
conic-gradient(red, yellow, lime, aqua, blue, magenta, red);
border-radius: 50%;
width: 200px; height: 200px;
background: conic-gradient(yellowgreen 40%, gold 0deg 75%, #f06 0deg);
border-radius: 50%;
width: 200px; height: 200px;
Repeating Gradients: the ''repeating-linear-gradient()'', ''repeating-radial-gradient()'', and ''repeating-conic-gradient()'' notations
In addition to ''linear-gradient()'', ''radial-gradient()'', and ''conic-gradient()'',
this specification defines repeating-linear-gradient(),
repeating-radial-gradient(),
and repeating-conic-gradient() values.
These notations take the same values
and are interpreted the same
as their respective non-repeating siblings defined previously.
background: repeating-conic-gradient(gold, #f06 20deg);
background: repeating-conic-gradient(
hsla(0,0%,100%,.2) 0deg 15deg,
hsla(0,0%,100%,0) 0deg 30deg
) #0ac;
background: repeating-conic-gradient(black 0deg 25%, white 0deg 50%);
background-size: 60px 60px;
The same checkerboard can be created via non-repeating conic gradients:
background: conic-gradient(black 25%, white 0deg 50%, black 0deg 75%, white 0deg);
background-size: 60px 60px;
Gradient Color-Stops
<color-stop-list> =
[ <
Plus:
Sequence:
N:
Note: If the hint is placed halfway between the two stops,
this is thus the ordinary linear interpolation.
If the hint is placed anywhere else,
it dictates the position of the "halfway point",
where the color is an equal blend between the two color stops,
and produces smooth, even blends between the color stops and the "halfway point".
Before the first color stop,
the line is the color of the first color stop.
After the last color stop, the line is the color of the last color stop.
If multiple color stops have the same position,
they produce an infinitesimal transition from the one specified first in the rule
to the one specified last.
In effect, the color suddenly changes at that position rather than smoothly transitioning.
PlogH(.5).
(1 - C) of the first stop and C of the second stop.
1. linear-gradient(red, white 20%, blue)
=1=>
linear-gradient(red 0%, white 20%, blue 100%)
2. linear-gradient(red 40%, white, black, blue)
=13=>
linear-gradient(red 40%, white 60%, black 80%, blue 100%)
3. linear-gradient(red -50%, white, blue)
=13=>
linear-gradient(red -50%, white 25%, blue 100%)
4. linear-gradient(red -50px, white, blue)
=13=>
linear-gradient(red -50px, white calc(-25px + 50%), blue 100%)
5. linear-gradient(red 20px, white 0px, blue 40px)
=2=>
linear-gradient(red 20px, white 20px, blue 40px)
6. linear-gradient(red, white -50%, black 150%, blue)
=12=>
linear-gradient(red 0%, white 0%, black 150%, blue 150%)
7. linear-gradient(red 80px, white 0px, black, blue 100px)
=23=>
linear-gradient(red 80px, white 80px, black 90px, blue 100px)
8. linear-gradient(red, 25%, white)
=14=>
linear-gradient(red 0%, rgb(100%,50%,50%) 25%, white 100%)
linear-gradient(90deg, red, transparent, blue)
In premultiplied space,
transitions to or from "transparent" always look nice:
On the other hand,
if a gradient were to incorrectly transition in non-premultiplied space,
the colors near "transparent" would noticeably darken to a grayish color,
because "transparent" is actually a shorthand for ''rgba(0,0,0,0)'', or transparent black: