Print part of a web page only with CSS and JavaScript

Поділитися
Вставка
  • Опубліковано 24 січ 2025

КОМЕНТАРІ • 65

  • @yacoubasawadogo8225
    @yacoubasawadogo8225 10 місяців тому +1

    Thank you so much 🙏. Very useful tutorial. I spent days researching how to generate a pdf. I came across tutorials for JSPDF, Html2pdf,... all of them didn't work. But your solution is so much and power! For those who would like to add margins to pages to prevent text from sticking to the borders:
    @media print {
    @page {
    margin-top: 10mm;
    margin-bottom:10mm;
    }
    }

  • @tinfoilhat4417
    @tinfoilhat4417 Рік тому

    Cheers for this, made it seem so easy!

  • @abdulrehman_jiwani
    @abdulrehman_jiwani Рік тому

    Bro the video was very informative
    I didn't know the concept of print function but you clarified my concept
    Thanks, Great bro 👍🏻

  • @OpenJavaScript
    @OpenJavaScript  2 роки тому

    Thanks for watching!
    👉 Source code and live working example: openjavascript.info/2022/05/17/printing-a-page-or-part-of-a-page-using-javascript/

  • @electromusicluxho
    @electromusicluxho Рік тому +2

    Uff... Men, resolviste mi dolor de cabeza en 5 minutos... jeeje... Gracias...

  • @James-is8rl
    @James-is8rl 8 місяців тому

    thanks bro, how to print but including css settings to, as padding, color etc ?

  • @yurisouzak
    @yurisouzak Рік тому +1

    omg i love u man... you're my hero!!!

  • @sumedhburbure4173
    @sumedhburbure4173 Рік тому +1

    Thanks A lot !! This video was very helpful

    • @OpenJavaScript
      @OpenJavaScript  Рік тому

      You're welcome, glad it helped, thanks for reaching out!

  • @lucasminamihara2265
    @lucasminamihara2265 Рік тому +1

    great video, helped me a lot! thank you

    • @OpenJavaScript
      @OpenJavaScript  Рік тому

      That's great to hear, glad you found it useful. Thanks for reaching out to let me know!

  • @jonathanladeno993
    @jonathanladeno993 9 місяців тому

    i have a problem for this method, in input radio type, it thus not get if it is selected or not

  • @x.prgrmmr2780
    @x.prgrmmr2780 2 роки тому +2

    now how do you remove the date and time, and your name above the printing area?

    • @OpenJavaScript
      @OpenJavaScript  2 роки тому +1

      I think this is actually being added by your system/browser, not JavaScript or CSS.
      To remove these, you can go to the print preview settings. I managed to removed them by disabling header and footer.

  • @_Eyesha
    @_Eyesha 2 роки тому +1

    How do you print body background image ?? I need to learn it for my computer class --

    • @OpenJavaScript
      @OpenJavaScript  2 роки тому

      You could try setting the printing area to the background image:
      @media print {
      body {
      content:url(imgs/bg.png);
      }
      }

  • @ngetsophea8812
    @ngetsophea8812 10 місяців тому

    I placed the code in a Wordpress Admin Dashboard to test printing. It returned a blank page.

  • @vartulo7361
    @vartulo7361 Рік тому

    انا استخدم نفس الخاصية ولكن احتاج الى جعل الخصائص يتغير عندا استخدام الهاتف اتناء فتح نافدة الطباعة

  • @geetikarijhwani4628
    @geetikarijhwani4628 2 роки тому +2

    not working for me
    even after writing display: block code for the area to be printed, print preview is displaying blank page

    • @OpenJavaScript
      @OpenJavaScript  2 роки тому +1

      Hard for me to diagnose without seeing your code but maybe this source code will help you:
      openjavascript.info/2022/05/17/printing-a-page-or-part-of-a-page-using-javascript/

    • @geetikarijhwani4628
      @geetikarijhwani4628 2 роки тому +2

      @@OpenJavaScript visibility property worked for me instead of display. Thanks for your reply

    • @OpenJavaScript
      @OpenJavaScript  2 роки тому

      @@geetikarijhwani4628 Glad you found the solution! Good luck.

    • @souha3233
      @souha3233 2 місяці тому

      @@geetikarijhwani4628 it did not work for me do u have any similar problems like this i click on print and nothing happens

  • @Kaushik-f9h
    @Kaushik-f9h 22 дні тому

    Bro i want to skip preview step directly make print how can i make

  • @crakton
    @crakton Рік тому

    thank you very much

  • @missakaabeyrathne8140
    @missakaabeyrathne8140 Рік тому +1

    Thank you 👍👍

  • @RedzGringo
    @RedzGringo 2 роки тому +1

    how can i print the backgroundcolor too?

    • @OpenJavaScript
      @OpenJavaScript  2 роки тому

      Try adding the following CSS to the print view:
      .print-area {
      background-color: red;
      -webkit-print-color-adjust: exact;
      }

  • @souha3233
    @souha3233 2 місяці тому

    it did not work for me i do not know why? this is my code clikc on reply to see it

  • @avaniverma4186
    @avaniverma4186 Місяць тому

    Not working showing blank page in print preview

  • @kvng_Edwin
    @kvng_Edwin 2 роки тому +1

    This worked for me not exactly but it worked

    • @OpenJavaScript
      @OpenJavaScript  2 роки тому

      What was the issue?
      Always happy to look into these and update if needed...

  • @mr.RAND5584
    @mr.RAND5584 Рік тому +1

    How to change paper size using js?

    • @OpenJavaScript
      @OpenJavaScript  Рік тому

      This cannot be done with JS as the window.print() function does not accept any arguments where you could specify options.
      However, you can do it with CSS by setting a size value for the page:
      @page {
      size: A4;
      }
      This solution works in all major browsers except Firefox. For Firefox, I'm not sure a workaround exists unfortunately. But if you find one, be sure to post here as maybe it would be useful to someone else.

  • @abdullahsoomro6238
    @abdullahsoomro6238 2 роки тому

    Thank you for the video

  • @pom8617
    @pom8617 Рік тому +1

    how to new page on div

    • @OpenJavaScript
      @OpenJavaScript  Рік тому

      If you want to set the content of a page to appear in a div like this:

  • @FlamingoFliez
    @FlamingoFliez 8 місяців тому

    can anyone explain me #content, #content * thing?

  • @raymarkado2526
    @raymarkado2526 Рік тому +1

    How to print scrollable div?

    • @OpenJavaScript
      @OpenJavaScript  Рік тому

      If you haven't already, you could try setting the following inside @media print {}
      {
      #scrollableDiv{
      width: 100%;
      height: 100%;
      }

    • @raymarkado2526
      @raymarkado2526 Рік тому +1

      @@OpenJavaScript How about how to remove header and footer, if found the solution on chrome but not in other browser like Mozilla, Edge etc?

    • @OpenJavaScript
      @OpenJavaScript  Рік тому

      @@raymarkado2526 I would not remove anything from the page but set the header and footer CSS display value to none inside @media print{}.

    • @raymarkado2526
      @raymarkado2526 Рік тому

      @@OpenJavaScript It seems that @media print only works on chrome but not in other browser for me I tried to set width and height to 100% and display none for header and footer but then again it all works on on chrome, can you upload a tutorial on how to do it though?

  • @jiarongteh513
    @jiarongteh513 3 місяці тому

    how about ios

  • @mobilephotography123
    @mobilephotography123 3 місяці тому +1

    vs code font name

  • @drakhellfallen6365
    @drakhellfallen6365 Рік тому +1

    how to remove the print button from printable

    • @OpenJavaScript
      @OpenJavaScript  Рік тому

      Set the button to not be displayed inside the print media media query as in the code below OR place the button somewhere else in the DOM that is outside the print-area:
      @media print {
      #printBtn {
      display: none;
      }
      body * {
      display: none;
      }
      .print-area, .print-area * {
      display: block;
      }
      }

    • @drakhellfallen6365
      @drakhellfallen6365 Рік тому +1

      @@OpenJavaScript thnx a lot

    • @OpenJavaScript
      @OpenJavaScript  Рік тому

      @@drakhellfallen6365 Welcome!

  • @learnwithrudy7179
    @learnwithrudy7179 2 роки тому

    thanks bro

  • @porkyminch5131
    @porkyminch5131 9 місяців тому

    If you want color, add this:
    -webkit-print-color-adjust: exact;

  • @artcode2802
    @artcode2802 2 роки тому +1

    it doesn't work

    • @OpenJavaScript
      @OpenJavaScript  2 роки тому

      Thanks for the feedback.
      I've just posted the source code and a live working version on my blog. Maybe this helps you out:
      openjavascript.info/2022/05/17/printing-a-page-or-part-of-a-page-using-javascript/

    • @artcode2802
      @artcode2802 2 роки тому

      @@OpenJavaScript thank you!! it's working now ;)

  • @mohamadalkhalafalbarho5841
    @mohamadalkhalafalbarho5841 Рік тому +1

    اااااا

    • @OpenJavaScript
      @OpenJavaScript  Рік тому

      Not sure how to respond 😂

    • @mohamadalkhalafalbarho5841
      @mohamadalkhalafalbarho5841 Рік тому +1

      @@OpenJavaScript My comment to know that I watched this video
      Do not watch it again
      I love javascript and your dedicated channel
      May God reward you for your content
      And please organize playlists

    • @OpenJavaScript
      @OpenJavaScript  Рік тому

      @@mohamadalkhalafalbarho5841 Organized playlists coming soon!