<div class="wrapper">
    <article class="flow">
      <h1>Direction of grid lines</h1>
      <figure class="callout">
        <p>Change the direction and writing mode to see the layout change.</p>
      <div class="controls">
        <label for="switcher">
          Set writing mode
          <select id="switcher">
            <option value="horizontal-tb">horizontal-tb</option>
            <option value="vertical-rl">vertical-rl</option>
            <option value="vertical-lr">vertical-lr</option>
        <label class="toggle" for="direction">
          <span class="toggle__label">Set direction to right-to-left</span>
          <input type="checkbox" role="switch" class="toggle__element" id="direction">
          <div class="toggle__decor" aria-hidden="true">
            <div class="toggle__thumb"></div>
      <div class="container" id="container">
        <div class="box span-row">Item one</div>
        <div class="box">Item two</div>
        <div class="box span-col">Item three has more text </div>
        <div class="box">Item four</div>
        <div class="box">Item five</div>
:root {
  --var-direction: 'ltr';
  --var-writing-mode: 'horizontal-tb';

.container {
  display: grid;
  direction: var(--var-direction);
  writing-mode: var(--var-writing-mode);
  grid-template-columns: 1fr 1fr 1fr;
  grid-auto-rows: minmax(100px, auto);
  grid-auto-flow: var(--var-packing);
  gap: 10px;
  background-color: var(--color-stroke);
  padding: 1em;
  max-width: 30rem;

.span-row {
  grid-row: auto / span 2;

.span-col {
  grid-column: auto / span 2;

.controls > * + * {
  margin-top: 1rem;
let root = document.documentElement;
let switcher = document.getElementById("switcher");
let direction = document.getElementById("direction");

switcher.addEventListener("change", function (evt) {
  root.style.setProperty('--var-writing-mode', evt.target.value);

direction.addEventListener("click", function (evt) {
  if(direction.checked) {
    root.style.setProperty('--var-direction', 'rtl');
  } else {
    root.style.setProperty('--var-direction', 'ltr');

External CSS

  1. https://codepen.io/web-dot-dev/pen/abpoXGZ.css

External JavaScript

This Pen doesn't use any external JavaScript resources.