Defining rule insets

When creating grid, flexbox, and multi-column layouts using CSS gap properties, you can define and control the gaps between columns and rows and the line decorations, or rules, within those gaps. The ends of the rules can be inset or outset. Rules that cross gutters can also be broken into segments at each gutter intersection. These segments can also be inset or outset. This guide explains how to inset the endpoints of rules and rule segments.

Overview

By default, there are no gaps between grid rows, grid columns, flex rows, or flex items, while there is a 1em wide gap between columns in multi-column layouts. With the gap shorthand and its constituent properties, you can control the spacing between adjacent columns and rows (and flex items) within a given layout context. See defining CSS gaps to learn about defining gaps in grid, flexbox, and multi-column layouts.

By default, only whitespace is added between rows and columns. You can add a rule within the gaps using the rule shorthand and its constituent properties, defining the rule's color, width, and style. The rule must have a visible style to be rendered. See Defining CSS gap rules to learn more about defining CSS gap rules.

By default, in grid and multi-column layouts, row and column rules span the full width of the row or the full height of the column, respectively. In flexbox, the rules between flex lines span the entire container. By default, these rules start and end at the container's content box edge. CSS gap module *-rule-inset-cap-* properties enable insetting or outsetting the start and end of these unbroken rules.

Rules between flex items in multi-line flex containers don't span the entire container. Rather, the rule segments between flex items span the cross-dimension of the flex line. When both horizontal and vertical rules are set and they intersect, the rules are broken into segments. Rule segments can also be inset or outset at one or both edges. The CSS gap module *-rule-inset-junction-* properties enable insetting or outsetting the start and ends of these rule segments at interior junctions where a segment meets or intersects another segment, while the *-rule-inset-cap-* properties are used to inset the edges that abut the container's edge, and those at intersections where no other rule segments meet or intersect the segment.

Splitting rules into segments

The *-rule-break properties enable splitting lines that span across multiple rows or columns into multiple segments. You can set the behavior for breaking rule lines at every intersection using the following properties:

  • column-rule-break: The keywords none, normal, or intersection specify whether the column rules break across row gaps or run continuously.
  • row-rule-break: Specifies whether the row rules break across column gaps or run continuously; accepts the same values as column-rule-break.
  • rule-break: Sets column-rule-break and row-rule-break to the same value.

In this demonstration, you can split the row or column rules (or both) into separate segments at column and row gap intersections, or keep the rules continuous.

css
.grid {
  column-rule-break: none;
  row-rule-break: intersection;
}

Try changing the column-rule-break and row-rule-break properties. The boxes in the demo are laid out using CSS Grid. In this case, the default normal keyword behaves as none, which causes the gap rules to overlap. Setting intersection breaks the lines into segments.

The normal keyword also behaves as none when applied to gaps in flex containers. However, it behaves as intersection in multi-column layouts.

The ends of each rule or rule segment can be inset or outset. The rest of this guide describes the different end edges and how to control them.

Rule segment edges

When a segment abuts a gap, by default the segment ends at the edge of that gap without crossing into it. Segments that abut the container's edge are drawn up to the edge of the container's content box. You can inset or outset the edges of every rule segment.

Junction and cap edges

There are two different segment endpoint types: junction and cap. When a rule breaks into segments, the endpoint type depends on whether it meets other rule segments at the gap edge it ends at. Rules intersect at interior junctions.

Segment endpoints at gap junctions where other segments are present are junction endpoints.

Change the value of the rule-inset-junction shorthand property to visualize the segment junction edges:

Segment endpoints that are not junction endpoints, including endpoints at the container's inline and block edges and at intersections where the rule-visibility-items property has prevented additional segments from being painted, are cap endpoints. If a rule spans the entire width or height of the container and isn't broken, both endpoints are cap endpoints.

An endpoint located at the container's edge is always a cap endpoint. All endpoints at interior gap junctions are junction endpoints unless no other segments are present at that junction, which only occurs if rules are not drawn due to setting the rule-visibility-items shorthand or component properties.

Change the value of the rule-inset-cap shorthand property to visualize the segment cap endpoints:

Start and end edges

Each rule or rule segment has two edges: a start and an end.

For column rules and column rule segments, the start of the segment is at the top edge, and the end is at the bottom. For continuous column rules, a cap-start endpoint is at the top edge of the container, and a cap-end endpoint is at the bottom. When a column rule segment's top or bottom endpoint is at an interior junction where other rule segments are present, those endpoints are junction-start and junction-end endpoints, respectively. If no other rule is present at the interior junction, it is a cap-start or cap-end endpoint, not a junction endpoint.

For row rules and row rule segments, whether the start endpoint is at the left or right edge depends on the writing mode. In left-to-right languages, like English, the start is the left edge and the end is the right edge. In right-to-left languages, like Arabic and Hebrew, the start is the right edge, and the end is the left edge. When a row rule segment starts or ends at an interior junction with other rule segments, those endpoints are cap-start and cap-end, respectively. Column rule segment endpoints at interior junctions with no other rule segments are either cap-start or cap-end.

Change the value of the rule-inset-start shorthand property to visualize the segment start endpoints:

Change the value of the rule-inset-end shorthand property to visualize the segment end endpoints:

You'll never find a junction endpoint abutting the container's start or end edge; those are always cap endpoints.

Insetting segment edges

By default, continuous rules are drawn from one end of the container to the other, and rule segments are drawn from gap edge to gap edge or container edge. There are individual *-rule-inset-* CSS properties, such as row-rule-inset-cap-end, to control the insetting of both -start and -end, -cap and -junction endpoints for both column- and row- rules (as well as several shorthands for combinations of these eight separate properties).

The rule-inset longhand properties

The longhand properties are written as [ column | row ]-rule-inset-[ cap | junction ]-[start | end ].

The column-rule-inset-* properties, including column-rule-inset-cap-end, column-rule-inset-cap-start, column-rule-inset-junction-end, and column-rule-inset-junction-start, enable insetting column segment cap or junction endpoints, with each longhand property controlling either the segments' starts or ends.

Similarly, the row-rule-inset-* properties, including row-rule-inset-cap-end, row-rule-inset-cap-start, row-rule-inset-junction-end, and row-rule-inset-junction-start, enable insetting row segment cap or junction endpoints, controlling the segment starts or ends.

Each longhand property takes one <inset-value> as its value, either a <length-percentage> or the overlap-join keyword. Length values behave the same for cap and junction endpoints, while percentage values and the overlap-join keyword will inset junction endpoints but resolve to 0 for cap endpoints.

Length inset values

Length values inset segments by the specified value. Positive <length> values reduce the segment length by the specified value. Negative values create an outset, extending junction segments into the gap adjacent to the segment endpoint and extending cap segments at the container edge to a point outside of the element's content box.

Percentage inset values

The effect of percentage values depends on whether the segment endpoint abuts the container edge or a gap. At the container's edge, percentage values are relative to 0, so percentage values never cause cap segment endpoints to extend outside the container content box.

For interior segment endpoints, percentages are relative to the gap at that edge, so relative to the size of the row-gap for interior column segment endpoints, and relative to the size of the column-gap for interior row segment endpoints.

Setting column-rule-inset-end: -50% offsets the bottom of the junction segment halfway through the row gap below the segment, no matter how wide the row gap is.

The overlap-join keyword

If row and column rules don't break, they overlap at junction intersections. Broken rules with junction endpoints set to overlap-join also overlap if a perpendicular segment is present to meet the segment endpoint at the interior junction.

The overlap-join keyword value offsets the end of interior segments, so they align with the far edge of the rule perpendicular to the rule segment. The value resolves to half the gap size (extending it to the middle of the gap) plus half the rule width.

For example, when column-rule-inset-start is set to the overlap-join keyword value, the start endpoints of the junction segments extend up into the row gap to meet, or "join", the top edge of the row rule painted in that gap.

When applied to a cap end, the overlap-join keyword value resolves to 0. This is expected at the container's edge. It is useful for interior cap segments, as there is no perpendicular rule at the interior junction for the segment to overlap.

In this demonstration, try setting the rule-inset-start shorthand property to overlap-join to see the effect.

Shorthand properties

In addition to the eight longhand properties, there are 15 shorthand properties.

Defining a value for start or end, row or column, cap and junction endpoints

There are shorthand properties that accept a single <inset-value>, setting both the cap and junction insets of either the starts or ends of column or row segments to the same value:

  • column-rule-inset-end: Insets both the column-rule-inset-cap-end and column-rule-inset-junction-end to the same value, defining the bottom insets of all column segments.

  • column-rule-inset-start: Insets both the column-rule-inset-cap-start and column-rule-inset-junction-start to the same value, defining the top insets of all column segments.

  • row-rule-inset-end: Insets the end inset of all row segments, setting both the row-rule-inset-cap-end and row-rule-inset-junction-end to the same value.

  • row-rule-inset-start: Insets the starts of all row segments, setting both the row-rule-inset-cap-start and row-rule-inset-junction-start to the same value.

Defining a value for cap or junction, row or column, start and end endpoints

You can use the following shorthands to inset the start and end endpoints of a particular type, in a row or column. The start and end endpoints can be set to the same value or two different values:

  • column-rule-inset-cap: Specifies both the column-rule-inset-cap-start and column-rule-inset-cap-end properties. If you specify only one value, both properties are set to that value. If you specify two values, the first sets column-rule-inset-cap-start and the second sets column-rule-inset-cap-end.

  • column-rule-inset-junction: Specifies both the column-rule-inset-junction-start and column-rule-inset-junction-end properties. If you specify only one value, both properties are set to that value. If you specify two values, the first sets column-rule-inset-junction-start and the second sets column-rule-inset-junction-end.

  • row-rule-inset-cap: Specifies both the row-rule-inset-cap-start and row-rule-inset-cap-end values. If you specify only one value, both properties are set to that value. If you specify two values, row-rule-inset-cap-start is set to the first value and row-rule-inset-cap-end to the second.

  • row-rule-inset-junction: Specifies both the row-rule-inset-junction-start and row-rule-inset-junction-end values. If you specify only one value, both properties are set to that value. If you specify two values, the first specifies row-rule-inset-junction-start and the second specifies row-rule-inset-junction-end.

Column and row shorthands

You can use the following shorthands to set two of the previously discussed shorthands at the same time — either insetting all endpoints of a particular type, or insetting all start or end endpoints. These shorthands can also take one or two values:

  • rule-inset-cap: Sets column-rule-inset-cap and row-rule-inset-cap to the same value.

  • rule-inset-junction: Sets column-rule-inset-junction and row-rule-inset-junction to the same value.

  • rule-inset-end: Sets column-rule-inset-end and row-rule-inset-end to the same value.

  • rule-inset-start: Sets column-rule-inset-start and row-rule-inset-start to the same value.

The rule-inset shorthands

There are three shorthand properties that inset all endpoints for column segments, row segments, or both. These properties offer more control than the end, start, cap, or junction shorthands, letting you set four endpoint types to different values.

  • row-rule-inset: Specifies the row-rule-inset-cap-start, row-rule-inset-cap-end, row-rule-inset-junction-start, and row-rule-inset-junction-end properties as one to four <inset-value> values. If you set the cap insets to different values from the junction insets, separate them with a slash (/).

  • column-rule-inset: Specifies the column-rule-inset-cap-start, column-rule-inset-cap-end, column-rule-inset-junction-start, and column-rule-inset-junction-end properties as one to four <inset-value> values. If you set the cap insets to different values from the junction insets, separate them with a slash (/).

  • rule-inset: Sets the column-rule-inset and row-rule-inset properties to the same values. Specifies the rule-inset-cap-start, rule-inset-cap-end, rule-inset-junction-start, and rule-inset-junction-end properties as one to four <inset-value> values. If you set the cap insets to different values from the junction insets, separate them with a slash (/).

Considering there are 23 values, it is surprising that there is no shorthand to set eight different values.

See also