How to print a single div of a react deep structure










1















I'm trying to print a single area using React. I've build a simple component Print:



import React, Component from "react";

class Print extends Component

render = () =>
return (
<div id="printable-area">
this.props.children
</div>
);



export default Print;


Add added the following stype to the topmost div of my application:



// Body
html
box-sizing: border-box;


*,
*:before,
*:after
box-sizing: inherit;


body
font-family: $default-font;
font-weight: normal;
font-size: 14px;
color: $ux-theme-color-dark;
margin: 0px !important;
padding: 0px !important;
background-color: $ux-theme-color-background;


// Print contol (A4)
@media print
body *
display: none;

#printable-area
display: block;




No matter where in my component tree I put my Print component, nothing is being printed (always an empty page):



... several components
<Print>
... printable components
</Print>
... several components


What am I missing here?










share|improve this question






















  • Have you tried using a library that handles these kinds of things for you? Such as print-job?

    – KevBot
    Nov 13 '18 at 21:51















1















I'm trying to print a single area using React. I've build a simple component Print:



import React, Component from "react";

class Print extends Component

render = () =>
return (
<div id="printable-area">
this.props.children
</div>
);



export default Print;


Add added the following stype to the topmost div of my application:



// Body
html
box-sizing: border-box;


*,
*:before,
*:after
box-sizing: inherit;


body
font-family: $default-font;
font-weight: normal;
font-size: 14px;
color: $ux-theme-color-dark;
margin: 0px !important;
padding: 0px !important;
background-color: $ux-theme-color-background;


// Print contol (A4)
@media print
body *
display: none;

#printable-area
display: block;




No matter where in my component tree I put my Print component, nothing is being printed (always an empty page):



... several components
<Print>
... printable components
</Print>
... several components


What am I missing here?










share|improve this question






















  • Have you tried using a library that handles these kinds of things for you? Such as print-job?

    – KevBot
    Nov 13 '18 at 21:51













1












1








1








I'm trying to print a single area using React. I've build a simple component Print:



import React, Component from "react";

class Print extends Component

render = () =>
return (
<div id="printable-area">
this.props.children
</div>
);



export default Print;


Add added the following stype to the topmost div of my application:



// Body
html
box-sizing: border-box;


*,
*:before,
*:after
box-sizing: inherit;


body
font-family: $default-font;
font-weight: normal;
font-size: 14px;
color: $ux-theme-color-dark;
margin: 0px !important;
padding: 0px !important;
background-color: $ux-theme-color-background;


// Print contol (A4)
@media print
body *
display: none;

#printable-area
display: block;




No matter where in my component tree I put my Print component, nothing is being printed (always an empty page):



... several components
<Print>
... printable components
</Print>
... several components


What am I missing here?










share|improve this question














I'm trying to print a single area using React. I've build a simple component Print:



import React, Component from "react";

class Print extends Component

render = () =>
return (
<div id="printable-area">
this.props.children
</div>
);



export default Print;


Add added the following stype to the topmost div of my application:



// Body
html
box-sizing: border-box;


*,
*:before,
*:after
box-sizing: inherit;


body
font-family: $default-font;
font-weight: normal;
font-size: 14px;
color: $ux-theme-color-dark;
margin: 0px !important;
padding: 0px !important;
background-color: $ux-theme-color-background;


// Print contol (A4)
@media print
body *
display: none;

#printable-area
display: block;




No matter where in my component tree I put my Print component, nothing is being printed (always an empty page):



... several components
<Print>
... printable components
</Print>
... several components


What am I missing here?







javascript css reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 21:39









MendesMendes

4,5001057136




4,5001057136












  • Have you tried using a library that handles these kinds of things for you? Such as print-job?

    – KevBot
    Nov 13 '18 at 21:51

















  • Have you tried using a library that handles these kinds of things for you? Such as print-job?

    – KevBot
    Nov 13 '18 at 21:51
















Have you tried using a library that handles these kinds of things for you? Such as print-job?

– KevBot
Nov 13 '18 at 21:51





Have you tried using a library that handles these kinds of things for you? Such as print-job?

– KevBot
Nov 13 '18 at 21:51












2 Answers
2






active

oldest

votes


















2














The Problem:



If you remove the @media print { rule to see what the styles would appear as when that rule is applied by the browser, you can see that the parent(s) of the printable area is hiding the actual printable area. If the parent is set to display none, then any child cannot show.






/* @media print */

body *
display: none;


#printable-area
display: block;



/* */

<div>
You can't see this.
<div id="printable-area">Printable Area (You can't see this either)</div>
</div>





A basic solution:



For this solution, you will need to traverse up the parent structure for the printable area and add some additional classNames:






/* @media print */

body> :not(.print-parent):not(.print-me)
display: none;


.print-parent> :not(.print-parent):not(.print-me)
display: none;



/* */

<div class="print-parent">
<div>This should not print</div>
<div class="print-parent">
<div class="print-me">Print just this!</div>
</div>
</div>








share|improve this answer

























  • Makes sense, but how to select printable-area no matter where it is inside my div tree ?

    – Mendes
    Nov 13 '18 at 21:52











  • @Mendes, I've added a basic solution for this, but you will likely run into issues with certain HTML structures and styles, etc.

    – KevBot
    Nov 13 '18 at 22:43












  • Thanks! Got my upvote and accepted answer!

    – Mendes
    Nov 13 '18 at 22:49



















0














There's probably an ancestor of your #printable-area div that's getting hidden by the body * rule. Even if your #printable-area div has display: block, it will still not be displayed if one of its containing elements is hidden.






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',
    autoActivateHeartbeat: false,
    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%2f53289900%2fhow-to-print-a-single-div-of-a-react-deep-structure%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    The Problem:



    If you remove the @media print { rule to see what the styles would appear as when that rule is applied by the browser, you can see that the parent(s) of the printable area is hiding the actual printable area. If the parent is set to display none, then any child cannot show.






    /* @media print */

    body *
    display: none;


    #printable-area
    display: block;



    /* */

    <div>
    You can't see this.
    <div id="printable-area">Printable Area (You can't see this either)</div>
    </div>





    A basic solution:



    For this solution, you will need to traverse up the parent structure for the printable area and add some additional classNames:






    /* @media print */

    body> :not(.print-parent):not(.print-me)
    display: none;


    .print-parent> :not(.print-parent):not(.print-me)
    display: none;



    /* */

    <div class="print-parent">
    <div>This should not print</div>
    <div class="print-parent">
    <div class="print-me">Print just this!</div>
    </div>
    </div>








    share|improve this answer

























    • Makes sense, but how to select printable-area no matter where it is inside my div tree ?

      – Mendes
      Nov 13 '18 at 21:52











    • @Mendes, I've added a basic solution for this, but you will likely run into issues with certain HTML structures and styles, etc.

      – KevBot
      Nov 13 '18 at 22:43












    • Thanks! Got my upvote and accepted answer!

      – Mendes
      Nov 13 '18 at 22:49
















    2














    The Problem:



    If you remove the @media print { rule to see what the styles would appear as when that rule is applied by the browser, you can see that the parent(s) of the printable area is hiding the actual printable area. If the parent is set to display none, then any child cannot show.






    /* @media print */

    body *
    display: none;


    #printable-area
    display: block;



    /* */

    <div>
    You can't see this.
    <div id="printable-area">Printable Area (You can't see this either)</div>
    </div>





    A basic solution:



    For this solution, you will need to traverse up the parent structure for the printable area and add some additional classNames:






    /* @media print */

    body> :not(.print-parent):not(.print-me)
    display: none;


    .print-parent> :not(.print-parent):not(.print-me)
    display: none;



    /* */

    <div class="print-parent">
    <div>This should not print</div>
    <div class="print-parent">
    <div class="print-me">Print just this!</div>
    </div>
    </div>








    share|improve this answer

























    • Makes sense, but how to select printable-area no matter where it is inside my div tree ?

      – Mendes
      Nov 13 '18 at 21:52











    • @Mendes, I've added a basic solution for this, but you will likely run into issues with certain HTML structures and styles, etc.

      – KevBot
      Nov 13 '18 at 22:43












    • Thanks! Got my upvote and accepted answer!

      – Mendes
      Nov 13 '18 at 22:49














    2












    2








    2







    The Problem:



    If you remove the @media print { rule to see what the styles would appear as when that rule is applied by the browser, you can see that the parent(s) of the printable area is hiding the actual printable area. If the parent is set to display none, then any child cannot show.






    /* @media print */

    body *
    display: none;


    #printable-area
    display: block;



    /* */

    <div>
    You can't see this.
    <div id="printable-area">Printable Area (You can't see this either)</div>
    </div>





    A basic solution:



    For this solution, you will need to traverse up the parent structure for the printable area and add some additional classNames:






    /* @media print */

    body> :not(.print-parent):not(.print-me)
    display: none;


    .print-parent> :not(.print-parent):not(.print-me)
    display: none;



    /* */

    <div class="print-parent">
    <div>This should not print</div>
    <div class="print-parent">
    <div class="print-me">Print just this!</div>
    </div>
    </div>








    share|improve this answer















    The Problem:



    If you remove the @media print { rule to see what the styles would appear as when that rule is applied by the browser, you can see that the parent(s) of the printable area is hiding the actual printable area. If the parent is set to display none, then any child cannot show.






    /* @media print */

    body *
    display: none;


    #printable-area
    display: block;



    /* */

    <div>
    You can't see this.
    <div id="printable-area">Printable Area (You can't see this either)</div>
    </div>





    A basic solution:



    For this solution, you will need to traverse up the parent structure for the printable area and add some additional classNames:






    /* @media print */

    body> :not(.print-parent):not(.print-me)
    display: none;


    .print-parent> :not(.print-parent):not(.print-me)
    display: none;



    /* */

    <div class="print-parent">
    <div>This should not print</div>
    <div class="print-parent">
    <div class="print-me">Print just this!</div>
    </div>
    </div>








    /* @media print */

    body *
    display: none;


    #printable-area
    display: block;



    /* */

    <div>
    You can't see this.
    <div id="printable-area">Printable Area (You can't see this either)</div>
    </div>





    /* @media print */

    body *
    display: none;


    #printable-area
    display: block;



    /* */

    <div>
    You can't see this.
    <div id="printable-area">Printable Area (You can't see this either)</div>
    </div>





    /* @media print */

    body> :not(.print-parent):not(.print-me)
    display: none;


    .print-parent> :not(.print-parent):not(.print-me)
    display: none;



    /* */

    <div class="print-parent">
    <div>This should not print</div>
    <div class="print-parent">
    <div class="print-me">Print just this!</div>
    </div>
    </div>





    /* @media print */

    body> :not(.print-parent):not(.print-me)
    display: none;


    .print-parent> :not(.print-parent):not(.print-me)
    display: none;



    /* */

    <div class="print-parent">
    <div>This should not print</div>
    <div class="print-parent">
    <div class="print-me">Print just this!</div>
    </div>
    </div>






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 '18 at 22:41

























    answered Nov 13 '18 at 21:50









    KevBotKevBot

    9,18611944




    9,18611944












    • Makes sense, but how to select printable-area no matter where it is inside my div tree ?

      – Mendes
      Nov 13 '18 at 21:52











    • @Mendes, I've added a basic solution for this, but you will likely run into issues with certain HTML structures and styles, etc.

      – KevBot
      Nov 13 '18 at 22:43












    • Thanks! Got my upvote and accepted answer!

      – Mendes
      Nov 13 '18 at 22:49


















    • Makes sense, but how to select printable-area no matter where it is inside my div tree ?

      – Mendes
      Nov 13 '18 at 21:52











    • @Mendes, I've added a basic solution for this, but you will likely run into issues with certain HTML structures and styles, etc.

      – KevBot
      Nov 13 '18 at 22:43












    • Thanks! Got my upvote and accepted answer!

      – Mendes
      Nov 13 '18 at 22:49

















    Makes sense, but how to select printable-area no matter where it is inside my div tree ?

    – Mendes
    Nov 13 '18 at 21:52





    Makes sense, but how to select printable-area no matter where it is inside my div tree ?

    – Mendes
    Nov 13 '18 at 21:52













    @Mendes, I've added a basic solution for this, but you will likely run into issues with certain HTML structures and styles, etc.

    – KevBot
    Nov 13 '18 at 22:43






    @Mendes, I've added a basic solution for this, but you will likely run into issues with certain HTML structures and styles, etc.

    – KevBot
    Nov 13 '18 at 22:43














    Thanks! Got my upvote and accepted answer!

    – Mendes
    Nov 13 '18 at 22:49






    Thanks! Got my upvote and accepted answer!

    – Mendes
    Nov 13 '18 at 22:49














    0














    There's probably an ancestor of your #printable-area div that's getting hidden by the body * rule. Even if your #printable-area div has display: block, it will still not be displayed if one of its containing elements is hidden.






    share|improve this answer



























      0














      There's probably an ancestor of your #printable-area div that's getting hidden by the body * rule. Even if your #printable-area div has display: block, it will still not be displayed if one of its containing elements is hidden.






      share|improve this answer

























        0












        0








        0







        There's probably an ancestor of your #printable-area div that's getting hidden by the body * rule. Even if your #printable-area div has display: block, it will still not be displayed if one of its containing elements is hidden.






        share|improve this answer













        There's probably an ancestor of your #printable-area div that's getting hidden by the body * rule. Even if your #printable-area div has display: block, it will still not be displayed if one of its containing elements is hidden.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 21:50









        AaroniusAaronius

        2,91252635




        2,91252635



























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289900%2fhow-to-print-a-single-div-of-a-react-deep-structure%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

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党