The HTML div measuring A4 dimensions surpasses the size of an A4 document

When I set the size of a parent div to A4 (size="21cm 29.7cm"), I encountered an issue in Chrome's print preview where the HTML page would not fit on A4 without any margins. I had to manually scale it down to 80%.

I was under the impression that using size="21cm 29.7cm" should automatically adjust the page to be A4 size. Is there something in my HTML code that could be causing this discrepancy?

UPDATE: During my research, I came across this helpful resource:

CSS to set A4 paper size

I attempted to use the following CSS code snippet but unfortunately, it did not resolve the issue:

@page {
  size: A4;
  margin: 0;
}
@media print {
  html, body {
    width: 210mm;
    height: 297mm;
  }
}

Below is a glimpse of my HTML structure:

<!Doctype>
<html>
<head>
  <style>
    .background
    {
      position: relative;
      top: 0;
      left: 0;
    }
    .text
    {
      position: absolute;
      top: 0px;
      left: 0px;
      width: 1449px;
      height: 2050px;
    }
  </style>
</head>
<body>
  <div style="position: relative; left: 0; top: 0;" size="21cm 29.7cm">
    <img src='page0022.jpg' class="background"/>
    <img src='0022.svg' class="text"/>
  </div>
</body>
</html>

Answer №1

Explore the importance of size: https://developer.mozilla.org/en-US/docs/Web/CSS/@page/size

Include the following code in your HTML document:

<div class="page">...</div>

Add this code to your CSS file

@page {
    size: 21cm 29.7cm;
    margin: 30mm 45mm 30mm 45mm;
     /* adjust margins as needed */
}

@media print {
    body{
        width: 21cm;
        height: 29.7cm;
        margin: 30mm 45mm 30mm 45mm; 
        /* adjust margins as needed */
   } 
}

If you prefer using pixels (though it's generally advised against), make sure to choose the appropriate DPI for printing:

  • 72 dpi (web) = 595 X 842 pixels
  • 300 dpi (print) = 2480 X 3508 pixels
  • 600 dpi (high-quality print) = 4960 X 7016 pixels

To avoid rendering issues, it's best to use cm (centimeters) or mm (millimeters) for sizing over pixels when working with different clients.

Answer №2

When working with CSS, it is important to remember that the size property should be defined within an @page rule in your stylesheet. It is not meant to be used as an attribute, as demonstrated in the example.

Check out this link for more information on using @page

@page {
  size: A4;
} 

The size attribute is typically utilized to specify the width of elements like <input> and <select>, based on pixels or character count depending on the element's type

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

React snap scrolling feature is not working as intended

I've been attempting to implement the snap scroll feature in my react app, but I'm facing issues with it. The regular CSS method doesn't seem to work well in Chrome, as discussed in this Scroll Snap Skips Section on Smaller Screen - Chrome t ...

Tips for creating space between table columns

I am looking to enhance the spacing between columns in my table to achieve a layout similar to the reference image provided. Can you suggest any adjustments I can make to my code to achieve this look? Reference table : https://i.sstatic.net/xfqiR.png He ...

Enable sidebar navigation exclusively for mobile and iPad devices

Is there a way to implement a different navigation bar specifically for mobile devices like iPads? I'm considering using this one: Here is the source code: JSFiddle If anyone knows how to achieve this, please share! <link rel="shortcut icon" typ ...

Position the "SHARE" button at the exact center of both the vertical and horizontal axes of an image (activating when the cursor hovers over it)

I'm currently working on a project and I really like the design on Fancy: . When you hover over an image, the background darkens and a button appears in the front. What I want to achieve is similar, but with a SHARE button instead. I have the image d ...

Is there a way to adjust the brightness of specific sections within an image using html, css, and js?

I am working on a project where I have an image with tags underneath that correspond to different parts of the image. For example, if the image is of a dog, one of the tags could be 'nose', indicating the portion of the image around the dog' ...

Slicknav is failing to appear on the screen, although it is being

After spending hours trying to implement a slicknav menu into my school project, I'm stuck and seeking guidance. Despite having some coding experience, I can't seem to find the solution to my problem. Any help, insight, or tips on fixing or impro ...

The height of the href is not displaying correctly when the text-decoration is disabled and it is placed next to

In my design, I have a link positioned next to an image with perfect vertical alignment. Everything looks great when the text has the standard text-decoration, centered vertically and all that. However, as soon as I remove the text-decoration, leaving the ...

gist-react: containing gist within a scrollable div

Struggling to contain a gist within a div with scrollbars on my next.js site using gist-react. It seems like my CSS is being overridden when the gist loads from the underlying dependency. Is there a way to achieve this without modifying the dependency itse ...

Is there a way to adjust the size of this map?

I’m attempting to adjust the dimensions of the div.wx-imap-pane.wx-module to a height of 346px and a width of 1727px using greasemonkey, but I’m struggling to make the change take effect. My attempt: div#div.wx-imap-pane.wx-module, div#div.wx-imap-pa ...

What is the best way to retrieve an element from an array within a script?

As a newbie in the realm of vue and Laravel Framework, I am eager to fetch data from an API and display it dynamically on my web page. To achieve this, I have already set up a table named 'Progress' where I seeded some initial data. Utilizing AP ...

I'm having trouble getting CSS to apply to my HTML file. Any idea why?

I conducted tests in both Chrome and Firefox, ruling out any browser-related issues. My CSS has been validated and is error-free. However, when I validate my HTML code, an error message pops up stating "Bad value “stylesheet” for attribute rel on eleme ...

Locate the closest pals near my present whereabouts

Is it possible to find my friends by obtaining their location from their mobile phones when they are near me? I have a code snippet below with the variable cities where I can input the numbers of my friends. Can I achieve this or do I need to make some m ...

using javascript to target a specific css selector attribute

I have a CSS file with the following code: .datagrid table tbody td { color: #00496B; border-left: 1px solid #E1EEF4; font-size: 16px ;font-weight: normal; } Is there a way to use JavaScript to dynamically change the font size? The code below worked ...

Tips for increasing the height of a div as rows are dynamically added to a table

I am facing an issue with a div main containing a table grid. Rows are dynamically added to the table, causing it to increase in height. However, the height of the div main remains unchanged, resulting in the table overlapping the footer content. What chan ...

Toggle the operational status of the bootstrap switch

Does anyone have a solution for customizing the appearance of the Bootstrap switch button when it is disabled? I have successfully styled it for the enabled state, but it defaults to a dark appearance when disabled. I would like the disabled state to have ...

Unable to process form submission with AngularJS + Stormpath

I am facing an issue with form submission. Even though I believe that the login and password data are being sent correctly, nothing happens when I submit the form. I am attempting to submit the form without using ngSubmit because it is not feasible in my s ...

Is it possible to nest a <dl> list within a <li> item?

Would it be considered semantically accurate to include a dl (along with some dt / dd elements) inside a li tag? While it's common to add block or inline elements within an li element, the feasibility of including dl, dt, and dd elements is uncertain ...

C# - In ASP.NET MVC, how can I send a model containing another model through an HTML form?

I'm currently working on a form that needs to be passed to my controller. Inside the main model, there is another model that I need to consider. Here is how the models and the form are structured. I'm not sure if the controller will be able to r ...

CSS styles not displaying correctly - malfunctioning (unusual)

Trying to tackle this issue, but feeling a bit lost. Whenever I resize the site below 1600px, everything breaks despite having media queries in place to handle the changes. One particular problem that's baffling me is related to a logo in an id that s ...

Tips for passing a timestamp to a Postgresql DB using a Button in a Form instead of a traditional rails time_select box

I am currently developing a CAD App in Rails 4 with Ruby 2. The goal is to provide users with a button on the screen to log an on-scene time and clear scene time, especially since most users will be using touch screen computers. Instead of using the defaul ...