Adding a margin to each page using the @page feature in CSS

I am currently working on a project where I have a dynamically generated HTML page that needs to be saved as a PDF. The issue I am facing is related to the page border, which needs to appear at the edge of the page when printed. To achieve this, I have set the @media print to specify the page size as A4 and margins as 0. However, the content ends up being overwritten on the page border, causing layout issues.

One solution I tried was adding padding to the border, which resolved the problem at the top of the first page. But this caused the content to overlap with the border at the bottom and on subsequent pages.

Visual examples of these problems can be seen in the images provided below.

https://i.sstatic.net/KT9SI.png

The top of the first page appears fine after adding padding, but issues persist at the bottom.

https://i.sstatic.net/eW2PW.png

In the second page, the entire content spills over into the margin area.

Here is the corresponding HTML code for reference:

// CSS styling
body {
  margin: 0px;
  padding: 0px;
}

// Page borders with different configurations
#pageborder {
  // Code for page border style 1
}

#pageborder2 {
  // Code for page border style 2
}

#pageborder3 {
  // Code for page border style 3
}

// Inner content styling
#innerContent {
  margin: 50px;
}

@media print {
  @page{
    margin: 0;
  }

  #pageborder3 {
    padding-top: 90px;
    padding-bottom: 90px;
  }
}
<body>
  <div id="pageborder">
</div>

<div id="pageborder2">
</div>
<div id="pageborder3">
</div>

<div id='innerContent'>
  <h1>Test</h1>
  <p>Lorem Ipsum</p>
</div>
</body>

Answer №1

For an effective solution, consider using a combination of page-break-inside: avoid and adjusting padding/margin on paragraphs.

Keep in mind that very large paragraphs around the page break might cause an awkward gap at the bottom of the page. To avoid this, it's recommended to divide the text into smaller paragraphs instead.

/* This snippet includes relevant styles for addressing the problem while removing unnecessary styles */

@page{
size: A4;
margin: 0;
}

#innerContent {
/* Margin around the content */
margin: 50px;
}

h1, h2, h3, h4, h5, h6, p, .flow-control 
/* Include elements or add class="flow-control" where needed */ 
{
page-break-inside: avoid; 
padding-top: 50px; 
margin-top: -50px; 
}

h1, h2{
margin-top: -20px; 
}
 
p { 
padding-bottom: 20px; 
}
<body>
<div class="pageborder" id="pageborder1"></div>
<div class="pageborder" id="pageborder2"></div>
<div class="pageborder" id="pageborder3"></div>

<article id='innerContent'>
    <h1>Lorem Ipsum</h1>
    
    <!-- Content with smaller paragraphs -->
    
<p>Paragraph 1...</p>
<p>Paragraph 2...</p>
...
<p>Paragraph N...</p>

  </article>
</body>

Answer №2

To achieve the desired effect, include the following lines of code into your CSS file:

#pageborder2:after{
     content: "";
    width: 92%;
    background: #FFF;
    height: 10px;
    position: fixed;
    margin-left: 15px;
    top: 10px;
    border-radius: 10px;
}
#pageborder3:after{
    content: "";
    width: 92%;
    background: #FFF;
    height: 10px;
    border-radius: 10px;
    position: fixed;
    bottom: 10px;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

jquery technique for toggling a single button

My goal is to use jQuery to toggle a button rather than the typical paragraph toggling. I want the button to appear and disappear when clicked, while also increasing the "score" upon each click and decreasing it when the button reappears. Can anyone guide ...

Adding AngularJS modules to an HTML file

Recently, I've been diving into the world of AngularJS, but I'm facing a roadblock - my HTML doesn't recognize the angular module I created. It's odd because the bindings work perfectly without involving the module... This is my HTML: ...

Having trouble with CSS values not being applied to dynamically injected HTML div elements in Angular 4?

Link to Codepen My Angular calendar application runs smoothly without any errors. However, I am encountering an issue where the CSS styles are not being applied to the page. When I implemented this separately, everything worked fine. But as soon as I inc ...

"Return to previous page" feature

I am having difficulty figuring out how to implement the "go back" function for the page. For instance, I have pages A, B, C, and D. The possible switching between pages is as follows: A -> (B <-> C) -> A or D -> (B <-> C) -> D (w ...

Retrieving an HTML element that has been added through DOM manipulation

After successfully creating a Jquery function that inserts a 'save button' into the page when a specific button is clicked, I encountered an issue with another function meant to be activated when the save button is clicked. The first function see ...

Don't forget to update the CSS stylesheet whenever templates are rendered in Flask

I have developed a Flask application using HTML and external (static) CSS stylesheets. One interesting feature of the application is a checkbox that uses JavaScript to toggle between two different CSS stylesheets - one for light mode and one for dark mode. ...

Arranging sequence of jQuery functions

I have implemented a loop using jQuery that iterates through specific elements on an HTML page. During each iteration, I switch over a variable and add HTML code to particular locations. The issue arises when one of the appends requires importing another ...

Align the single element to the right

Is there a way to right align a single element within a dropdown list using Bootstrap 5? I typically use "justify-content-end" for this purpose, but it doesn't seem to work in this case. <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn- ...

Flexbox alignment issue: Text is not vertically centered

I'm facing an issue with centering my content vertically. Upon inspecting the divs, I noticed some empty space below them that seems to be causing the problem. When I tried adding <line-height: 3> in the dev tool styles, it seemed to center prop ...

Injecting controllers and classes dynamically using AngularJS

I'm currently working on a dynamic widgets list where each widget is defined by its html, class, controller, and name. Some of these parameters may be empty based on the type of widget. These widgets are then loaded dynamically into <li> element ...

The Tab style in Mobile Angular UI does not get applied correctly when nested within an ng-repear

While working on a tabbed control with mobile-angular-ui (), I encountered an issue when trying to generate the tabs dynamically. Initially, everything looked good with the following code: <ul class="nav nav-tabs" ui-state='activeTab' ui-def ...

Different ways to modify the underline and label color in Material-UI's <Select/> component

A while ago, I came across this useful demo that helped me change the colors of input fields. Here is the link: https://stackblitz.com/edit/material-ui-custom-outline-color Now, I'm on a quest to find a similar demo for customizing the color of selec ...

My webpage layout doesn't quite accommodate my content, so I need to adjust it to be more zoom

Why does the page container load bigger than the html/body container in mobile mode, as shown in the photo? https://i.sstatic.net/fD1N4.png Here is my CSS: html { margin-top: 48px; width: 100%; } body { margin: 0; width: 100%; background: ...

What is the best way to dynamically set minDate for a datetime-local input field?

I need assistance with setting up two input fields: one for the start date and one for the end date. I want the end date to automatically populate with the same value as the start date when the start date is filled, and have the minimum value set to be the ...

When you add CSS font-size to the body element, how can you effectively manage the nesting of tags and the concept of inheritance within

Currently revamping a website loaded with old content. The main change in design is to make the site adaptable to different screen sizes. I am utilizing the font-size in the body element for this purpose and setting all measurements to ems. While this meth ...

Setting a custom background image in HTML using the designated file PATH

My current issue involves the inability to set my background photo using a CSS file. I am utilizing NetBeans for this project. Inside a folder named css, there is a file called global.css which contains the following code: body{ background-image: url ...

Angular 4: Conditional CSS classes causing issues with transitions

After scouring through stackoverflow, I have yet to find a solution to my current issue. I am utilizing a conditional class on a div that is applied when a boolean variable becomes true. Below is the code snippet in question: <div [class.modalwindow-sh ...

How can I easily tailor Google Map designs for embedding on websites using iframes?

How do I customize the color of Map attributes within an iframe? <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3929.7682222383455!2d78.13052821529986!3d9.95323247656748!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x3b00c5e0 ...

Enhance your UI experience with a beautifully styled button using Material-

I was using a Material UI button with a purple background. <Button component={Link} to={link} style={{ background: '#6c74cc', borderRadius: 3, border: 0, color: 'white', heig ...

Prevent content overlap in Tumblr's sidebars by using CSS positioning techniques

I'm facing an issue with my blog on Tumblr where the sidebar keeps overlapping the main content. I have modified a premade template and am trying to position the sidebar in the top right corner using absolute or fixed positioning: #sidebar{ position: ...