Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide form controls when printing an HTML page with CSS

Tags:

Certain HTML form elements have extra UI attached to them, like the up/down arrows on number. When printing the page however, those buttons are no longer needed, as the user can not interact with them in print form.

Text boxes are easy to deal with:

@media print {
    input {
        border: none;
        border-bottom: 2px solid #000000;
    }
}

Makes them print quite nicely, as a line with text on it. Just like a form one would fill out by hand. However doing the same for inputs like number leaves you with those nasty up/down arrows:

HTML <input type="number">

And then there are even less useful printouts, like range, which means nothing when on a page:

HTML <input type="range"

Is there any way to get around this? Any way to style that portion of the element to be invisible, but still see the value/text?

I realize one could swap out the type="" attribute with JS, or have another element holding the value to be displayed on print, but if there is a solution that can be done with CSS only, that would be superior.

like image 659
zeel Avatar asked May 05 '13 06:05

zeel


People also ask

How do I hide the elements when printing a website using CSS?

To hide the element, add “display:none” to the element with CSS.

How do I hide print button when printing?

The media query is used to hide an element when printing web pages. Use @media print query and set the visibility hidden to that element that needs to hide at printing.

How do you hide a form in HTML?

The <input type="hidden"> defines a hidden input field. A hidden field lets web developers include data that cannot be seen or modified by users when a form is submitted. A hidden field often stores what database record that needs to be updated when the form is submitted.

How do you hide the form of an element?

To hide an element, set the style display property to “none”. document. getElementById("element").


1 Answers

You can try to hide specific elements with CSS selectors

@media print {
    input[type=range] {
        display: none;
    }
}

However, to hide the arrows in a number element, perhaps you could try to put 2 elements instead, 1 text and 1 number, and then display the number when in screen mode, while the text is hidden, and vice-versa in print mode

@media print {
    input[type=text] {
        display: inline-block;
        border:none;
        border-bottom:2px solid #000;
        etc..
    }
    input[type=number] {
        display: none;
    }
}
@media screen {
    input[type=number] {
        display: inline-block;
    }
    input[type=text] {
        display: none;
    }
}

Something similar can be done for other form elements. It will depend on your implementation if you can use this method.

like image 155
Opifexer Avatar answered Sep 18 '22 11:09

Opifexer