Skip to content

[css-cascade] non-destructive shorthands #1461

@jonathantneal

Description

@jonathantneal

In Cascading and Inheritance: Shorthand Properties, we learn that shorthands allow authors to specify the values of several properties with a single property. This is very useful for a few reasons, but I find it most useful because it keep thoughts grouped together. This advantage has a profound catch; omitted values in shorthands are assigned their initial value.

So, to remain non-destructive looks something like this:

.non-destructive-block-margin {
  margin-top: 10px;
  margin-bottom: 30px;
}

.non-destructive-inline-margin {
  margin-left: auto;
  margin-right: auto;
}

.non-destructive-margin-variation {
  margin-top: 0;
  margin-left: auto;
  margin-right: auto;
}

With these separated properties, the “margin” thought is no longer grouped together. So, is there a way we could make non-destructive shorthands? I have 2 suggestions.

Solution 1: Skip tokens

As proposed in #733, a skip token would allow authors to prevent the resetting of specific values in shorthands.

.non-destructive-block-margin {
  margin: 30px *;
}

.non-destructive-inline-margin {
  margin: * auto;
}

In order for this to actually work, these skip tokens would be limited to ordered shorthands. However, there may be another way around that:

Suggestion 2: Identifiers

In Grid Layout Module: Named Grid Lines, we learn about named lines which allows authors to easily identify the grid lines being styled. Identifiers could be used by shorthands to limit the reach of overrides.

.non-destructive-block-margin {
  margin: [block] 10px 30px;
}

.non-destructive-inline-margin {
  margin: [inline] auto;
}

.non-destructive-margin-variation {
  margin: [block-start] 0 [inline] auto;
}

An advantage of this syntax is that it would not be limited to order shorthands.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions