Skip to the main content

Article Layout with Sticky Contents

Free Layout

A two-column article page whose contents list stays in view beside the text on wide screens and folds into a details block on phones.

Checked on 3 December 2026 in Chrome 143.0.7499.169: 12 automated checks. What the checks are

Preview and code

HTML

<div class="toc-page">
  <header class="toc-page__hero">
    <div class="toc-page__head">
      <p class="toc-page__kicker">Example article</p>
      <h1 class="toc-page__title">Notes on page layout</h1>
      <p class="toc-page__lede">A short read with contents that stay in view beside the text on wide screens and fold away on phones.</p>
    </div>
  </header>

  <div class="toc-page__grid">
    <aside class="toc-page__side">
      <nav class="toc toc--side" aria-label="On this page">
        <p class="toc__label">On this page</p>
        <ol class="toc__list">
          <li><a href="#toc-start">Start with the content</a></li>
          <li><a href="#toc-measure">Pick a line length</a></li>
          <li><a href="#toc-sticky">Keep contents in view</a></li>
          <li><a href="#toc-margin">Leave room for the anchor</a></li>
          <li><a href="#toc-check">Check it on a phone</a></li>
        </ol>
      </nav>
      <details class="toc toc--fold">
        <summary class="toc__summary">On this page</summary>
        <nav aria-label="On this page, folded">
          <ol class="toc__list">
            <li><a href="#toc-start">Start with the content</a></li>
            <li><a href="#toc-measure">Pick a line length</a></li>
            <li><a href="#toc-sticky">Keep contents in view</a></li>
            <li><a href="#toc-margin">Leave room for the anchor</a></li>
            <li><a href="#toc-check">Check it on a phone</a></li>
          </ol>
        </nav>
      </details>
    </aside>

    <article class="toc-page__article">
      <section class="toc-page__section" aria-labelledby="toc-start">
        <h2 id="toc-start">Start with the content</h2>
        <p>A page is easier to lay out once you know what it has to say. Write the headings first, then the paragraphs under them, and only then decide where the columns go.</p>
        <p>Most long pages need two things at once: a comfortable column of text and a way to jump about. The rest of this note builds both with nothing but HTML and CSS.</p>
      </section>
      <section class="toc-page__section" aria-labelledby="toc-measure">
        <h2 id="toc-measure">Pick a line length</h2>
        <p>Lines of roughly sixty to seventy characters are easy to follow. Setting the width of the article in <code>ch</code> units ties the column to the type, so it stays right when the font size changes.</p>
        <p>The contents column can then take whatever space is left over, and it should never squeeze the text below its measure.</p>
      </section>
      <section class="toc-page__section" aria-labelledby="toc-sticky">
        <h2 id="toc-sticky">Keep contents in view</h2>
        <p>With <code>position: sticky</code> and a <code>top</code> offset, the contents list scrolls with the page until it reaches the edge, then stays put. It needs no script, and it keeps its place in the reading order.</p>
        <p>The list sits in a column of its own that is as tall as the article. That tall parent is what gives the sticky box room to travel.</p>
      </section>
      <section class="toc-page__section" aria-labelledby="toc-margin">
        <h2 id="toc-margin">Leave room for the anchor</h2>
        <p>When a link jumps to a heading, the heading lands at the very top. A <code>scroll-margin-top</code> on each section keeps it clear of any bar above it and gives the eye a moment to settle.</p>
        <p>The same rule helps people who use the keyboard: after a jump, the heading is fully on screen.</p>
      </section>
      <section class="toc-page__section" aria-labelledby="toc-check">
        <h2 id="toc-check">Check it on a phone</h2>
        <p>On a narrow screen there is no room for two columns, so the list becomes a folded block above the text. Open it, pick a heading and read on. Nothing is hidden for good.</p>
        <p>Resize the preview to see both layouts.</p>
      </section>
    </article>
  </div>
</div>

CSS

.toc-page {
  --toc-bg: #ffffff;
  --toc-tint: #f3f4fb;
  --toc-ink: #1c2033;
  --toc-muted: #4b5266;
  --toc-line: #d5d9e6;
  --toc-accent: #4338ca;
  --toc-focus: #1c2033;
  box-sizing: border-box;
  min-height: 100vh;
  padding: 0 0 3rem;
  color: var(--toc-ink);
  background: var(--toc-bg);
}
.toc-page *, .toc-page *::before, .toc-page *::after { box-sizing: inherit; }
.toc-page__hero { padding: 2.25rem 1rem 2rem; margin-bottom: 2rem; background: var(--toc-tint); border-bottom: 1px solid var(--toc-line); }
.toc-page__head, .toc-page__grid { max-width: 56rem; margin-inline: auto; }
.toc-page__grid { padding-inline: 1rem; }
.toc-page__kicker { margin: 0 0 0.35rem; color: var(--toc-accent); font-size: 0.8rem; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; }
.toc-page__title { margin: 0 0 0.5rem; font-size: clamp(1.75rem, 1.3rem + 2vw, 2.6rem); line-height: 1.15; letter-spacing: -0.015em; }
.toc-page__lede { margin: 0; max-width: 42rem; color: var(--toc-muted); font-size: 1.05rem; }

.toc-page__grid { display: block; }
.toc-page__article { max-width: 65ch; }
.toc-page__section { margin-bottom: 2rem; scroll-margin-top: 1rem; }
.toc-page__section h2 { margin: 0 0 0.6rem; font-size: 1.35rem; line-height: 1.25; scroll-margin-top: 1rem; }
.toc-page__section p { margin: 0 0 1rem; line-height: 1.65; }
.toc-page__section code { padding: 0.1em 0.35em; font-size: 0.9em; background: var(--toc-tint); border: 1px solid var(--toc-line); border-radius: 5px; }

/* Contents */
.toc__label, .toc__summary { margin: 0 0 0.6rem; color: var(--toc-muted); font-size: 0.78rem; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; }
.toc__list { margin: 0; padding: 0; list-style: none; border-left: 2px solid var(--toc-line); }
.toc__list a {
  display: block; padding: 0.4rem 0.75rem; margin-left: -2px;
  color: var(--toc-ink); font-size: 0.92rem; text-decoration: none; border-left: 2px solid transparent;
}
.toc__list a:hover { color: var(--toc-accent); border-left-color: var(--toc-accent); background: var(--toc-tint); }
.toc__list a:focus-visible { outline: 3px solid var(--toc-focus); outline-offset: -3px; }

/* Phones: a folded block above the article */
.toc--side { display: none; }
.toc--fold { margin-bottom: 1.5rem; background: var(--toc-tint); border: 1px solid var(--toc-line); border-radius: 12px; }
.toc__summary { display: flex; align-items: center; min-height: 2.75rem; margin: 0; padding: 0 1rem; cursor: pointer; list-style: none; }
.toc__summary::-webkit-details-marker { display: none; }
.toc__summary::after { content: ""; width: 0.55rem; height: 0.55rem; margin-left: auto; border-right: 2px solid currentColor; border-bottom: 2px solid currentColor; transform: rotate(45deg); }
.toc--fold[open] .toc__summary::after { transform: rotate(-135deg); }
.toc__summary:focus-visible { outline: 3px solid var(--toc-focus); outline-offset: -3px; border-radius: 12px; }
.toc--fold nav { padding: 0 1rem 0.9rem; }

/* Wide screens: two columns, contents stay in view */
@media (min-width: 44rem) {
  .toc-page { padding-bottom: 4rem; }
  .toc-page__hero { padding: 3rem 1.5rem 2.5rem; }
  .toc-page__grid { padding-inline: 1.5rem; }
  .toc-page__grid { display: grid; grid-template-columns: 13.5rem minmax(0, 1fr); gap: 2.5rem; align-items: start; }
  .toc-page__side { position: sticky; top: 1.25rem; }
  .toc--side { display: block; }
  .toc--fold { display: none; }
}
@media (prefers-color-scheme: dark) {
  .toc-page {
    --toc-bg: #171a28; --toc-tint: #232844; --toc-ink: #eceef8; --toc-muted: #b3b9d0;
    --toc-line: #3d4466; --toc-accent: #b4b9ff; --toc-focus: #ffffff;
  }
}

Accessibility notes

Written by the author of the snippet. Each note has a test step or a markup check that reads the page.

  • The contents are a nav landmark with a label; the phone version uses a native details element.
  • Each heading keeps a scroll margin so a jump never hides it.
  • Only one of the two contents lists is shown at a time, so a reader hears it once.
Layout
Free

Site Footer

A site footer with a short brand note, four groups of links that wrap from four columns to two to one, and a bottom bar with a back-to-top link.

  • HTML
  • CSS
  • No library
  • Layout
  • Grid