0

The title is obvious i think. This is where i got some infos :

Provide Print functionality in ASP.NET MVC 4

How do I hide an element when printing a web page?

first, css :

@media print
{
    .no-print, .no-print *
    {
        display: none !important;
    }
}

so, in my cshtml :

<td style="max-width: 100px;"><a href="javascript:window.print()" class="btn btn-default">Print</a>

and now things i don't want to print :

<div style="top:120px;left:10px;position:absolute;" class="no-print"></div>

I also tried class="noPrint"

All's good, exept in the preview before printing i still see my menu...so i guess it WILL be printed... I use chrome.

Any idea ?

Community
  • 1
  • 1
Antoine Pelletier
  • 3,164
  • 3
  • 40
  • 62

2 Answers2

1

With the information you've provided, I can't see any reason why the print media queries wouldn't execute and prevent the menu from being printed. Take a look at the fiddle that exposes this markup:

<td style="max-width: 100px;">
  <a href="javascript:window.print()" class="btn btn-default">Print</a>
</td>

<div style="top:120px;left:10px;position:absolute;" class="no-print">
  Won't print this!
  <span>Or this!</span>
</div>

It shows the basic elements you provided, and printing appears to hide the elements correctly.

Could you provide a little more detail? Maybe something else is going on that's exposing those elements or maybe the CSS hasn't been saved- something along these lines.

1

I think this question may have to do with conflicting '@' symbols. I saw in another SO post they talked about the @@ over-ride and said it needed a space after the double @@. For me, that didn't work with the space, but this worked:

    <style>
        @@media print
        {
            .no-print { display: none !important; };
        }
    </style>

I then added the .no-print class where it was needed.

Dharman
  • 30,962
  • 25
  • 85
  • 135