PrintCSS: Page Margin Boxes
Tags: html, css, printcss, margin
Published: 2021-02-27 23:15:02

In my article about running elements like headers, we already worked with page margin boxes without explaining them in detail.

So what are [page margin boxes](https://www.w3.org/TR/css-page-3/#margin-boxes)? They are boxes within the page margin and can contain generated content, like page numbers. Also, they are often used to add the document title or copyright information on many pages.

There are 16 page margin boxes so let us have a look at every single box. We will go clockwise, starting with the top left corner.

If you want to try the samples yourself, head over to [printcss.live](https://printcss.live/) and play around with different PDF rendering tools.

![top-left-corner page margin box, rendered with typeset.sh](https://cdn-images-1.medium.com/max/2000/1*qw6KTwMmsgh2Om84crjphA.png)*top-left-corner page margin box, rendered with typeset.sh*

### @top-left-corner (1)

The top left corner’s size is defined by the top and left margin of the page. In the screenshot on the left, you see the corner on a page with a margin of 20mm. This means our box will also be 20x20mm. If you defined the page’s left margin as 10mm and the top margin to be 20mm, the top left corner would have a size of 10x20mm.

    @page  {
     margin: 20mm;

     @top-left-corner{
      content:"1";
      background-color:#1a202c;
      color:#fff;
      text-align:center;
     }
    }

### @top-left (2)

![top-left page margin box, rendered with typeset.sh](https://cdn-images-1.medium.com/max/2000/1*Ix_360UXqxssK3fgsUuLrQ.png)*top-left page margin box, rendered with typeset.sh*

The top left margin box sits between the top left corner and the top center box. The width of this box is variable. As you see in the screenshot above, it goes all the way to the top right corner page margin box. Once we define the top center box, the width will decrease as the center box also takes up some space.

    @page  {
     margin: 20mm;

     /* previous css code... */
      
     @top-left{
      content:"2";
      background-color:#742a2a;
      color:#fff;
      text-align:center;
     }
    }

### @top-center (3)

![top-center page margin box, rendered with typeset.sh](https://cdn-images-1.medium.com/max/2000/1*f6BIp41SI22vvlb9biHA4Q.png)*top-center page margin box, rendered with typeset.sh*

The top center is, as the name suggests, in the center of the top margin boxes. This box also has a variable width. The width depends on its neighbors’ width, the top left, and top right page margin boxes.

    @page  {
     margin: 20mm;

     /* previous css code... */

     @top-center{
      content:"3";
      background-color:#7b341e;
      color:#fff;
      text-align:center;
     }
    }

### @top-right (4)

![top-right page margin box, rendered with typeset.sh](https://cdn-images-1.medium.com/max/2000/1*rx4vyynwV136ShHiUM8i-g.png)*top-right page margin box, rendered with typeset.sh*

The top right margin box sits between the top center and the top right corner box. The width of this box is variable. As you see in the screenshot above, it goes from the center box to the top right corner page margin box.

    @page  {
     margin: 20mm;

     /* previous css code... */

     @top-right{
      content:"4";
      background-color:#744210;
      color:#fff;
      text-align:center;
     }
    }

![top right corner, rendered with typeset.sh](https://cdn-images-1.medium.com/max/2000/1*Ub4-__YQMW1O5RCsrX4wMA.png)*top right corner, rendered with typeset.sh*

### @top-right-corner (5)

The top right corner box size is defined by the top and right margin of the page. Look at the definition of the top left corner for details.

    @page  {
     margin: 20mm;

     /* previous css code... */

     @top-right-corner{
      content:"5";
      background-color:#22543d;
      color:#fff;
      text-align:center;
     }
    }

### @right-top (6)

![right top page margin box, rendered with typeset.sh](https://cdn-images-1.medium.com/max/2000/1*Rexl1vvwqhBPiKQsuOVMcw.png)*right top page margin box, rendered with typeset.sh*

The right top page margin box has a variable height. It fills all the space between the top right corner and the right middle box. As we have not defined the right middle box yet, this box goes till the bottom right corner box.

    @page  {
     margin: 20mm;

     /* previous css code... */

     @right-top{
      content:"6";
      background-color:#234e52;
      color:#fff;
     }
    }

### @right-middle (7)

![The right middle page margin box, rendered with typeset.sh](https://cdn-images-1.medium.com/max/2000/1*KbeFzAfkWdzR5Ar2yRCqlQ.png)*The right middle page margin box, rendered with typeset.sh*

The right middle box is vertically centered on the right side. Depending on its height, the neighboring boxes (right top and right bottom) adjust.

    @page  {
     margin: 20mm;

     /* previous css code... */
      
     @right-middle{
      content:"7";
      background-color:#2a4365;
      color:#fff;
     }
    }

### @right-bottom (8)

![The right bottom margin box (8), rendered with typeset.sh](https://cdn-images-1.medium.com/max/2000/1*IrIR45uieyu5Xh7FvCDklQ.png)*The right bottom margin box (8), rendered with typeset.sh*

The right bottom page margin box behaves the same as the right top box, just on the page’s bottom. It fills the space between the right middle box and the right bottom corner box.

    @page  {
     margin: 20mm;

     /* previous css code... */
       
     @right-bottom{
      content:"8";
      background-color:#3c366b;
      color:#fff;
     }
    }

### @bottom-right-corner, @bottom-right, @bottom-center, @bottom-left, @bottom-left-corner (9–13)

![The bottom page margin boxes, rendered with typeset.sh](https://cdn-images-1.medium.com/max/2000/1*uuOeF3FXTGeZXfMInW8b_A.png)*The bottom page margin boxes, rendered with typeset.sh*

The bottom page margin boxes behave the same way as the top page margin boxes. For details, please have a look at the top boxes above.

    @page  {
     margin: 20mm;

     /* previous css code... */
       
     @bottom-right-corner {
      content:"9";
      background-color:#44337a;
      color:#fff;
      text-align:center;
     }
      
     @bottom-right {
      content:"10";
      background-color:#702459;
      color:#fff;
      text-align:center;
     }

     @bottom-center{
      content:"11";
      background-color:#1a202c;
      color:#fff;
      text-align:center;
     }

     @bottom-left{
      content:"12";
      background-color:#742a2a;
      color:#fff;
      text-align:center;
     }

     @bottom-left-corner{
      content:"13";
      background-color:#7b341e;
      color:#fff;
      text-align:center;
     }
    }

### @left-bottom, @left-middle, @left-top (14–16)

For the left page margin boxes, have a look at the right boxes. They behave the same way just on the other side.

![All page margin boxes on one page, rendered with typeset.sh](https://cdn-images-1.medium.com/max/2000/1*bsae1ORqneJQCpZ7QgVQlQ.png)*All page margin boxes on one page, rendered with typeset.sh*

    @page  {
     margin: 20mm;

     /* previous css code... */
     
     @left-bottom{
      content:"14";
      background-color:#744210;
      color:#fff;
     }
      
     @left-middle{
      content:"15";
      background-color:#22543d;
      color:#fff;
     }
      
     @left-top{
      content:"16";
      background-color:#234e52;
      color:#fff;
     }
    }