Copyright © 2009 W3C® (MIT, ERCIM, Keio), All Rights Reserved. W3C liability, trademark and document use rules apply.
This CSS Image Values module defines the syntax for <image> values in CSS. <image> values can be a single URI to an image, a list of URIs denoting a series of fallbacks, sprites (image slices), or gradients.
This is a public copy of the editors' draft. It is provided for discussion only and may change at any moment. Its publication here does not imply endorsement of its contents by W3C. Don't cite this document other than as work in progress.
The (archived) public mailing list www-style@w3.org (see instructions) is preferred for discussion of this specification. When sending e-mail, please put the text “css3-images” in the subject, preferably like this: “[css3-images] …summary of comment…”
This document was produced by the CSS Working Group (part of the Style Activity).
This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.
url()’ notation
image()’ notation
This section is non-normative.
In CSS Levels 1 and 2, image values, such as those used in the
‘background-image’ property,
could only be given by a single URI value. This module introduces
additional notations that allow a 2D image to be given as a list of URIs
denoting fallbacks, as a slice of a larger image (sprite), and as a
gradient.
A document or implementation cannot conform to CSS Image Values Level 3 alone, but can claim conformance to CSS Image Values Level 3 if it satisfies the conformance requirements in this specification when implementing CSS or another host language that normatively references this specification.
Conformance to CSS Namespaces is defined for two classes:
The conformance requirements are expressed with a combination of descriptive assertions and RFC 2119 terminology. The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the normative parts of this document are to be interpreted as described in RFC 2119. However, for readability, these words do not appear in all uppercase letters in this specification. All of the text of this specification is normative except sections explicitly marked as non-normative, examples, and notes. [RFC2119]
Examples in this specification are introduced with the words "for
example" or are set apart from the normative text with
class="example", like this:
This is an example of an informative example.
Informative notes begin with the word "Note" and are set apart from the
normative text with class="note", like this:
Note, this is an informative note.
The <image> value type denotes a 2D image. It is defined as
<image> = <url> | <sprite> | <image-list> | <linear-gradient> | <radial-gradient>
Image values can be used in many CSS properties, including the
‘background-image’, ‘list-style-image’, ‘cursor’ properties [CSS21].
url()’ notationThe simplest way to indicate an image is to reference an image file by
URI. This is done with the ‘url()’ notation, defined in [CSS21].
In the example below, a background image is specified with
‘url()’ syntax:
background-image: url(wavy.png);
A portion of an image may be referenced (clipped out and used as a standalone image) by use of fragment identifiers. Need a spec to reference here. Expecting to get one from Media Fragments WG.
For example,
background-image: url('logos.png#xywh=10,30,60,20')
uses the 60 pixel by 20 pixel rectangle of logos.png
beginning at the point 10 pixels in from the left, 30 pixels down from
the top.
Note that quotation marks are required here, because
unquoted commas are not allowed in ‘url()’ syntax.
image()’ notationThe ‘image()’ notation allows an
author to specify multiple images, each one a fallback for the previous.
The UA must use only the first image that it can load and display. The
syntax for ‘image()’ is defined as
<image-list> = image( [ <image-decl> , ]* [ <image-decl> ] [ or <color> ]? )
where <image-decl> is given by
<image-decl> = [ <string> | <url-token> ] [ snap? && <resolution> ]?
<url-token> is given as
[!#$%&*-~]|{nonascii}|{escape} (i.e. the contents of
‘url()’) using the productions in the
CSS2.1
tokenization. The <url-token> must not contain
unescaped brackets, commas, white space characters, single quotes (') or
double quotes ("); if it does the ‘image()’ containing it is invalid.
Each string or url-token represents the URI of an image. If a
resolution is given, then the image must be rendered at the specified
resolution. If the ‘snap’ keyword is
also specified, and the image is a raster image, then the image must be
rendered at the resolution closest to the specified resolution that would
result in no pixel rounding. I don't think "no pixel
rounding" is the right terminology here... basically we want to avoid
blurry images.
The optional color at the end of the list is the fallback color. It is used if, and only if, none of the images can be loaded and displayed (e.g. because they are in unsupported formats, or cannot be found, or are corrupted in some way, or because image loading is disabled in the UA). It is treated as a single-color image with no intrinsic dimensions.
For example, the rule below would tell the UA to load ‘wavy.svg’ if it can; failing that to load
‘wavy.png’ and display it at 150dpi;
failing that to display ‘wavy.gif’;
and finally, if none of the images can be loaded and displayed, to use
the color ‘blue’ to create a
dimensionless background image.
background-image: image(wavy.svg, 'wavy.png' 150dpi, "wavy.gif" or blue);
The ‘background-image’
property specifies that dimensionless images must stretch to cover the
entire background positioning area [CSS3BG], so if none of the
specified images can be displayed the background will be painted blue.
As with any image, this fallback will be painted over the ‘background-color’ (if any).
If the URL for the image ends in a file extension (a period followed by
letters) of four ascii letters ([a-zA-Z]) or less, then the UA
may skip trying to load images whose file extension matches an
image format it knows it does not support. For example, a UA that knows
it supports JPEG and GIF, but expects to encounter many PNG and SVG
images may choose to skip over images with filenames ending in
‘.png’ or ‘.svg’ without loading them to check their MIME
type. However it must not skip over images with filenames ending in
extensions it doesn't recognize.
The image sprites syntax is different from image slices noted above. Image slice syntax allows you to specify the coordinates of a region in the image and use that as a standalone image, but all the coordinates must be specified inline. This is great for one-off cutouts, but becomes unnecessarily verbose when the slices fit into a grid structure. The goal of this section is to define a syntax that:
As yet unwritten. Discussions/proposals include:
A gradient is a browser-generated image specified entirely in CSS,
which consists of smooth fades between several colors. There are two
basic kinds of gradients currently defined, linear and radial. These are
specified by the linear-gradient() and
radial-gradient() functions, and can be used any place an
image can currently be used. As well, two variant functions
repeating-linear-gradient() and
repeating-radial-gradient() are defined.
In many places this specification references a box, such as "the box's top-left corner" or "the box's right side". In all of these circumstances, the box refers to the rectangle that would be filled by an SVG image without intrinsic dimensions used in the same context. See the CSS 2.1 spec for clarification on this.
As with the other <image> types defined in this specification, gradients can be used in any property that accepts images. For example:
background: linear-gradient(white, gray);
list-style-image: radial-gradient(circle, #006, #00a 90%,
#0000af 100%, white 100%)
A linear gradient is created by specifying a gradient-line and then several colors placed along that line. The image is constructed by creating an infinite canvas and painting it with lines perdendicular to the gradient-line, with the color of the painted line being the color of the gradient-line where the two intersect.
linear-gradient() syntaxlinear-gradient([<bg-position> || <angle>,]? <color-stop>, <color-stop>[, <color-stop>]*);
The first argument to the function specifies the
gradient-line, which gives the gradient a direction. The
gradient typically starts at the starting-point and ends at the
ending-point, but this is not a firm requirement. The starting-point
merely defines where 0%, 0px, etc are located when used in the
<color-stop> construction, and the ending-point
defines where 100% is. Color-stops can have values below 0 or above 100%,
and they refer to positions on the gradient-line before the
starting-point or after the ending-point as expected.
<bg-position> is taken from the Backgrounds and
Borders Module, and has the same definition. <angle>
is from the Values Module, and has the same definition.
If the <angle> is omitted in the first argument, the
<bg-position> specifies the starting-point of the
gradient-line. The ending-point is obtained by rotating the
starting-point 180 degrees around the center of the box. For example,
specifying "right" would place the starting-point in the center of the
right edge of the box (as described by Backgrounds and Borders) and the
ending-point in the center of the left edge, producing a horizontal
gradient that went from right to left. Specifying "top left" would place
the starting-point in the top-left corner of the box, and the
ending-point in the bottom-right corner of the box, and would produce a
diagonal gradient. Specifying "left 10px" would place the starting-point
at the left edge, 10px from the top and the ending-point on the right
edge, 10px from the bottom - if you would like a horizontal gradient that
start 10px in from the left edge, specify "10px center" or "left 10px
center". Refer to the Backgrounds and Borders Module for more examples.
If the <bg-position> is omitted in the first
argument, the starting-point is in one of the box's corners, based on the
<angle>. If the angle is between [0deg,90deg), the
starting point is the bottom-left corner. If the angle is between
[90deg,180deg), the starting point is the bottom-right corner. If the
angle is between [180deg,270deg), the starting point is the top-right
corner. If the angle is between [270deg,360deg), the starting point is
the top-left corner. The gradient-line extends from this corner in the
direction specified by <angle>. The ending-point is
the point on the gradient-line where a line drawn perpendicular to the
gradient-line intersects the corner opposite the starting corner.
If both <bg-position> and <angle>
are specified, then the starting-point is determined by the
<bg-position>, and the gradient-line extends from that
point in the direction specified by the <angle>. The
ending-point is determined similarly to the method in the previous
paragraph - the <angle> defines a ‘starting corner’ (though it has no effect on where
the gradient-line starts - that's determined by the
<bg-position>), and the ending-point is the point on
the gradient-line where a line drawn perpendicularly to it would
intersect the corner opposite the starting corner. (In some cases this
may cause the gradient to ‘reverse
direction’, for example if you specify "-10px -10px 135deg"
- the ending-point is identical to if you had specified an angle of
315deg.)
If both are omitted, it is equivalent to simply specifying "top" for the first argument, and defines a vertical gradient.
If both starting-point and ending-point are the same, then the gradient is just a solid color, defined by the last color-stop in the list. For example, "left top 90deg", and "center center" will both result in a solid-color gradient.
The <color-stop> stands for:
<color> [ <percentage> | <length> ]?
The color-stops are points placed along the line defined by the gradient-line at the beginning of the rule. Percentages refer to the length of the gradient-line, 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. With either measure, color-stops may be placed before the starting-point or after the ending-point; this is perfectly valid, as the starting-point and ending-point are just convenient references, and do not directly interact in the display of the gradient. At each color-stop, the line is the color of the color-stop. 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. Between two color-stops, the colors are interpolated as SVG gradients.
If the first color-stop does not have a <length> or
<percentage>, it defaults to 0%. If the last
color-stop does not have a <length> or
<percentage>, it defaults to 100%.
Color-stops must be specified in order. After assigning default values to the first and last stops if necessary, if a color-stop has a specified position that is less than the specified position of any color-stop before it in the list, its position is changed to be equal to the largest specified position of any color-stop before it.
Finally, after correcting any misordered stops, if any other color-stop
does not have a <length> or
<percentage> specified, then all adjacent color-stops
without a <length> or <percentage>
are collected together, and then evenly spaced between the color-stops on
the boundaries that do have a <length> or
<percentage>.
If multiple color-stops fall on the same point, 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 point rather than smoothly transitioning.
It is recommended that authors not mix different types of units, such
as px, em, or %, in a single rule, as this can cause a color-stop to
unintentionally try to move before an earlier one. For example, the rule
linear-gradient(red, yellow 100px, blue 50%) would work as
expected as long as the gradient's block was at least 200px tall. If it
was 150px tall, however, the blue color-stop would be specifying a
position of "75px", before the yellow color-stop, and would be corrected
to a position of 100px.
All of the following examples are applied to a box that is 200px wide and 100px tall.
Below are various ways of specifying a basic vertical gradient:
linear-gradient(top, yellow, blue);
linear-gradient(bottom, blue, yellow);
linear-gradient(-90deg, yellow, blue);
linear-gradient(270deg, yellow, blue);
linear-gradient(top, yellow 0%, blue 100%);
linear-gradient(center top, yellow, blue);
linear-gradient(yellow, blue);

This gradient goes from the upper-left to the lower-right corner.
linear-gradient(top left, yellow, blue);
linear-gradient(0 0, yellow, blue);
linear-gradient(top 0px left 0px, yellow, blue);
linear-gradient(bottom right, blue, yellow);

This demonstrates the use of an angle in the gradient. Compare this image with the previous example. In both gradients, the top-left of the box is pure yellow, and the bottom-right of the box is pure blue. The difference is in the angle that the gradient follows.
linear-gradient(-45deg, yellow, blue);
linear-gradient(315deg, yellow, blue);

This demonstrates a 3-color gradient, and how to specify the location of a stop explicitly:
linear-gradient(top, yellow, blue 20%, #0f0);

Here we use the full background-position syntax to specify the direction of the gradient. Notice that in this example a substantial portion of the image is pure yellow and pure blue, because the starting and ending points of the gradient-line are not on the box edge, but rather somewhere within the box.
linear-gradient(20px 30px, yellow, blue);
linear-gradient(left 20px top 30px, yellow, blue);

A radial gradient is specified in a manner very similar to a linear gradient. You define a gradient-line by providing a starting-point and an angle and place color-stops along this line, but also specify a gradient-shape that determines where the gradient-line ends and the overall appearance of the gradient image. The image is constructed by creating an infinite canvas and painting it with concentric scaled copies of the ending-shape, with the color of the painted shape being the color of the gradient-line where the two intersect.
radial-gradient() Syntaxradial-gradient([<bg-position> || <angle>,]? [<shape> || <size>,]? <color-stop>, <color-stop>[, <color-stop>]*)
The first argument to the function specifies the
gradient-line.
<bg-position> is taken from the Backgrounds and
Borders Module, and has the same definition. It specifies the
starting-point of the gradient-line, and the center of the
gradient-shape. If omitted, it defaults to center.
<angle> is from the Values Module, and has the same
definition. If omitted, it defaults to 0deg. The gradient-line extends
from the starting-point at this angle.
The second argument to the function specifies the
gradient-shape. <shape>
is defined as [ circle | ellipse ]. circle
indicates that the gradient-shape will be a circle with a constant
radius. ellipse indicates that the gradient-shape will be an
axis-aligned ellipse (that is, its major and minor radiuses will be
horizontal and vertical, not necessarily in that order). If omitted, it
defaults to ellipse.
<size> is defined as [ closest-side |
closest-corner | farthest-side | farthest-corner | contain | cover
]. If <shape> is circle and
<size> is closest-side, the ending-shape
is a circle sized so that it exactly meets the side of the box closest to
its center. For example, if the box was 100px wide and 200px tall, and
the starting-point of the gradient-line was '10% 10%', then
the closest side is the left side of the box (it is 10px from the
starting-point, while the top is 20px from it, and the right and bottom
sides are much further). The gradient-shape would be a circle with a
radius of 10px. If <shape> is ellipse and
<size> is closest-side, the
gradient-shape is an ellipse sized so that it exactly meets the vertical
and horizontal sides of the box closest to its center. Using the same box
and starting-point as the previous example, the gradient-shape would be
an ellipse with a 20px vertical radius and a 10px horizontal radius. (If
necessary, such as if the starting-point is outside of the box, extend
the sides of the box so that there is a line the ellipse can meet.)
farthest-side is identical to closest-side,
except that the gradient-shape is sized to meet the side of the box that
is farthest from its center (or the farthest vertical and horizontal
sides, in the case of ellipse). closest-corner
and farthest-corner size the gradient-shape so that it
exactly meets the closest or farthest corner of the box from its center,
respectively. If <shape> is ellipse, the
gradient-shape has the same ratio of width to height that it would if
closest-side or farthest-side were specified,
as appropriate. contain is a synonym for
closest-side, and cover is a synonym for
farthest-corner. If omitted, <size>
defaults to cover.
In certain circumstances the given parameters may define a degenerate
shape - a circle or ellipse with a radius of 0. In these instances the
gradient image is just a solid color equal to the color of the last
color-stop in the rule. The following combinations of values will trigger
this: closest-side if the starting-point is on a box edge,
closest-corner if the starting-point is on a box corner, and
ellipse and closest-corner if the
starting-point is on a box-edge.
The ending-point of the gradient-line is determined by extending a line
from the starting-point in the direction specified by the
<angle> in the first argument. The ending-point is
where this line intersects the gradient-shape.
<color-stop> is defined identically to its treatment
in linear-gradient(). As in linear gradients, 0% is at the
starting-point of the gradient-line, and 100% is at the ending-point.
Distances past 100% can be specified, and simply indicate a color-stop
placed on the gradient-line a corresponding distance from the
starting-point. Negative distances are allowed in a radial gradient and
work the same as in linear gradients with respect to setting the color of
the gradient-line, but colors before the starting-point of the
gradient-line are not displayed. For example, radial-gradient(red
-50px, yellow 100px) would produce an elliptical gradient which
starts with a reddish-orange color in the center (the color 1/3 between
red and yellow) and transitions to yellow at 100px wide.
All of the following examples are applied to a box that is 200px wide and 100px tall.
These examples demonstrate the basic syntax for radial gradients:
radial-gradient(yellow, green);
radial-gradient(center, ellipse cover, yellow 0%, green 100%);
radial-gradient(50% 50%, farthest-corner, yellow, green);

radial-gradient(circle, yellow, green);

radial-gradient(red, yellow, green);

The next two images show a gradient originating from somewhere other than the center of the box:
radial-gradient(bottom left, farthest-side, red, yellow 50px, green);

radial-gradient(bottom left 90deg, farthest-side, red, yellow 50px, green);

Pay attention to the differences in these two images. In both of them the distance between the red color-stop and the yellow color-stop is 50px; the difference is the direction this distance is measured. By default the angle is 0deg, which means that distances are measured from the center going right. An angle of 90deg makes it measure from the center going up. Specifying an angle between 0deg and 90deg would produce a gradient somewhere between these two examples.
Here we illustrate a ‘contain’
gradient.
radial-gradient(20px 30px, contain, red, yellow, green)

radial-gradient(20px 30px, circle contain, red, yellow, green)

If the difference in the first and last color-stops is 0 (for example,
in the rule repeating-linear-gradient(red 10px, blue 10px)),
the gradient defines a solid-color image with the color of the last
color-stop in the rule.
This specification defines the following units as part of the <resolution> value type:
px’ unit
The default resolution of raster images in CSS is
‘1dppx’.