In this example, we demonstrate the effects of different reading-flow values on a grid container.
HTML
The markup includes a <select> element for selecting different reading-flow values, and a wrapper <div> containing four <a> elements.
<form>
<label for="reading-flow">Choose reading flow:</label>
<select id="reading-flow">
<option>normal</option>
<option>grid-rows</option>
<option>grid-columns</option>
<option>grid-order</option>
</select>
</form>
<div class="wrapper">
<a class="a" href="#">Item A</a>
<a class="b" href="#">Item B</a>
<a class="c" href="#">Item C</a>
<a class="d" href="#">Item D</a>
</div>
CSS
We use a display value of grid to turn the <div> into a grid container, and display the grid items in three columns using grid-template-columns. We also set grid-template-areas to describe different placement areas in those columns, and place the <a> elements in those areas using grid-area. Initially, we set a reading-flow of normal so the items are read or tabbed through in the default DOM source order.
Finally, we set an order value of 1 on the first <a> element; this has no effect on the layout because it does not override the grid area placement, but it does have an effect when a certain reading-flow value is set, as you'll se later on.
Reading from left to right, the resulting display order of the grid items is "Item D", "Item B", "Item C", then "Item A".
.wrapper {
display: grid;
grid-template-columns: repeat(3, 150px);
grid-template-areas:
"d b b"
"c c a";
reading-flow: normal;
}
.a {
grid-area: a;
}
.b {
grid-area: b;
}
.c {
grid-area: c;
}
.d {
grid-area: d;
}
a:first-child {
order: 1;
}
JavaScript
In our script, we grab references to the <select> element and wrapper <div>, then add a change event listener to the <select> element. When a new value is selected, it is set as a reading-flow property value on the wrapper.
const selectElem = document.getElementById("reading-flow");
const wrapperElem = document.querySelector(".wrapper");
selectElem.addEventListener("change", () => {
wrapperElem.style.readingFlow = selectElem.value;
});
Result
The demo renders like this:
First, try tabbing through the links with reading-flow: normal set. The tabbing order is "Item A", "Item B", "Item C", and "Item D", as that is the order of the elements in the DOM.
Now change the reading-flow value and then try tabbing through the links again:
- A value of
grid-rows causes the items to be tabbed through in the visual display order by row. This is "Item D", "Item B", "Item C", then "Item A". - A value of
grid-columns causes the items to be tabbed through in the visual display order by column. This is "Item D", "Item C", "Item B", then "Item A". - A value of
grid-order causes the items to be tabbed through in DOM order, but takes into account any order value changes. Since we have order: 1; set on the first <a> element, the tabbing order is "Item B", "Item C", "Item D", then "Item A".