CSS @top-center At-Rule

The @top-center at-rule generates content in the center of the top margin of a printed page.

It is nested inside @page and is useful for running document titles, page numbers, and other repeated print information.

Syntax

@page {
  margin: 20mm;
  @top-center {
    content: "Website Report";
    font-size: 10pt;
  }
}

The @top-center at-rule must be nested inside @page. Its content declaration supplies text or other generated content for the top-center page-margin box.

How @top-center Works

During print layout, the browser generates a box in the middle of the top page margin. The box can repeat on each printed page without extra HTML markup.

This content is not part of the ordinary screen layout. Inspect it in Print Preview or a generated PDF.

Position on the Page

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

The top margin provides vertical space for the running header. The available width depends on the page size and any neighboring top-left or top-right margin boxes.

Styling the Header

@page {
  margin-top: 25mm;
  @top-center {
    content: "CSS Reference";
    font: bold 10pt Arial, sans-serif;
    color: #175d91;
    border-bottom: 1px solid #175d91;
  }
}

Page-margin boxes support generated content and applicable styling. Borders, fonts, and spacing may vary across print engines, so verify the printed result.

Page Numbers

@page {
  @top-center {
    content: "Page " counter(page);
  }
}

counter(page) supplies the current printed page number. Combine it with literal text to produce a label.

First-Page Overrides

@page {
  @top-center {
    content: "Reference Guide";
  }
}
@page :first {
  @top-center {
    content: none;
  }
}

The :first page selector can suppress or replace a header on the first printed page, leaving it on subsequent pages.

The top edge has @top-left, @top-center, and @top-right positions. These can hold different running information.

@page {
  @top-left { content: "Chapter 1"; }
  @top-center { content: "Reference Guide"; }
  @top-right { content: counter(page); }
}

@top-center Example

Print this document to see the title centered in the top page margin.

HTML

<h1>Print Example</h1>
<p>Open Print Preview to inspect the top margin.</p>

CSS

@page {
  size: A4;
  margin: 25mm 20mm 20mm;
  @top-center {
    content: "Website Report";
    font: bold 10pt Arial, sans-serif;
    color: #175d91;
  }
}

Print Preview: In a supporting browser, the label appears in the top-center margin of each applicable page. A normal Browser Result cannot demonstrate a print-only margin box.

Browser Support

CSS page-margin boxes are available in recent major browsers, but older browsers and some PDF or print engines may omit them. Check Can I Use? for CSS page margin boxes and verify the actual printed output.

Specifications

@top-center is defined in CSS Paged Media Module Level 3.

CSS Paged Media Module Level 3