You are about to read a tutorial written by a member of this website. Although the author probably invested every effort to make this tutorial's content correct, we can not guarantee that the information on this page is 100% exact. Please keep this in mind when reading this tutorial. ;o)
Thanks to this course, you'll be able to print only a page's content. It's quite useful to save the surfers' ink cartdriges
Code: HTML 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 | <!DOCTYPE html PUBLIC "-//W3C//DTD xHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<title>My great website</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<div id="header"> <!-- Header -->
</div>
<div id="menu"> <!-- Menus -->
</div>
<div id="body"> <!-- Body -->
</div>
<div id="footer"> <!-- Footer -->
</div>
</body>
</html>
|
So, we want to print what is between
<div id="body"> and
</div>
To do it properly, we're going to need a style sheet. We're going to call it
print.css
Don't give another name to the style sheet, either the CSS won't be taken into account, as we are going to put this line in the XHTML page :
<link rel="stylesheet" type="text/css" media="print" href="print.css" />
First : put a white background, and the text color : black. You can also give the text size for the whole page.
Code: CSS1
2
3
4
5
6 | body
{
background-color:#FFFFFF;
color:#000000;
font-size:12pt;
}
|
Then, there is to hide the elements you don't want (such as the header, the menu and the footer). We're going to use the
display property with the
none value.
Code: CSS 1
2
3
4
5
6
7
8
9
10
11
12
13
14 | #header
{
display:none;
}
#menu
{
display:none;
}
#footer
{
display:none;
}
|
Then, the color of the body's background must be white.
Code: CSS1
2
3
4 | #body
{
background-color:#FFFFFF;
}
|
Here is the whole
print.css file :
Code: CSS 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 | body
{
background-color:#FFFFFF;
color:#000000;
font-size:12pt;
}
// Beginning of the part NOT TO print
#header
{
display:none;
}
#menu
{
display:none;
}
#footer
{
display:none;
}
// End of the part NOT TO print
// Beginning of the part to print
#body
{
background-color:#FFFFFF;
}
// End of the part to print
|
Here is the main line to write to be able to print the content.
Code: HTML1 | <link rel="stylesheet" type="text/css" media="print" href="print.css" />
|
Here is a JavaScript link to print :
Code: HTML1 | <a href="#" onclick="javascript:window.print()">Print this page</a>
|
The whole code of the page :
Code: HTML 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >
<head>
<title>Print only the content</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="css.css" title="main CSS file with header, menu and footer" /><!-- Basic CSS -->
<link rel="stylesheet" type="text/css" media="print" href="print.css" /><!-- CSS file used to print -->
</head>
<body>
<div id="header"><!-- the header --></div>
<div id="menu"><!-- the menu --></div>
<div id="body">
<p>
<a href="#" onclick="javascript:window.print()">Print this page</a><br />
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec molestie. Sed aliquam sem ut arcu. Phasellus sollicitudin. Vestibulum condimentum facilisis nulla. In hac habitasse platea dictumst. Nulla nonummy. Cras quis libero. Cras venenatis. Aliquam posuere lobortis pede. Nullam fringilla urna id leo. Praesent aliquet pretium erat. Praesent non odio. Pellentesque a magna a mauris vulputate lacinia. Aenean viverra. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Aliquam lacus. Mauris magna eros, semper a, tempor et, rutrum et, tortor.
</p>
</div>
<div id="footer"><!-- The footer --></div>
</body>
</html>
|
Now you know how to do it for your website;)
This course is over. It's my first one, and I hope you've understood everything !