CSS @top-left At-Rule

The @top-left at-rule defines generated content in the top-left margin box along the top edge of a printed page.

It is a page-margin at-rule nested inside @page. Authors can use it for running document headers, page numbers, or short labels that repeat across printed pages. Its content appears in print layout, not in the normal browser viewport.

@top-left Syntax

@page {
  margin: 20mm;
  @top-left {
    content: "CSS Reference";
  }
}

Place @top-left inside an @page rule. The content property supplies the generated text, counter, or other supported content for that margin box. The at-rule cannot be used as an ordinary top-level style rule.

How @top-left Works

During printing, the browser creates page boxes with a content area and surrounding margins. The @top-left rule describes a box in the left portion of the top page margin. Its generated content can repeat on every applicable printed page without adding repeated HTML elements.

Page-margin boxes are separate from the browser’s own print headers and footers. If browser-provided headers are enabled, they may compete for space or visually overlap with CSS-generated content.

Page-Margin Placement

The box occupies the left portion of the top page margin. The three top-edge boxes share the available top margin width; long labels may need more space or shorter text. Increasing the top margin makes more vertical room for the header.

@page {
  size: A4;
  margin: 25mm 20mm 20mm;
  @top-left {
    content: "CSS Reference";
  }
}

The size declaration sets the intended paper size, while margin reserves space around the page content. The final result also depends on printer settings and the browser’s print engine.

Styling the Margin Box

Page-margin boxes support generated content and applicable CSS properties such as font, color, background, border, and alignment. Choose a legible font size and sufficient contrast for print.

@page {
  margin: 25mm;
  @top-left {
    content: "CSS Reference";
    font: 9pt Arial, sans-serif;
    color: #175d91;
    text-align: left;
    vertical-align: middle;
  }
}

Some properties may render differently in different print engines. Always inspect the generated PDF or printed sheet rather than assuming the screen view reflects the output.

Page Selectors

Use page pseudo-classes such as :first, :left, and :right to vary the margin content on selected printed pages.

@page {
  @top-left {
    content: "CSS Reference";
  }
}
@page :first {
  @top-left {
    content: "";
  }
}

In this example, the default label is omitted from the first page. Whether left- and right-hand pages are used depends on the document’s pagination and page progression.

The related @top-center and @top-right boxes occupy other positions around the page. You can combine several margin boxes in the same @page rule to build a print header or footer.

@page {
  margin: 25mm;
  @top-left {
    content: "CSS Guide";
  }
  @bottom-right {
    content: counter(page);
  }
}

The counter(page) expression generates the current printed page number. To show the total page count, check support for counter(pages) in the target print engine.

@top-left Example

This example displays a short label in the top-left margin box along the top edge of a printed page and a page number in the bottom-right margin. Use Print Preview to inspect the result.

HTML

<h1>CSS Print Guide</h1>
<p>Open Print Preview to see the generated margin content.</p>
<p>Add enough text to create multiple pages.</p>

CSS

@page {
  size: A4;
  margin: 25mm;
  @top-left {
    content: "CSS Print Guide";
    font: bold 9pt Arial, sans-serif;
    color: #175d91;
  }
  @bottom-right {
    content: counter(page);
    font: 9pt Arial, sans-serif;
  }
}

Print Preview: In a supporting browser, the label appears in the top-left margin box along the top edge of a printed page. The page number appears at the bottom-right. Because this feature only appears during pagination, an ordinary on-screen Browser Result would not demonstrate it accurately.

Browser Support

CSS page-margin boxes are supported in current versions of several major browsers, but older browsers and some print engines may ignore them. Support for particular styling properties may also vary.

For current compatibility, visit Can I Use? and search for CSS page margin boxes. Test the actual print or PDF output in the browsers you intend to support.

Specifications

The @top-left page-margin at-rule is defined in CSS Paged Media Module Level 3.

CSS Paged Media Module Level 3