Re-displaying the current heading after a page break











up vote
4
down vote

favorite
1












I'm creating a document with WeasyPrint.
I have sections that have names, some of which might span across multiple pages.
When a section is too long, a page break occurs. What I am trying to do is to re-display the current section's name, ideally with the same formatting.



The following MWE shows how the section title is not displayed after a page break:



<html>
<body>
<h1>First section</h1>
<p>Lorem ipsum...</p>
<p>Lorem ipsum...</p>
<p>Lorem ipsum...</p>
<p>Lorem ipsum...</p>

<p style="break-after: always;"></p>

<p>Lorem ipsum...</p>
</body>
</html>


Output of weasyprint example.html example.pdf:



enter image description here



I want First section to be displayed, as a <h1> tag, at the top on the left page.



I would like to do as this tex.stackexchange post which, as I understand it, basically consists in checking if the current page number exceeds the current total page count, and if it does, inserting the last section title encountered.



I'm not aware of the possibility to do so in HTML, does it exist?
Is there any workaround to do this? If not, is it possible to have WeasyPrint execute custom Python code on some page-break hook?










share|improve this question




























    up vote
    4
    down vote

    favorite
    1












    I'm creating a document with WeasyPrint.
    I have sections that have names, some of which might span across multiple pages.
    When a section is too long, a page break occurs. What I am trying to do is to re-display the current section's name, ideally with the same formatting.



    The following MWE shows how the section title is not displayed after a page break:



    <html>
    <body>
    <h1>First section</h1>
    <p>Lorem ipsum...</p>
    <p>Lorem ipsum...</p>
    <p>Lorem ipsum...</p>
    <p>Lorem ipsum...</p>

    <p style="break-after: always;"></p>

    <p>Lorem ipsum...</p>
    </body>
    </html>


    Output of weasyprint example.html example.pdf:



    enter image description here



    I want First section to be displayed, as a <h1> tag, at the top on the left page.



    I would like to do as this tex.stackexchange post which, as I understand it, basically consists in checking if the current page number exceeds the current total page count, and if it does, inserting the last section title encountered.



    I'm not aware of the possibility to do so in HTML, does it exist?
    Is there any workaround to do this? If not, is it possible to have WeasyPrint execute custom Python code on some page-break hook?










    share|improve this question


























      up vote
      4
      down vote

      favorite
      1









      up vote
      4
      down vote

      favorite
      1






      1





      I'm creating a document with WeasyPrint.
      I have sections that have names, some of which might span across multiple pages.
      When a section is too long, a page break occurs. What I am trying to do is to re-display the current section's name, ideally with the same formatting.



      The following MWE shows how the section title is not displayed after a page break:



      <html>
      <body>
      <h1>First section</h1>
      <p>Lorem ipsum...</p>
      <p>Lorem ipsum...</p>
      <p>Lorem ipsum...</p>
      <p>Lorem ipsum...</p>

      <p style="break-after: always;"></p>

      <p>Lorem ipsum...</p>
      </body>
      </html>


      Output of weasyprint example.html example.pdf:



      enter image description here



      I want First section to be displayed, as a <h1> tag, at the top on the left page.



      I would like to do as this tex.stackexchange post which, as I understand it, basically consists in checking if the current page number exceeds the current total page count, and if it does, inserting the last section title encountered.



      I'm not aware of the possibility to do so in HTML, does it exist?
      Is there any workaround to do this? If not, is it possible to have WeasyPrint execute custom Python code on some page-break hook?










      share|improve this question















      I'm creating a document with WeasyPrint.
      I have sections that have names, some of which might span across multiple pages.
      When a section is too long, a page break occurs. What I am trying to do is to re-display the current section's name, ideally with the same formatting.



      The following MWE shows how the section title is not displayed after a page break:



      <html>
      <body>
      <h1>First section</h1>
      <p>Lorem ipsum...</p>
      <p>Lorem ipsum...</p>
      <p>Lorem ipsum...</p>
      <p>Lorem ipsum...</p>

      <p style="break-after: always;"></p>

      <p>Lorem ipsum...</p>
      </body>
      </html>


      Output of weasyprint example.html example.pdf:



      enter image description here



      I want First section to be displayed, as a <h1> tag, at the top on the left page.



      I would like to do as this tex.stackexchange post which, as I understand it, basically consists in checking if the current page number exceeds the current total page count, and if it does, inserting the last section title encountered.



      I'm not aware of the possibility to do so in HTML, does it exist?
      Is there any workaround to do this? If not, is it possible to have WeasyPrint execute custom Python code on some page-break hook?







      python html css weasyprint






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 at 15:40

























      asked Nov 15 at 15:24









      Right leg

      7,92132149




      7,92132149
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          While it is not possible at this time, you can still use an awesome workaround.



          When printing, only three kind of elements are automatically reproduced on each page:




          • elements with fixed position

          • headers, declared with @top-left, @top-center, @top-right etc.

          • footers, declared with @bottom-left etc.


          We have to use one of them to build up a css-only solution: we will choose headers.
          So, the first part of the question is: how can I set a different header for each page? Or, in other words, how can I set a chapter's header?



          Achieving this goal is quite simple: once decided which tag or class should contain the chapter's title, set a new CSS string for it:



          h1 {
          string-set: doctitle content();
          }


          Then display the string in the header:



          @page {
          size: A4;
          margin: 1.6cm .6cm 1.2cm .6cm;

          @top-center {
          content: string(doctitle);
          }
          }


          Now you will have something like this:



          pages with chapter's headers



          Let me add some code to your:



          <html>
          <body>
          <h1>First section</h1>
          <p>Lorem ipsum...</p>
          <p>Lorem ipsum...</p>
          <p>Lorem ipsum...</p>
          <p>Lorem ipsum...</p>

          <p style="break-after: always;"></p>

          <p>Lorem ipsum...</p>

          <h1>Second section</h1>
          <p>Lorem ipsum 2...</p>
          <p>Lorem ipsum 2...</p>
          <p>Lorem ipsum 2...</p>
          <p>Lorem ipsum 2...</p>

          <p style="break-after: always;"></p>

          <p>Lorem ipsum 2...</p>
          </body>
          </html>


          In this case your headers will be:




          • 1st page: "First section"

          • 2nd page: "First section"… then will start the second section in the same page, with his own title

          • 3rd page: "Second section"


          chapter headers on 3 pages



          Next step: set same style for headers and chapter's titles, so headers can have the same appearance as titles:



          h1 {
          string-set: doctitle content();
          font-family: 'Liberation Serif';
          font-size: 28pt;
          line-height: 1.2em;
          }

          @page {
          size: A4;
          margin: 1.6cm .6cm 1.2cm .6cm;

          @top-left {
          content: string(doctitle);
          font-family: 'Liberation Serif';
          font-size: 28pt;
          line-height: 1.2em;
          }
          }


          Then you will have something like this:



          pages with chapter's headers styled as titles



          Now, we need to fix the latest issue: the title in the first page looks like duplicated, because of the presence of both title and header.



          Fixing it is quite simple:



          body h1:first-of-type {
          position: absolute;
          left: -30cm;
          }


          I have positioned the first title outside the printing area. Unfortunately setting it to display: none will cause that not even the header will be displayed. You have other alternatives, such as visibility: hidden or font-size: 0 or color: transparent, but these three options will always let some blank space between the header and the first paragraph.



          Now probably it's time to increase the header's height, adding top-padding to @top-left;
          The result should look like this:



          chapter's headers and hidden title



          This technique is not 100% safe: if a chapter that is not the first one will coincidentally start in a new page, both header and title will be shown, one close to the other. In any case this is not a frequent scenario.



          Further improvements can consider a different approach to page breaks.



          <html>
          <body>
          <section class="chapter">
          <h1>First section</h1>
          <p>Lorem ipsum...</p>
          <p>Lorem ipsum...</p>
          <p>Lorem ipsum...</p>
          <p>Lorem ipsum...</p>
          <p>Lorem ipsum...</p>

          <h1>Second section</h1>
          <p>Lorem ipsum 2...</p>
          <p>Lorem ipsum 2...</p>
          <p>Lorem ipsum 2...</p>
          </section>
          <section class="chapter">
          <h1>Third section</h1>
          <p>Lorem ipsum 3...</p>
          <p>Lorem ipsum 3...</p>
          <p>Lorem ipsum 3...</p>
          <p>Lorem ipsum 3...</p>
          </section>
          </body>
          </html>


          Styling page-breaks on chapters, and managing title hiding for the first child of any chapter:



          section.chapter {
          break-after: always;
          }
          section.chapter h1:first-of-type {
          position: absolute;
          left: -30cm;
          }





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322647%2fre-displaying-the-current-heading-after-a-page-break%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            While it is not possible at this time, you can still use an awesome workaround.



            When printing, only three kind of elements are automatically reproduced on each page:




            • elements with fixed position

            • headers, declared with @top-left, @top-center, @top-right etc.

            • footers, declared with @bottom-left etc.


            We have to use one of them to build up a css-only solution: we will choose headers.
            So, the first part of the question is: how can I set a different header for each page? Or, in other words, how can I set a chapter's header?



            Achieving this goal is quite simple: once decided which tag or class should contain the chapter's title, set a new CSS string for it:



            h1 {
            string-set: doctitle content();
            }


            Then display the string in the header:



            @page {
            size: A4;
            margin: 1.6cm .6cm 1.2cm .6cm;

            @top-center {
            content: string(doctitle);
            }
            }


            Now you will have something like this:



            pages with chapter's headers



            Let me add some code to your:



            <html>
            <body>
            <h1>First section</h1>
            <p>Lorem ipsum...</p>
            <p>Lorem ipsum...</p>
            <p>Lorem ipsum...</p>
            <p>Lorem ipsum...</p>

            <p style="break-after: always;"></p>

            <p>Lorem ipsum...</p>

            <h1>Second section</h1>
            <p>Lorem ipsum 2...</p>
            <p>Lorem ipsum 2...</p>
            <p>Lorem ipsum 2...</p>
            <p>Lorem ipsum 2...</p>

            <p style="break-after: always;"></p>

            <p>Lorem ipsum 2...</p>
            </body>
            </html>


            In this case your headers will be:




            • 1st page: "First section"

            • 2nd page: "First section"… then will start the second section in the same page, with his own title

            • 3rd page: "Second section"


            chapter headers on 3 pages



            Next step: set same style for headers and chapter's titles, so headers can have the same appearance as titles:



            h1 {
            string-set: doctitle content();
            font-family: 'Liberation Serif';
            font-size: 28pt;
            line-height: 1.2em;
            }

            @page {
            size: A4;
            margin: 1.6cm .6cm 1.2cm .6cm;

            @top-left {
            content: string(doctitle);
            font-family: 'Liberation Serif';
            font-size: 28pt;
            line-height: 1.2em;
            }
            }


            Then you will have something like this:



            pages with chapter's headers styled as titles



            Now, we need to fix the latest issue: the title in the first page looks like duplicated, because of the presence of both title and header.



            Fixing it is quite simple:



            body h1:first-of-type {
            position: absolute;
            left: -30cm;
            }


            I have positioned the first title outside the printing area. Unfortunately setting it to display: none will cause that not even the header will be displayed. You have other alternatives, such as visibility: hidden or font-size: 0 or color: transparent, but these three options will always let some blank space between the header and the first paragraph.



            Now probably it's time to increase the header's height, adding top-padding to @top-left;
            The result should look like this:



            chapter's headers and hidden title



            This technique is not 100% safe: if a chapter that is not the first one will coincidentally start in a new page, both header and title will be shown, one close to the other. In any case this is not a frequent scenario.



            Further improvements can consider a different approach to page breaks.



            <html>
            <body>
            <section class="chapter">
            <h1>First section</h1>
            <p>Lorem ipsum...</p>
            <p>Lorem ipsum...</p>
            <p>Lorem ipsum...</p>
            <p>Lorem ipsum...</p>
            <p>Lorem ipsum...</p>

            <h1>Second section</h1>
            <p>Lorem ipsum 2...</p>
            <p>Lorem ipsum 2...</p>
            <p>Lorem ipsum 2...</p>
            </section>
            <section class="chapter">
            <h1>Third section</h1>
            <p>Lorem ipsum 3...</p>
            <p>Lorem ipsum 3...</p>
            <p>Lorem ipsum 3...</p>
            <p>Lorem ipsum 3...</p>
            </section>
            </body>
            </html>


            Styling page-breaks on chapters, and managing title hiding for the first child of any chapter:



            section.chapter {
            break-after: always;
            }
            section.chapter h1:first-of-type {
            position: absolute;
            left: -30cm;
            }





            share|improve this answer



























              up vote
              2
              down vote



              accepted










              While it is not possible at this time, you can still use an awesome workaround.



              When printing, only three kind of elements are automatically reproduced on each page:




              • elements with fixed position

              • headers, declared with @top-left, @top-center, @top-right etc.

              • footers, declared with @bottom-left etc.


              We have to use one of them to build up a css-only solution: we will choose headers.
              So, the first part of the question is: how can I set a different header for each page? Or, in other words, how can I set a chapter's header?



              Achieving this goal is quite simple: once decided which tag or class should contain the chapter's title, set a new CSS string for it:



              h1 {
              string-set: doctitle content();
              }


              Then display the string in the header:



              @page {
              size: A4;
              margin: 1.6cm .6cm 1.2cm .6cm;

              @top-center {
              content: string(doctitle);
              }
              }


              Now you will have something like this:



              pages with chapter's headers



              Let me add some code to your:



              <html>
              <body>
              <h1>First section</h1>
              <p>Lorem ipsum...</p>
              <p>Lorem ipsum...</p>
              <p>Lorem ipsum...</p>
              <p>Lorem ipsum...</p>

              <p style="break-after: always;"></p>

              <p>Lorem ipsum...</p>

              <h1>Second section</h1>
              <p>Lorem ipsum 2...</p>
              <p>Lorem ipsum 2...</p>
              <p>Lorem ipsum 2...</p>
              <p>Lorem ipsum 2...</p>

              <p style="break-after: always;"></p>

              <p>Lorem ipsum 2...</p>
              </body>
              </html>


              In this case your headers will be:




              • 1st page: "First section"

              • 2nd page: "First section"… then will start the second section in the same page, with his own title

              • 3rd page: "Second section"


              chapter headers on 3 pages



              Next step: set same style for headers and chapter's titles, so headers can have the same appearance as titles:



              h1 {
              string-set: doctitle content();
              font-family: 'Liberation Serif';
              font-size: 28pt;
              line-height: 1.2em;
              }

              @page {
              size: A4;
              margin: 1.6cm .6cm 1.2cm .6cm;

              @top-left {
              content: string(doctitle);
              font-family: 'Liberation Serif';
              font-size: 28pt;
              line-height: 1.2em;
              }
              }


              Then you will have something like this:



              pages with chapter's headers styled as titles



              Now, we need to fix the latest issue: the title in the first page looks like duplicated, because of the presence of both title and header.



              Fixing it is quite simple:



              body h1:first-of-type {
              position: absolute;
              left: -30cm;
              }


              I have positioned the first title outside the printing area. Unfortunately setting it to display: none will cause that not even the header will be displayed. You have other alternatives, such as visibility: hidden or font-size: 0 or color: transparent, but these three options will always let some blank space between the header and the first paragraph.



              Now probably it's time to increase the header's height, adding top-padding to @top-left;
              The result should look like this:



              chapter's headers and hidden title



              This technique is not 100% safe: if a chapter that is not the first one will coincidentally start in a new page, both header and title will be shown, one close to the other. In any case this is not a frequent scenario.



              Further improvements can consider a different approach to page breaks.



              <html>
              <body>
              <section class="chapter">
              <h1>First section</h1>
              <p>Lorem ipsum...</p>
              <p>Lorem ipsum...</p>
              <p>Lorem ipsum...</p>
              <p>Lorem ipsum...</p>
              <p>Lorem ipsum...</p>

              <h1>Second section</h1>
              <p>Lorem ipsum 2...</p>
              <p>Lorem ipsum 2...</p>
              <p>Lorem ipsum 2...</p>
              </section>
              <section class="chapter">
              <h1>Third section</h1>
              <p>Lorem ipsum 3...</p>
              <p>Lorem ipsum 3...</p>
              <p>Lorem ipsum 3...</p>
              <p>Lorem ipsum 3...</p>
              </section>
              </body>
              </html>


              Styling page-breaks on chapters, and managing title hiding for the first child of any chapter:



              section.chapter {
              break-after: always;
              }
              section.chapter h1:first-of-type {
              position: absolute;
              left: -30cm;
              }





              share|improve this answer

























                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted






                While it is not possible at this time, you can still use an awesome workaround.



                When printing, only three kind of elements are automatically reproduced on each page:




                • elements with fixed position

                • headers, declared with @top-left, @top-center, @top-right etc.

                • footers, declared with @bottom-left etc.


                We have to use one of them to build up a css-only solution: we will choose headers.
                So, the first part of the question is: how can I set a different header for each page? Or, in other words, how can I set a chapter's header?



                Achieving this goal is quite simple: once decided which tag or class should contain the chapter's title, set a new CSS string for it:



                h1 {
                string-set: doctitle content();
                }


                Then display the string in the header:



                @page {
                size: A4;
                margin: 1.6cm .6cm 1.2cm .6cm;

                @top-center {
                content: string(doctitle);
                }
                }


                Now you will have something like this:



                pages with chapter's headers



                Let me add some code to your:



                <html>
                <body>
                <h1>First section</h1>
                <p>Lorem ipsum...</p>
                <p>Lorem ipsum...</p>
                <p>Lorem ipsum...</p>
                <p>Lorem ipsum...</p>

                <p style="break-after: always;"></p>

                <p>Lorem ipsum...</p>

                <h1>Second section</h1>
                <p>Lorem ipsum 2...</p>
                <p>Lorem ipsum 2...</p>
                <p>Lorem ipsum 2...</p>
                <p>Lorem ipsum 2...</p>

                <p style="break-after: always;"></p>

                <p>Lorem ipsum 2...</p>
                </body>
                </html>


                In this case your headers will be:




                • 1st page: "First section"

                • 2nd page: "First section"… then will start the second section in the same page, with his own title

                • 3rd page: "Second section"


                chapter headers on 3 pages



                Next step: set same style for headers and chapter's titles, so headers can have the same appearance as titles:



                h1 {
                string-set: doctitle content();
                font-family: 'Liberation Serif';
                font-size: 28pt;
                line-height: 1.2em;
                }

                @page {
                size: A4;
                margin: 1.6cm .6cm 1.2cm .6cm;

                @top-left {
                content: string(doctitle);
                font-family: 'Liberation Serif';
                font-size: 28pt;
                line-height: 1.2em;
                }
                }


                Then you will have something like this:



                pages with chapter's headers styled as titles



                Now, we need to fix the latest issue: the title in the first page looks like duplicated, because of the presence of both title and header.



                Fixing it is quite simple:



                body h1:first-of-type {
                position: absolute;
                left: -30cm;
                }


                I have positioned the first title outside the printing area. Unfortunately setting it to display: none will cause that not even the header will be displayed. You have other alternatives, such as visibility: hidden or font-size: 0 or color: transparent, but these three options will always let some blank space between the header and the first paragraph.



                Now probably it's time to increase the header's height, adding top-padding to @top-left;
                The result should look like this:



                chapter's headers and hidden title



                This technique is not 100% safe: if a chapter that is not the first one will coincidentally start in a new page, both header and title will be shown, one close to the other. In any case this is not a frequent scenario.



                Further improvements can consider a different approach to page breaks.



                <html>
                <body>
                <section class="chapter">
                <h1>First section</h1>
                <p>Lorem ipsum...</p>
                <p>Lorem ipsum...</p>
                <p>Lorem ipsum...</p>
                <p>Lorem ipsum...</p>
                <p>Lorem ipsum...</p>

                <h1>Second section</h1>
                <p>Lorem ipsum 2...</p>
                <p>Lorem ipsum 2...</p>
                <p>Lorem ipsum 2...</p>
                </section>
                <section class="chapter">
                <h1>Third section</h1>
                <p>Lorem ipsum 3...</p>
                <p>Lorem ipsum 3...</p>
                <p>Lorem ipsum 3...</p>
                <p>Lorem ipsum 3...</p>
                </section>
                </body>
                </html>


                Styling page-breaks on chapters, and managing title hiding for the first child of any chapter:



                section.chapter {
                break-after: always;
                }
                section.chapter h1:first-of-type {
                position: absolute;
                left: -30cm;
                }





                share|improve this answer














                While it is not possible at this time, you can still use an awesome workaround.



                When printing, only three kind of elements are automatically reproduced on each page:




                • elements with fixed position

                • headers, declared with @top-left, @top-center, @top-right etc.

                • footers, declared with @bottom-left etc.


                We have to use one of them to build up a css-only solution: we will choose headers.
                So, the first part of the question is: how can I set a different header for each page? Or, in other words, how can I set a chapter's header?



                Achieving this goal is quite simple: once decided which tag or class should contain the chapter's title, set a new CSS string for it:



                h1 {
                string-set: doctitle content();
                }


                Then display the string in the header:



                @page {
                size: A4;
                margin: 1.6cm .6cm 1.2cm .6cm;

                @top-center {
                content: string(doctitle);
                }
                }


                Now you will have something like this:



                pages with chapter's headers



                Let me add some code to your:



                <html>
                <body>
                <h1>First section</h1>
                <p>Lorem ipsum...</p>
                <p>Lorem ipsum...</p>
                <p>Lorem ipsum...</p>
                <p>Lorem ipsum...</p>

                <p style="break-after: always;"></p>

                <p>Lorem ipsum...</p>

                <h1>Second section</h1>
                <p>Lorem ipsum 2...</p>
                <p>Lorem ipsum 2...</p>
                <p>Lorem ipsum 2...</p>
                <p>Lorem ipsum 2...</p>

                <p style="break-after: always;"></p>

                <p>Lorem ipsum 2...</p>
                </body>
                </html>


                In this case your headers will be:




                • 1st page: "First section"

                • 2nd page: "First section"… then will start the second section in the same page, with his own title

                • 3rd page: "Second section"


                chapter headers on 3 pages



                Next step: set same style for headers and chapter's titles, so headers can have the same appearance as titles:



                h1 {
                string-set: doctitle content();
                font-family: 'Liberation Serif';
                font-size: 28pt;
                line-height: 1.2em;
                }

                @page {
                size: A4;
                margin: 1.6cm .6cm 1.2cm .6cm;

                @top-left {
                content: string(doctitle);
                font-family: 'Liberation Serif';
                font-size: 28pt;
                line-height: 1.2em;
                }
                }


                Then you will have something like this:



                pages with chapter's headers styled as titles



                Now, we need to fix the latest issue: the title in the first page looks like duplicated, because of the presence of both title and header.



                Fixing it is quite simple:



                body h1:first-of-type {
                position: absolute;
                left: -30cm;
                }


                I have positioned the first title outside the printing area. Unfortunately setting it to display: none will cause that not even the header will be displayed. You have other alternatives, such as visibility: hidden or font-size: 0 or color: transparent, but these three options will always let some blank space between the header and the first paragraph.



                Now probably it's time to increase the header's height, adding top-padding to @top-left;
                The result should look like this:



                chapter's headers and hidden title



                This technique is not 100% safe: if a chapter that is not the first one will coincidentally start in a new page, both header and title will be shown, one close to the other. In any case this is not a frequent scenario.



                Further improvements can consider a different approach to page breaks.



                <html>
                <body>
                <section class="chapter">
                <h1>First section</h1>
                <p>Lorem ipsum...</p>
                <p>Lorem ipsum...</p>
                <p>Lorem ipsum...</p>
                <p>Lorem ipsum...</p>
                <p>Lorem ipsum...</p>

                <h1>Second section</h1>
                <p>Lorem ipsum 2...</p>
                <p>Lorem ipsum 2...</p>
                <p>Lorem ipsum 2...</p>
                </section>
                <section class="chapter">
                <h1>Third section</h1>
                <p>Lorem ipsum 3...</p>
                <p>Lorem ipsum 3...</p>
                <p>Lorem ipsum 3...</p>
                <p>Lorem ipsum 3...</p>
                </section>
                </body>
                </html>


                Styling page-breaks on chapters, and managing title hiding for the first child of any chapter:



                section.chapter {
                break-after: always;
                }
                section.chapter h1:first-of-type {
                position: absolute;
                left: -30cm;
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 16 at 16:18

























                answered Nov 16 at 15:37









                Kalamun

                18415




                18415






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322647%2fre-displaying-the-current-heading-after-a-page-break%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

                    How to change which sound is reproduced for terminal bell?

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?