CSS Shapes Module Level 2

Editor’s Draft,

Specification Metadata
This version:
https://drafts.csswg.org/css-shapes-2/
Latest published version:
https://www.w3.org/TR/css-shapes-2/
Issue Tracking:
CSSWG Issues Repository
Inline In Spec
Editors:
(Microsoft Corporation)
(Adobe Systems, Inc.)
Suggest an Edit for this Spec:
GitHub Editor

Abstract

This draft contains the features of CSS relating to wrapping content around and inside shapes. It (implicitly for now) includes and extends the functionality of CSS Shapes Level 1 [CSS-SHAPES]. The main points of extension compared to level 1 include additional ways of defining shapes, defining an exclusion area using a shape, and restricting an element’s content area using a shape.

CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, etc.

Status of this document

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.

Please send feedback by filing issues in GitHub (preferred), including the spec code “css-shapes” in the title, like this: “[css-shapes] …summary of comment…”. All issues and comments are archived. Alternately, feedback can be sent to the (archived) public mailing list www-style@w3.org.

This document is governed by the 15 September 2020 W3C Process Document.

1. Introduction

This section is not normative.

Level 1 of this specification defined properties to control the geometry of an element’s float area. This level defines how shapes apply to exclusions. It also includes a shape-inside property for applying a shape to an element’s content area. Finally, it defines new ways of specifying shapes for all of these applications.

1.1. Value Definitions

This specification follows the CSS property definition conventions from [CSS2] using the value definition syntax from [CSS-VALUES-3]. Value types not defined in this specification are defined in CSS Values & Units [CSS-VALUES-3]. Combination with other CSS modules may expand the definitions of these value types.

In addition to the property-specific values listed in their definitions, all properties defined in this specification also accept the CSS-wide keywords as their property value. For readability they have not been repeated explicitly.

2. Terminology

exclusion area

The area used for excluding inline flow content around an exclusion box. The exclusion area is equivalent to the border box for an exclusion box. This specification’s shape-outside property can be used to define arbitrary, non-rectangular exclusion areas. The shape-inside property also defines an exclusion area, but in this case it is the area outside the shape that inline content avoids.

float area

The area used for wrapping content around a float element. By default, the float area is the float element’s margin box. This specification’s shape-outside property can be used to define arbitrary, non-rectangular float areas.

content area

The content area is normally used for layout of the inline flow content of a box.

direction-agnostic size

The direction-agnostic size of a box is equal to the length of the diagonal of the box, divided by sqrt(2).

Note: This is a method of averaging the width and height of a box used by SVG in some cases, when a percentage of a box’s size is desired but the context doesn’t specifically favor the width or the height. For square boxes, this is the same as the width/height.

3. Shapes

Shapes define arbitrary geometric contours around which inline content flows. The shape-outside property defines the float area for a float, and the exclusion area for an exclusion.

4. Basic Shapes

basic-shape

In all current engines.

Firefox54+Safari10.1+Chrome37+
Opera24+Edge79+
Edge (Legacy)NoneIENone
Firefox for Android54+iOS Safari10.3+Chrome for Android37+Android WebView37+Samsung Internet3.0+Opera Mobile24+

Add the final level 1 section.

4.1. Supported Shapes

clip-path

In all current engines.

Firefox3.5+Safari9.1+Chrome55+
Opera42+Edge79+
Edge (Legacy)12+IE10+
Firefox for Android4+iOS Safari9.3+Chrome for Android55+Android WebView55+Samsung Internet6.0+Opera Mobile42+

Add the final level 1 sections.

4.1.1. The shape() Function

While the path() function allows reuse of the SVG path syntax to define more arbitrary shapes than allowed by more specialized shape functions, it requires writing a path as a single string (which is not compatible with, for example, building a path piecemeal with var()), and inherits a number of limitations from SVG, such as implicitly only allowing the px unit.

The shape() function defines a path in the same way, but does so with more standard CSS syntax, and allows the full range of CSS functionality, such as additional units and math functions.

shape() = shape( <fill-rule>? from <coordinate-pair>, <shape-command>#)

The <fill-rule> is interpreted identically to the same argument in path().

The rest of the arguments define a list of path data commands, identical to that of an SVG Path, which the function represents.

The from <coordinate-pair> represents the starting point for the first shape-command. It adds an initial absolute moveto to the list of path data commands, with the <coordinate-pair> measured from the top-left corner of the reference box.

The sequence of <shape-command>s represent further path data commands. Each command’s starting point is the previous command’s ending point.

<shape-command> = <move-command> | <line-command> | <hv-line-command> |
                  <curve-command> | <smooth-command> | <arc-command> | close
<coordinate-pair> = <length-percentage>{2}
<by-to> = by | to
<move-command> = move <by-to> <coordinate-pair>
<line-command> = line <by-to> <coordinate-pair>
<hv-line-command> = [hline | vline] <by-to> <length-percentage>
<curve-command> = curve <by-to> <coordinate-pair> via <coordinate-pair>{1,2}
<smooth-command> = smooth <by-to> <coordinate-pair> [via <coordinate-pair>]?
<arc-command> = arc <by-to> <coordinate-pair> of <length-percentage>{1,2}
                    [ <arc-sweep> || <arc-size> || <angle> ]?
<arc-sweep> = cw | ccw
<arc-size> = large | small
<coordinate-pair> = <length-percentage>{2}
Defines a pair of coordinates, representing a rightward and downward offset, respectively, from a specified reference point. Percentages are resolved against the width or height, respectively, of the reference box.
<by-to> = by | to
Every command can be specified in "absolute" or "relative" coordinates, determined by their <by-to> component. to indicates that any <coordinate-pair>s in the command are relative to the top-left corner of the reference box, while by indicates that the <coordinate-pair>s are relative to the command’s starting point.

Note: In either case, <percentage> values in <coordinate-pair>s are always computed relative to the reference box’s size.

<move-command> = move <by-to> <coordinate-pair>
Adds a moveto command to the list of path data commands, with an ending point specified by the <coordinate-pair>.

This draws nothing, and merely "moves the pen" for the next command.

Note: This starts a new subpath, for the purpose of the close command.

<line-command> = line <by-to> <coordinate-pair>
Adds a lineto command to the list of path data commands, with an ending point specified by the <coordinate-pair>.

This draws a straight line from the command’s starting point to its ending point.

<hv-line-command> = [hline | vline] <by-to> <length-percentage>
Adds a horizontal (for hline) or vertical (for vline) lineto command to the list of path data commands.

This is equivalent to a line command with the <length-percentage> given as the horizontal or vertical component, respectively, of the <coordinate-pair>, and the other component specified appropriately to make the line horizontal or vertical.

<curve-command> = curve <by-to> <coordinate-pair> via <coordinate-pair>{1,2}
Adds a Bézier curve command to the list of path data commands, ending at the point specified by the first <coordinate-pair>.

The via component specifies control points for the curve: if a single <coordinate-pair> is provided, the command specifies a quadratic curve; if two <coordinate-pair>s are provided, it specifies a cubic curve.

<smooth-command> = smooth <by-to> <coordinate-pair> [via <coordinate-pair>]?
Adds a smooth Bézier curve command to the list of path data commands, ending at the point specified by the first <coordinate-pair>.

The via component specifies control points for the curve: if it’s omitted, the command specifies a smooth quadratic curve; if it’s provided, if specifies a smooth cubic curve.

Note: A smooth command is equivalent to a curve command with the first control point automatically specified as the reflection of the previous curve’s second control point around the starting point, or as the starting point if the previous path data command wasn’t a curve. This ensures G1 continuity with the previous command, so the curve appears to smoothly continue from the previous command, rather than possibly making a sudden direction change.

<arc-command> = arc <by-to> <coordinate-pair> of <length-percentage>{1,2} [ <arc-sweep> || <arc-size> || <angle> ]
Add an elliptical arc command to the list of path data commands, ending at the point specified by the <coordinate-pair>.

The of component specifies the size of the ellipse that the arc is taken from. The first <length-percentage> provides the horizontal radius of the ellipse and the second provides the vertical radius. Like for <coordinate-pair>s, <percentage> values are resolved against the width or height of the reference box, as appropriate.

If only one <length-percentage> is provided, both radiuses use the provided value. In that case, <percentage> values are resolved against the direction-agnostic size of the reference box (similar to the circle() function).

Note that SVG has some specific error-handling for the ellipse radiuses:
  • if the endpoint is the same as the starting point, the command does nothing

  • if either radius is zero, the command is equivalent to a <line-command> to the ending point

  • if either radius is negative, its absolute value is used instead

  • if the radiuses don’t describe an ellipse large enough to intersect both the starting point and ending point (after rotation by the specified <angle>), they are scaled up uniformly until the ellipse is just large enough to reach.

The ellipse described by the specified radiuses defaults to being axis-aligned, but can be rotated by specifying an <angle>. Similar to the rotate() transform function, positive angles specify a clockwise rotation, and negative angles specify a counterclockwise rotation. If omitted, this defaults to 0deg.

The ending point, radiuses, and angle, taken together, usually define two possible ellipses that intersect the starting point and ending point, and each ellipse can be traced in either direction, for a total of four possible arcs. The <arc-sweep> and <arc-size> components specify which of these arcs is desired:

  • <arc-sweep> can be cw or ccw, indicating that the arc that is traced around the ellipse clockwise or counter-clockwise from the center, respectively, must be chosen. If omitted, this defaults to ccw.

    Note: In the SVG arc command, cw corresponds to the value 1 for the sweep flag, and ccw to the value 0.

  • <arc-size> can be large or small, indicating that the larger or smaller, respectively, of the two possible arcs must be chosen. If omitted, this defaults to small.

    Note: In the SVG arc command, large corresponds to the value 1 for the large flag, and small to the 0.

    Note: If the starting and ending points are on exactly opposite sides of the ellipse, both possible arcs are the same size, but also there is only one possible ellipse. In this case, the <arc-sweep> distinguishes which of the two possible arcs will be chosen, and <arc-size> has no effect.

A depiction of the two possible ellipses, and four possible arcs, that can be chosen between.
close
Adds a closepath command to the list of path data commands.

Note: This is similar to a line command with its ending point set to the starting point of the subpath. When specifying a raw shape, they’re identical, but if the path is stroked, the ending point of the close command is smoothly joined with the start of the subpath, which affects how line-joins and line-caps are rendered.

4.1.1.1. Interpolating the shape() Function

shape() and path() functions can be interpolated with each other if their associated list of path data commands is the same length and has the same commands, in order. If the starting and ending values are both path() functions, the interpolated value is a path() function; otherwise it’s a shape() function. In either case, the interpolated value must represent the same list of path data commands, with each command having its numerical components interpolated between the corresponding components of the starting and ending list.

For this purpose, commands are "the same" if they use the same command keyword, and use the same <by-to> keyword. For curve and smooth, they also must have the same number of control points.

If an arc command has different <arc-sweep> between its starting and ending list, then the interpolated result uses cw for any progress value between 0 and 1. If it has different <arc-size> keywords, then the interpolated result uses large for any progress value between 0 and 1.

Note: The arc keyword interpolation rules are meant to match existing SVG path interpolation rules.

5. Referencing SVG shapes

An SVG shape can be referenced using the url() syntax. The shape can be any of the SVG basic shapes or a path element.

results of referencing SVG shapes
<style>
div {
  height: 400px;
  width: 400px;
}
.in-a-circle {
  shape-outside: url(#circle_shape);
}

.in-a-path {
  shape-outside: url(#path-shape);
}
</style>

<svg ...>
  <circle id="circle_shape" cx="50%" cy="50%" r="50%" />
  <path id="path-shape" d="M 100 100 L 300 100 L 200 300 z" />
</svg>

<div class="around-a-circle">...</div>
<div class="around-a-path">...</div>

6. Shapes from Image

Add the final level 1 section.

One suggestion is to define a shape based on an element’s rendered content. This could have security implications.

Another suggestion is to add something to an image() function that determines the relevant pixels to use (both for defining a shape and for display).

7. Shapes from Box Values

Add the final level 1 section.

8. Declaring Shapes

A shape can be declared with the shape-outside property, with possible modifications from the shape-margin property. The shape defined by the shape-outside and shape-margin properties changes the geometry of a float element’s float area and an exclusion element’s exclusion area.

A shape can be declared with the shape-inside property, with possible modifications from the shape-padding property. The shape defined by the shape-inside and shape-padding properties defines an exclusion area that contributes to the element’s wrapping context. The shape-inside property applies to all block-level elements.

The red box illustrates an exclusion element’s content box, which is unmodified and subject to normal CSS positioning (here absolute positioning).
<style type="text/css">
.exclusion  {
  wrap-flow: both;
  position: absolute;
  top: 25%;
  left: 25%;
  width: 50%;
  height: 50%;
  shape-outside: circle(50% at 50% 50%);
  border: 1px solid red;
}
</style>

<div style=”position: relative;”>
  <div class=”exclusion”></div>
  Lorem ipsum dolor sit amet...
</div>

Example rendering of circle shape and box model.

8.1. The shape-outside Property

shape-outside

In all current engines.

Firefox62+Safari10.1+Chrome37+
Opera24+Edge79+
Edge (Legacy)NoneIENone
Firefox for Android62+iOS Safari10.3+Chrome for Android37+Android WebView37+Samsung Internet3.0+Opera Mobile24+

Add the final level 1 section with the change that shape-outside applies to block-level elements and has an effect if the element is an exclusion.

8.2. The shape-inside Property

The shape-inside property adds one or more exclusion areas to the element’s wrapping context. This modifies the normal rectangular shape of the content area to a possibly non-rectangular wrapping area. The exclusion areas are defined by subtracting the shape from the element’s content area. Any part of the shape outside the element’s content area has no effect.

Name: shape-inside
Value: auto | outside-shape | [ <basic-shape> || shape-box ] | <image> | display
Initial: auto
Applies to: block-level elements
Inherited: no
Percentages: n/a
Computed value: computed lengths for <basic-shape>, the absolute URL for <url>, otherwise as specified
Canonical order: per grammar
Animation type: as defined for <basic-shape>, otherwise discrete

The values of this property have the following meanings:

auto
The shape is computed based on the content box of the element.
outside-shape
The shape is computed based on the shape defined by the shape-outside and shape-margin properties.
<basic-shape>
The shape is computed based on the values of one of inset(), circle(), ellipse() or polygon().
<url>
If the <uri> references an SVG shape element, that element defines the shape. Otherwise, if the <uri> references an image, the shape is extracted and computed based on the alpha channel of the specified image.

If the <uri> does not reference an SVG shape element or an image, the effect is as if the value auto had been specified.

display
The shape is computed based on the shape of the display as described in css-round-display.

The shape-inside property applies to floats.

The shape-inside property may not apply on some elements such as elements with a computed display value of table.

Content flowing with and without a shape-inside
Effect of shape-inside on inline content.

Overflow content avoids the exclusion area(s) added by shape-inside and shape-padding (as well as any other exclusion areas in the element’s wrapping context). In other words, overflow continues outside the rectangular bounds of the element.

Overflow interacting with rounded rect Overflow interacting with ellipse
Overflow interacting with exclusion areas defined by shape-inside and shape-padding.

improve the illustration above, using text to show overflow instead of grey boxes.

When a shape-inside has a definite size (no percentages used in the shape’s definition) an auto-sized element should use the shape as a constraint in determining its maximum size.

8.3. The shape-image-threshold Property

shape-image-threshold

In all current engines.

Firefox62+Safari10.1+Chrome37+
Opera24+Edge79+
Edge (Legacy)NoneIENone
Firefox for Android62+iOS Safari10.3+Chrome for Android37+Android WebView37+Samsung Internet3.0+Opera Mobile24+

Add the final level 1 section with the change that it applies to both shape-inside and shape-outside.

8.4. The shape-image-source-type Property

Should we add an alpha/luminance switch to determine which values we use from the shape-image source? This could just be a keyword on the shape-image-threshold property. Whatever we go with should be compatible with the alpha/luminance switch from mask sources.

8.5. The shape-margin property

shape-margin

In all current engines.

Firefox62+Safari10.1+Chrome37+
Opera24+Edge79+
Edge (Legacy)NoneIENone
Firefox for Android62+iOS Safari10.3+Chrome for Android37+Android WebView37+Samsung Internet3.0+Opera Mobile24+

Add the final level 1 section with the change that it applies to exclusions.

8.6. The shape-padding Property

The shape-padding property adds padding to a shape-inside. This defines a new shape where every point is the specified distance from the shape-inside. This property takes on positive values only.

Name: shape-padding
Value: <length>
Initial: none
Applies to: block-level elements
Inherited: no
Percentages: n/a
Computed value: the absolute length
Canonical order: per grammar
Animation type: by computed value
Example of a shape-padding offset
A shape-padding creating an offset from a circular shape-inside. The light blue rectangles represent inline content affected by the shape created by the padding.

Note: The shape-padding property only affects layout of content inside the element it applies to while the shape-margin property only affects layout of content outside the element.

Conformance

Document conventions

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.

Advisements are normative sections styled to evoke special attention and are set apart from other normative text with <strong class="advisement">, like this: UAs MUST provide an accessible alternative.

Tests

Tests relating to the content of this specification may be documented in “Tests” blocks like this one. Any such block is non-normative.


Conformance classes

Conformance to this specification is defined for three conformance classes:

style sheet
A CSS style sheet.
renderer
A UA that interprets the semantics of a style sheet and renders documents that use them.
authoring tool
A UA that writes a style sheet.

A style sheet is conformant to this specification if all of its statements that use syntax defined in this module are valid according to the generic CSS grammar and the individual grammars of each feature defined in this module.

A renderer is conformant to this specification if, in addition to interpreting the style sheet as defined by the appropriate specifications, it supports all the features defined by this specification by parsing them correctly and rendering the document accordingly. However, the inability of a UA to correctly render a document due to limitations of the device does not make the UA non-conformant. (For example, a UA is not required to render color on a monochrome monitor.)

An authoring tool is conformant to this specification if it writes style sheets that are syntactically correct according to the generic CSS grammar and the individual grammars of each feature in this module, and meet all other conformance requirements of style sheets as described in this module.

Partial implementations

So that authors can exploit the forward-compatible parsing rules to assign fallback values, CSS renderers must treat as invalid (and ignore as appropriate) any at-rules, properties, property values, keywords, and other syntactic constructs for which they have no usable level of support. In particular, user agents must not selectively ignore unsupported component values and honor supported values in a single multi-value property declaration: if any value is considered invalid (as unsupported values must be), CSS requires that the entire declaration be ignored.

Implementations of Unstable and Proprietary Features

To avoid clashes with future stable CSS features, the CSSWG recommends following best practices for the implementation of unstable features and proprietary extensions to CSS.

Non-experimental implementations

Once a specification reaches the Candidate Recommendation stage, non-experimental implementations are possible, and implementors should release an unprefixed implementation of any CR-level feature they can demonstrate to be correctly implemented according to spec.

To establish and maintain the interoperability of CSS across implementations, the CSS Working Group requests that non-experimental CSS renderers submit an implementation report (and, if necessary, the testcases used for that implementation report) to the W3C before releasing an unprefixed implementation of any CSS features. Testcases submitted to W3C are subject to review and correction by the CSS Working Group.

Further information on submitting testcases and implementation reports can be found from on the CSS Working Group’s website at http://www.w3.org/Style/CSS/Test/. Questions should be directed to the public-css-testsuite@w3.org mailing list.

Index

Terms defined by this specification

Terms defined by reference

References

Normative References

[CSS-BOX-4]
Elika Etemad. CSS Box Model Module Level 4. 21 April 2020. WD. URL: https://www.w3.org/TR/css-box-4/
[CSS-DISPLAY-3]
Tab Atkins Jr.; Elika Etemad. CSS Display Module Level 3. 18 December 2020. CR. URL: https://www.w3.org/TR/css-display-3/
[CSS-IMAGES-3]
Tab Atkins Jr.; Elika Etemad; Lea Verou. CSS Images Module Level 3. 17 December 2020. CR. URL: https://www.w3.org/TR/css-images-3/
[CSS-SHAPES]
Vincent Hardy; Rossen Atanassov; Alan Stearns. CSS Shapes Module Level 1. 20 March 2014. CR. URL: https://www.w3.org/TR/css-shapes-1/
[CSS-TRANSFORMS-1]
Simon Fraser; et al. CSS Transforms Module Level 1. 14 February 2019. CR. URL: https://www.w3.org/TR/css-transforms-1/
[CSS-VALUES-3]
Tab Atkins Jr.; Elika Etemad. CSS Values and Units Module Level 3. 6 June 2019. CR. URL: https://www.w3.org/TR/css-values-3/
[CSS-VALUES-4]
Tab Atkins Jr.; Elika Etemad. CSS Values and Units Module Level 4. 11 November 2020. WD. URL: https://www.w3.org/TR/css-values-4/
[CSS-VARIABLES-1]
Tab Atkins Jr.. CSS Custom Properties for Cascading Variables Module Level 1. 3 December 2015. CR. URL: https://www.w3.org/TR/css-variables-1/
[CSS2]
Bert Bos; et al. Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification. 7 June 2011. REC. URL: https://www.w3.org/TR/CSS21/
[CSS3-EXCLUSIONS]
Rossen Atanassov; Vincent Hardy; Alan Stearns. CSS Exclusions Module Level 1. 15 January 2015. WD. URL: https://www.w3.org/TR/css3-exclusions/
[RFC2119]
S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. March 1997. Best Current Practice. URL: https://tools.ietf.org/html/rfc2119

Informative References

[SVG2]
Amelia Bellamy-Royds; et al. Scalable Vector Graphics (SVG) 2. 4 October 2018. CR. URL: https://www.w3.org/TR/SVG2/

Property Index

Name Value Initial Applies to Inh. %ages Anim­ation type Canonical order Com­puted value
shape-inside auto | outside-shape | [ <basic-shape> || shape-box ] | <image> | display auto block-level elements no n/a as defined for <basic-shape>, otherwise discrete per grammar computed lengths for <basic-shape>, the absolute URL for <url>, otherwise as specified
shape-padding <length> none block-level elements no n/a by computed value per grammar the absolute length

Issues Index

One suggestion is to define a shape based on an element’s rendered content. This could have security implications.
Another suggestion is to add something to an image() function that determines the relevant pixels to use (both for defining a shape and for display).
improve the illustration above, using text to show overflow instead of grey boxes.