CSS Step Sizing

Editor’s Draft,

This version:
https://drafts.csswg.org/css-step-sizing/
Feedback:
www-style@w3.org with subject line “[css-step-sizing] … message topic …” (archives)
Issue Tracking:
Inline In Spec
Editor:
(Google)

Abstract

This module contains CSS features for aligning content size to multiple of unit size.

CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, in speech, 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.

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 “css-step-sizing” in the subject, preferably like this: “[css-step-sizing] …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.

This document is governed by the 1 September 2015 W3C Process Document.

1. Introduction

This specification provides features to control sizes of CSS objects according to the rules desired by use cases.

Controlling sizes of CSS objects to be multiple of a unit is desired in many cases. This level of the specification focuses on following cases.

By controlling heights of line boxes, lines of text in different fonts can create consistent visuals to help readability.

Also by stacking such line boxes, authors can align lines across columns, pages, scroll-snapped blocks, or multiple blocks placed absolutely, to produce vertical rhythm.

Vertical rhythm kept through pictures and different size of text in a multi-column document.

Controlling widths of block-level boxes to the multiple of the specified unit gives the ability to control a block so that it can fit mono-space characters without remainders. One of the use cases this feature addresses is better readability of East Asian documents. In Han ideographic-based scripts such as Chinese or Japanese, most characters have 1em advance, and due to that nature, most such documents are justified.

Adjusting widths of block-level boxes to the multiple of 1em helps to minimize cases where justification needs to expand spacing.

2. Adjusting Line Box Heights: the line-height-step property

Name: line-height-step#propdef-line-height-stepReferenced in:2. Adjusting Line Box Heights: the line-height-step property
Value: <length> <integer>?
Initial: 0px
Applies to: block containers
Inherited: yes
Percentages: N/A
Media: visual
Computed value: the absolute length for length, others as specified
Animatable: no

Values have the following meanings:

<length>
This value defines the step unit#line-height-step-step-unitReferenced in:2. Adjusting Line Box Heights: the line-height-step property2.1. Stepping Line Box Heights (2) (3) (4) (5) (6) (7) (8)2.2. Aligning Baselines (2) (3) (4) for line box heights. Non-negative <length>s are valid.
<integer>
This value defines the step baseline position#line-height-step-step-baseline-positionReferenced in:2.1. Stepping Line Box Heights2.2. Aligning Baselines (2) (3) (4) (5) (6) (7) (8) within the step unit. Values between 1 and 100 (inclusive) are valid.

Naming under discussion.

Should the step baseline position be percent? Not much precision is needed, but there was one feedback that percent is easier to use.

2.1. Stepping Line Box Heights

When the step unit is set to a positive <length>, the line box heights are rounded up to the multiple of the unit.

[CSS21] §10.8 Line height calculations defines how to compute the line box height after the line box was constructed from inline-level boxes. The rounding is applied to the computed line box height by assuming that there is an inline-level box that has adjusted A' and D' in the line box.

Rounding up the computed line box height.

The step baseline position determines how the additional spaces are distributed.

In the following example, the height of line box in each paragraph is rounded up to the step unit.

:root {
  font-size: 12pt;
  --my-grid: 18pt;
  line-height-step: var(--my-grid);
}
h1 {
  font-size: 20pt;
  margin-top: calc(2 * var(--my-grid));
}
p {
  margin: 0;
}

The line box in <h1> does not fit into one step unit and thus occupies two, but it is still centered within the two step unit.

Authors can keep margins or other properties to be multiple of step unit using var() and calc() as in the example above.

If author prefers, tools like Sass can make such declarations shorter.

$gu: 18px;

@function gu($n) {
  @return $n * $gu;
}

h1 {
  font-size: 20pt;
  margin: gu(1.2) auto gu(1.8);
}
It is usually recommended to set the line-height lower than the step unit. The used line height can increase due to several factors such as the use of vertical-align or font fallback.

2.2. Aligning Baselines

When the step baseline position is set, the additional spaces are distributed using the following formula:

Given:

This formula pushes the baseline of the line box down to the closest step baseline position, and the bottom to the next step unit.

The following CSS sets the step baseline position to 14pt (20pt × 0.70) within each step unit.

:root {
  line-height-step: 20pt 70;
}

The baseline of the line box is pushed down to the closest step baseline position by adding the space-over.

<h1> is as tall as its baseline being lower than the second step baseline position that it is pushed down to the third step baseline position.

Since the bottom of the line box is rounded to the closest step unit next to the descenders of the line box, large descenders may result in unintended spaces under the line box. Oftentimes changing step baseline position can control such situations.

2.3. Notes on Block-level Boxes

This section is not normative.

This level of the specification does not provide features to adjust heights of block-level boxes.

The following CSS turns <h2> to inline-blocks.
:root {
  line-height-step: 18pt;
}
h2 {
  display: inline-block;
  width: 100%;
  line-height-step: 0;
  line-height: 1.2;
}

When an <h2> is long enough to wrap, text inside the <h2> uses line-height: 1.2, while the height of the <h2> block is rounded up to the multiple of 18pt. See a sample in action.

3. Stepping Widths: the inline-size-step property

Name: inline-size-step#propdef-inline-size-stepReferenced in:3. Stepping Widths: the inline-size-step property
Value: <length>
Initial: 0px
Applies to: all elements but non-replaced inline elements, table rows, and row groups
Inherited: no
Percentages: N/A
Media: visual
Computed value: the absolute length
Animatable: no

Non-negative <length>s are valid.

When this property is set to a positive <length> and the available inline size is definite, the available inline size of the element is rounded down to the closest multiple of the specified <length> before it is used.

Since this property only adjusts available inline size, it does not guarantee that the inline size of child boxes are the multiple of the specified <length> if there were other constraints.

When this property is applied to a multi-column element, the available inline size of the content box is not rounded but the available inline size of the column box is rounded.

When using this property with CSS layout features such as tables or flexbox, authors are expected to apply this property to item boxes rather than container boxes; e.g., table cells rather than tables, or flex-items rather than flex-containers.

However, the special case above is needed because authors cannot style column boxes.

This property improves the justification for Han ideograph-based scripts such as Chinese or Japanese.

With the following CSS, the <article> elements and blocks with bodytext class are justified, but the expansion occurs only when there are non-CJK characters in the line because their logical widths are adjusted to the multiple of 1em.

article, .bodytext {
  font-size: 12pt;
  text-align: justify;
  inline-size-step: 1em;
}

4. Privacy and Security Considerations

This specification introduces no new privacy leaks, or security considerations beyond "implement it correctly".

5. Acknowledgments

This specification would not have been possible without the help from: Takao Baba, Chris Eppstein, Shinyu Murakami, Tsutomu Nanjo, Charlie Neely, Florian Rivoal, Hiroshi Sakakibara, Alan Stearns, and the CSS Working Group members.

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.

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.

Requirements for Responsible Implementation of CSS

The following sections define several conformance requirements for implementing CSS responsibly, in a way that promotes interoperability in the present and future.

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 property 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.

Implementations of CR-level Features

Once a specification reaches the Candidate Recommendation stage, implementers should release an unprefixed implementation of any CR-level feature they can demonstrate to be correctly implemented according to spec, and should avoid exposing a prefixed variant of that feature.

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-SIZING-4]
CSS Intrinsic & Extrinsic Sizing Module Level 4 URL: https://drafts.csswg.org/css-sizing-4/
[CSS-VALUES]
Tab Atkins Jr.; Elika Etemad. CSS Values and Units Module Level 3. 11 June 2015. CR. URL: http://dev.w3.org/csswg/css-values/
[CSS-WRITING-MODES-3]
Elika Etemad; Koji Ishii. CSS Writing Modes Level 3. 15 December 2015. CR. URL: http://dev.w3.org/csswg/css-writing-modes-3/
[CSS21]
Bert Bos; et al. Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification. 7 June 2011. REC. URL: http://www.w3.org/TR/CSS2
[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

[CSS-VARIABLES-1]
Tab Atkins Jr.. CSS Custom Properties for Cascading Variables Module Level 1. 3 December 2015. CR. URL: http://dev.w3.org/csswg/css-variables/

Property Index

Name Value Initial Applies to Inh. %ages Media Ani­mat­able Com­puted value
line-height-step <length> <integer>? 0px block containers yes N/A visual no the absolute length for length, others as specified
inline-size-step <length> 0px all elements but non-replaced inline elements, table rows, and row groups no N/A visual no the absolute length

Issues Index

Naming under discussion.
Should the step baseline position be percent? Not much precision is needed, but there was one feedback that percent is easier to use.