CSS @right-middle At-Rule

The @right-middle at-rule defines generated content in the middle of the right margin of a printed page.

It is a page-margin at-rule nested inside @page. Authors can use it for running labels, section names, or other information that repeats in printed documents. Its output is visible in supported print rendering, not the normal webpage view.

@right-middle Syntax

@page {
  margin: 25mm;
  @right-middle {
    content: "Reference";
  }
}

The @right-middle block must be placed inside an @page rule. Its content declaration supplies text or other generated content for the right-middle page-margin box.

How @right-middle Works

When a browser lays out a document for printing, the @page rule describes the page box and its margins. The @right-middle rule generates a margin box in the middle position along the page's right edge.

Unlike an absolutely positioned HTML element, this margin box belongs to each printed page. It can appear on multiple pages without adding repeated markup to the document.

Placement and Space

The right-middle margin box occupies the middle portion of the right page margin, between the right-top and right-bottom positions. Its available dimensions depend on the page size, the page margins, and the layout of neighboring margin boxes.

@page {
  size: A4;
  margin: 20mm 30mm 20mm 20mm;
  @right-middle {
    content: "Chapter 2";
  }
}

The larger right margin leaves more room for the label. A margin box does not automatically rotate its text; use an appropriate writing mode when a vertical label is needed.

Styling Margin Content

Page-margin boxes support generated content and applicable CSS styling, including fonts, colors, borders, and backgrounds. Results depend on the browser's print implementation.

@page {
  margin-right: 30mm;
  @right-middle {
    content: "Reference Guide";
    font: bold 10pt Arial, sans-serif;
    color: #175d91;
    writing-mode: vertical-rl;
    text-align: center;
  }
}

The writing-mode: vertical-rl declaration requests vertically arranged text. Test the result in Print Preview because support for individual properties on page-margin boxes can vary.

The right edge has three named margin boxes: @right-top, @right-middle, and @right-bottom. They occupy distinct positions and can be used together.

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

The counter(page) function supplies the current printed page number. If several boxes contain long text, their available space may affect the layout.

Printed Output

Page-margin content is not displayed in the ordinary browser viewport. To inspect it, open the browser's Print Preview or print the document to PDF in a browser that supports CSS page-margin boxes.

Browser print settings, such as paper size, scaling, and margins, may affect the output. The browser's built-in headers and footers are separate from CSS-generated page-margin content.

@right-middle Example

This example adds a vertical label to the right margin and a page number to the bottom-right margin of each printed page.

HTML

<h1>CSS Reference Guide</h1>
<p>Open Print Preview to inspect the right page margin.</p>
<p>Add enough content to create multiple printed pages.</p>

CSS

@page {
  size: A4;
  margin: 20mm 30mm 20mm 20mm;
  @right-middle {
    content: "CSS Reference";
    writing-mode: vertical-rl;
    font: bold 10pt 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 middle of the right page margin and the page number appears near the bottom-right corner. This is a print-only demonstration, so a normal Browser Result block would not show the feature.

Browser Support

CSS page-margin boxes are supported in current versions of several major browsers, but older browsers and some print engines may not render them. Individual styling properties may also behave differently in printed output.

Check the current compatibility information at Can I Use? by searching for CSS page margin boxes, and test the actual printed or PDF result.

Specifications

The @right-middle at-rule is defined in CSS Paged Media Module Level 3 as one of the page-margin at-rules used inside @page.

CSS Paged Media Module Level 3