CSS print header and footer page numbers

CSS Printing Header and Footer Page Numbers

CSS Printing Header and Footer Page Numbers

When printing a web page, sometimes we need to display page numbers in the header or footer of the printed page for easier reading and management. In CSS, we can achieve this functionality using specific properties and pseudo-classes. This article will detail how to get page numbers in the header and footer when printing a web page, and provide sample code.

1. Displaying Page Numbers in the Header

To display page numbers in the header, we can use the @page rule and the content attribute. Here is a simple sample code:


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

body {
counter-reset: page;
}

h1 {
page-break-before: always;
}

p {
page-break-inside: avoid;
}

In the example above, we use the @page rule to define styles for displaying page numbers in the header. We use content: "Page " counter(page); to display the page number information. We use counter-reset: page; in the body element to initialize the page number counter. We insert a page break before the h1 element to avoid paragraph breaks.

2. Displaying Page Numbers in the Footer

Similarly, we can also display page number information in the footer. Below is a sample code:

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

In the above example, we use the @page rule to define the style for displaying page numbers in the footer, and use content: "Page " counter(page); to display the page number information.

3. Displaying Different Page Numbers in the Header and Footer

Sometimes we need to display different page number information in the header and footer. This can be achieved using properties such as @top-left, @top-right, @bottom-left, and @bottom-right. Here’s a sample code:

@page { 
@top-left { 
content: "Header Page " counter(header); 
} 
@bottom-right { 
content: "Footer Page " counter(footer); 
} 
} 

body { 
counter-reset: header footer; 
} 

h1 { 
page-break-before: always; 
} 

p { 
page-break-inside: avoid; 
} 

In the above example, we use the @top-left and @bottom-right properties to define different page number information in the header and footer, respectively, through content: "Header Page " counter(header); and content: "Footer Page " counter(footer); to display different page number information.

4. Customizing Page Number Formats

In addition to displaying simple page number information, you can also customize the page number format, such as adding prefixes, suffixes, separators, and more. Here is a sample code:

@page { 
@bottom-center { 
content: "Page " counter(page, upper-roman) " of " counter(pages); 
} 
} 

In the above example, we use counter(page, upper-roman) to display the page number in Roman numerals, and counter(pages) to display the total number of pages.

5. Controlling the Display of Headers and Footers on Printed Pages

Sometimes we need to control the display of headers and footers when printing a page. This can be achieved using the display property. The following is an example code:

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

In the above example, we use display: none; to prevent the header from being displayed on the printed page.

6. Control the Position of the Header and Footer on the Printed Page

In addition to displaying content, we can also control the position of the header and footer on the printed page, such as centering, left, or right. The following is an example code:

@page { 
@top-center { 
content: "Page " counter(page); 
} 
@bottom-left { 
content: "Footer Page " counter(page); 
} 
@bottom-right { 
content: "Footer Page " counter(page); 
} 
} 

In the above example, we use the @top-center, @bottom-left, and @bottom-right properties to control the position of the header and footer.

7. Control the Style of the Header and Footer on the Printed Page

In addition to positioning, you can also control the style of the header and footer on the printed page, such as font, color, size, etc. Here is a sample code:

@page {
@top-center {
content: "Page " counter(page);
font-family: Arial, sans-serif;
color: #333;
font-size: 12px;
}
}

In the above example, we use font-family: Arial, sans-serif;, color: #333;, and font-size: 12px; to control the header’s font, color, and size.

8. Controlling the Margins of the Header and Footer on Printed Pages

Sometimes we need to control the margins of the header and footer on printed pages. This can be achieved using the margin property. Below is a sample code:

@page { 
@top-center { 
content: "Page " counter(page); 
margin-top: 20px; 
margin-bottom: 10px; 
} 
} 

In the above example, we use margin-top: 20px; and margin-bottom: 10px; to control the top and bottom margins of the header.

9. Controlling the Borders of the Header and Footer on Printed Pages

In addition to margins, we can also control the borders of the header and footer on printed pages, such as line type, color, and thickness. Here’s a sample code:

@page { 
@top-center { 
content: "Page " counter(page); 
border-top: 1px solid #333; 
border-bottom: 1px dashed #666; 
} 
} 

In the above example, we use border-top: 1px solid #333; and border-bottom: 1px dashed #666; to control the top and bottom borders of the header.

10. Controlling the Background of the Header and Footer on Printed Pages

In addition to borders, we can also control the background color, https://coder-cafe.com/wp-content/uploads/2025/09/images, and more of the header and footer on printed pages. Here’s a sample code:

@page { 
@top-center { 
content: "Page " counter(page); 
background-color: #f0f0f0; 
} 
} 

In the above example, we use background-color: #f0f0f0; to control the background color of the header.

11. Controlling the Transparency of the Header and Footer on Printed Pages

In addition to the background color, we can also control the transparency of the header and footer on printed pages. Here’s a sample code:

@page { 
@top-center { 
content: "Page " counter(page); 
background-color: rgba(255, 255, 255, 0.5); 
} 
} 

In the above example, we use background-color: rgba(255, 255, 255, 0.5); to control the background transparency of the header.

12. Controlling the Fixed Position of Headers and Footers on Printed Pages

Sometimes we need to fix the position of headers and footers when printing a page. This can be achieved using the position property. Below is a sample code:

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

In the above example, we use position: fixed; to fix the position of the header.

13. Controlling the Floating Position of the Header and Footer on the Printed Page

In addition to fixed positioning, we can also control the floating position of the header and footer on the printed page, such as left, right, or center. Below is a sample code:

@page { 
@top-center { 
content: "Page " counter(page); 
position: absolute; 
left: 50%; 
transform: translateX(-50%); 
} 
} 

In the above example, we use position: absolute;, left: 50%;, and transform: translateX(-50%); to center the header.

14. Controlling the Rotation Angle of the Header and Footer on Printed Pages

Sometimes we need to rotate the angle of the header and footer when printing a page. This can be achieved using the transform property. Here’s a sample code:

@page { 
@top-center { 
content: "Page " counter(page); 
transform: rotate(45deg); 
} 
} 

In the above example, we use transform: rotate(45deg); to rotate the header angle.

15. Controlling the Hiding of Headers and Footers on Printed Pages

Sometimes we need to hide the header and footer when printing a page. This can be achieved using the display property. Below is a sample code:

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

In the above example, we use display: none; to hide the header.

16. Controlling the Display of Headers and Footers on Printed Pages

In addition to hiding, we can also control the display of headers and footers on printed pages, such as displaying them on all pages or only on the first page. Here’s a sample code:

@page { 
@top-center { 
content: "Page " counter(page); 
display: block; 
} 
@page :first { 
@top-center { 
display: none; 
} 
} 
} 

In the above example, we use display: block; to display the header, and use display: none; in @page :first to hide the header of the first page.

17. Controlling the Display of Headers and Footers on Printed Pages

Sometimes we need to display headers and footers only when printing a page. We can achieve this using the @media print media query. Here’s an example:

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

In the above example, we use the @media print media query to control the display of the header when printing.

18. Controlling the Spacing Between the Header and Footer of a Printed Page

Sometimes we need to control the spacing between the header and footer of a printed page and the page content. This can be achieved using the margin property. Below is a sample code:

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

In the above example, we use margin: 20mm; to control the spacing between the header and the page content.

19. Controlling the Size of the Header and Footer on a Printed Page

Sometimes we need to control the size of the header and footer on a printed page. This can be achieved using the size attribute. Below is a sample code:

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

In the above example, we use size: A4; to control the page size of the header and footer.

Through the above sample code, we can flexibly control the display, style, and position of the header and footer on the printed page to meet different needs.

Leave a Reply

Your email address will not be published. Required fields are marked *