Printing HTML div and page break issue when using float / position styles

477 Views Asked by At

See this class:

.divDutySlip {
    width: 90mm;
    min-height: 120mm; 
    padding: 2mm;
    /*float:left;
    position: relative; */
    margin: 2mm;
    border: 1px solid #000000;
    page-break-inside: avoid;

}

I have commented out two styles. Commenting them out causing the content to page-break correct. No "Duty Slip" is split across a page:

Preview

Now, if I introduce the float and position styles and try again the page break logic fails. If I try to print landscape then I get slips side by side but they still split on page breaks. This example is with a lot more content and I have blocked out the names:

Preview

So how can I fill the page with these slips and prevent splitting over page breaks? At the moment the only way I can do it is by commenting out those styles and limiting to one column of duty slips.

CSS:

body {
    font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
    font-size: 12pt;
    text-align: left;
    color: #000000;
    background-color: #ffffff;
}
.divDutySlip {
    width: 90mm;
    min-height: 120mm; 
    padding: 2mm;
    /*float:left;
    position: relative; */
    margin: 2mm;
    border: 1px solid #000000;
    page-break-inside: avoid;

}
.textTitle {
    font-size: 14pt;
    font-weight: 700;
}
.textName {
    font-size: 12pt;
}
.tableDutySlip {
    width: 100%;
    border:1px black solid;
    border-collapse:collapse;
}
.tableDutySlip td {
    border:1px black solid;
}
.cellHeading {
    font-weight: 700;
    background-color: #808080;
}
.cellDate {

}
.cellAssignments {

}
.columnDate {
    width: 25%; 
}
.columnAssignments {
    width: 75%;
}

@media screen
{
    br { display: none; }
}

HTML:

<!DOCTYPE html PUBLIC "//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="IE=edge" http-equiv="X-UA-Compatible" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<link href="SRRSchedule-Duty%20Slips1.css" rel="stylesheet" type="text/css" />
<title>Assignment Duties</title>
</head>

<body>

<div class="divDutySlip">
    <h1 class="textTitle">Assignment Duties</h1>
    <h2 class="textName">Test1</h2>
    <table class="tableDutySlip">
        <colgroup>
            <col class="columnDate" /><col class="columnAssignments" />
        </colgroup>
        <tr>
            <td class="cellHeading">Date</td>
            <td class="cellHeading">Assignments</td>
        </tr>
        <tr>
            <td class="cellDate">Thu, Apr 9</td>
            <td class="cellAssignments">Mic Left, Mic Right</td>
        </tr>
        <tr>
            <td class="cellDate">Sun, Apr 12</td>
            <td class="cellAssignments">Watchtower Reader</td>
        </tr>
        <tr>
            <td class="cellDate">Thu, Apr 16</td>
            <td class="cellAssignments">Mic Left, Mic Right</td>
        </tr>
    </table>
</div>
<div class="divDutySlip">
    <h1 class="textTitle">Assignment Duties</h1>
    <h2 class="textName">Test2</h2>
    <table class="tableDutySlip">
        <colgroup>
            <col class="columnDate" /><col class="columnAssignments" />
        </colgroup>
        <tr>
            <td class="cellHeading">Date</td>
            <td class="cellHeading">Assignments</td>
        </tr>
        <tr>
            <td class="cellDate">Sun, Apr 12</td>
            <td class="cellAssignments">Mic Left, Mic Right</td>
        </tr>
        <tr>
            <td class="cellDate">Sun, Apr 19</td>
            <td class="cellAssignments">Mic Left, Mic Right</td>
        </tr>
    </table>
</div>
<div class="divDutySlip">
    <h1 class="textTitle">Assignment Duties</h1>
    <h2 class="textName">test3</h2>
    <table class="tableDutySlip">
        <colgroup>
            <col class="columnDate" /><col class="columnAssignments" />
        </colgroup>
        <tr>
            <td class="cellHeading">Date</td>
            <td class="cellHeading">Assignments</td>
        </tr>
        <tr>
            <td class="cellDate">Sun, Apr 19</td>
            <td class="cellAssignments">Watchtower Reader</td>
        </tr>
    </table>
</div>

</body>

</html>

Fiddle:

https://jsfiddle.net/e3kradLh/

1

There are 1 best solutions below

1
vishnu sandhireddy On

In case if you would like to have all the dutyslips in one page you can create a wrapper div for all the dutyslips and add display flex style to it. https://jsfiddle.net/jhbktoya/1/

body {
  font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
  font-size: 12pt;
  text-align: left;
  color: #000000;
  background-color: #ffffff;
}

.wrapper {
  display: flex;
}

.divDutySlip {
  width: 90mm;
  min-height: 120mm;
  padding: 2mm;
  margin: 2mm;
  border: 1px solid #000000;
  page-break-inside: avoid;
}

.textTitle {
  font-size: 14pt;
  font-weight: 700;
}

.textName {
  font-size: 12pt;
}

.tableDutySlip {
  width: 100%;
  border: 1px black solid;
  border-collapse: collapse;
}

.tableDutySlip td {
  border: 1px black solid;
}

.cellHeading {
  font-weight: 700;
  background-color: #808080;
}

.cellDate {}

.cellAssignments {}

.columnDate {
  width: 25%;
}

.columnAssignments {
  width: 75%;
}

@media screen {
  br {
    display: none;
  }
}
<div class='wrapper'>
  <div class="divDutySlip">
    <h1 class="textTitle">Assignment Duties</h1>
    <h2 class="textName">Test1</h2>
    <table class="tableDutySlip">
      <colgroup>
        <col class="columnDate" />
        <col class="columnAssignments" />
      </colgroup>
      <tbody>
        <tr>
          <td class="cellHeading">Date</td>
          <td class="cellHeading">Assignments</td>
        </tr>
        <tr>
          <td class="cellDate">Thu, Apr 9</td>
          <td class="cellAssignments">Mic Left, Mic Right</td>
        </tr>
        <tr>
          <td class="cellDate">Sun, Apr 12</td>
          <td class="cellAssignments">Watchtower Reader</td>
        </tr>
        <tr>
          <td class="cellDate">Thu, Apr 16</td>
          <td class="cellAssignments">Mic Left, Mic Right</td>
        </tr>
      </tbody>
    </table>
  </div>
  <div class="divDutySlip">
    <h1 class="textTitle">Assignment Duties</h1>
    <h2 class="textName">Test2</h2>
    <table class="tableDutySlip">
      <colgroup>
        <col class="columnDate" />
        <col class="columnAssignments" />
      </colgroup>
      <tbody>
        <tr>
          <td class="cellHeading">Date</td>
          <td class="cellHeading">Assignments</td>
        </tr>
        <tr>
          <td class="cellDate">Sun, Apr 12</td>
          <td class="cellAssignments">Mic Left, Mic Right</td>
        </tr>
        <tr>
          <td class="cellDate">Sun, Apr 19</td>
          <td class="cellAssignments">Mic Left, Mic Right</td>
        </tr>
      </tbody>
    </table>
  </div>
  <div class="divDutySlip">
    <h1 class="textTitle">Assignment Duties</h1>
    <h2 class="textName">test3</h2>
    <table class="tableDutySlip">
      <colgroup>
        <col class="columnDate" />
        <col class="columnAssignments" />
      </colgroup>
      <tbody>
        <tr>
          <td class="cellHeading">Date</td>
          <td class="cellHeading">Assignments</td>
        </tr>
        <tr>
          <td class="cellDate">Sun, Apr 19</td>
          <td class="cellAssignments">Watchtower Reader</td>
        </tr>
      </tbody>
    </table>
  </div>

</div>