Nossos voluntários ainda não traduziram este artigo para o Português (do Brasil) . Junte-se a nós e ajude a fazer o trabalho!
Tables
This is the 14th and last section of Part I of the CSS Getting Started tutorial. Many of the pages in this tutorial have focused on the CSS properties and values that you can use to specify how to display a document. This page looks again at the purpose and structure of CSS stylesheets.
Information: Media
The purpose of CSS is to specify how documents are presented to the user. Presentation can take more than one form.
For example, you are probably reading this page on a display device. But you might also want to project it on a screen for a large audience, or print it. These different media can have different characteristics. CSS provides ways to present a document differently in different media.
To specify rules that are specific to a type of media, use @media
followed by the media type, followed by curly braces that enclose the rules.
A document on a web site has a navigation area to allow the user to move around the site.
In the markup language, the navigation area's parent element has the id nav-area
. (In HTML5, this can be marked up with the <nav>
element instead of <div>
with an id attribute.)
When the document is printed the navigation area has no purpose, so the stylesheet removes it completely:
@media print { #nav-area {display: none;} }
Some of the common media types are:
screen |
Color computer display |
print |
Paged media |
projection |
Projected display |
all |
All media (the default) |
There are other ways to specify the media type of a set of rules.
The document's markup language might allow the media type to be specified when the stylesheet is linked to the document. For example, in HTML you can optionally specify the media type with a media
attribute in the LINK
tag.
In CSS you can use @import
at the start of a stylesheet to import another stylesheet from a URL, optionally specifying the media type.
By using these techniques you can separate style rules for different media types into different files. This is sometimes a useful way to structure your stylesheets.
For full details of media types, see Media in the CSS Specification.
There are more examples of the display
property in a later page in this tutorial: XML data.
Printing
CSS has some specific support for printing and for paged media in general.
A @page
rule can set the page margins. For double-sided printing, you can specify the margins separately for @page:left
and @page:right
.
For print media, you normally use appropriate length units like inches (in
) and points (pt
= 1/72 inch), or centimeters (cm
) and millimeters (mm
). It is equally appropriate to use ems (em
) to match the font size, and percentages (%
).
You can control how the content of the document breaks across page boundaries, by using the page-break-before
, page-break-after
and page-break-inside
properties.
This rule sets the page margins to one inch on all four sides:
@page {margin: 1in;}
This rule ensures that every H1 element starts on a new page:
h1 {page-break-before: always;}
For full details of CSS support for paged media, see Paged media in the CSS Specification.
Like other features of CSS, printing depends on your browser and its settings. For example, your Mozilla browser supplies default margins, headers and footers when it prints. When other users print your document, you probably cannot predict the browser and the settings that they will use, so you probably cannot control the results completely.
User interfaces
CSS has some special properties for devices that support a user interface, like computer displays. These make the document's appearance change dynamically as the user works with the interface.
There is no special media type for devices with user interfaces.
There are five special selectors:
Selector | Selects |
E |
Any E element that has the pointer over it |
E |
Any E element that has keyboard focus |
E |
The E element that is involved in the current user action |
E |
Any E element that is a hyperlink to a URL that the user has not visited recently |
E |
Any E element that is a hyperlink to a URL that the user has visited recently |
Note: The information that can be obtained from the :visited selector is restricted in Gecko 2.0. See Privacy and the :visited selector for more details.
The cursor
property specifies the shape of the pointer: Some of the common shapes are as follows. Place your mouse over the items in this list to see the actual pointer shapes in your browser:
Selector | Selects |
pointer |
Indicating a link |
wait |
Indicating that the program cannot accept input |
progress |
Indicating that the program is working, but can still accept input |
default |
The default (usually an arrow) |
An outline
property creates an outline that is often used to indicate keyboard focus. Its values are similar to the border
property, except that you cannot specify individual sides.
Some other features of user interfaces are implemented using attributes, in the normal way. For example, an element that is disabled or read-only has the disabled attribute or the readonly attribute. Selectors can specify these attributes like any other attributes, by using square brackets: [disabled]
or [readonly]
.
These rules specify styles for a button that changes dynamically as the user interacts with it:
.green-button { background-color:#cec; color:#black; border:2px outset #cec; } .green-button[disabled] { background-color:#cdc; color:#777; } .green-button:active { border-style: inset; }
This wiki does not support a user interface on the page, so these buttons do not "click". Here are some static images to illustrate the idea:
|
A fully functional button also has a dark outline around the entire button when it is the default, and a dotted outline on the face of the button when it has keyboard focus. It might also have a hover effect when the pointer is over it.
For more information about user interfaces in CSS, see User interface in the CSS Specification.
There is an example of Mozilla's markup language for user interfaces, XUL, in Part II of this tutorial.
Action: Printing a document
- Make a new HTML document,
doc4.html
. Copy and paste the content from here:<!DOCTYPE html> <html> <head> <title>Print sample</title> <link rel="stylesheet" href="style4.css"> </head> <body> <h1>Section A</h1> <p>This is the first section...</p> <h1>Section B</h1> <p>This is the second section...</p> <div id="print-head"> Heading for paged media </div> <div id="print-foot"> Page: </div> </body> </html>
- Make a new stylesheet,
style4.css
. Copy and paste the content from here:/*** Print sample ***/ /* defaults for screen */ #print-head, #print-foot { display: none; } /* print only */ @media print { h1 { page-break-before: always; padding-top: 2em; } h1:first-child { page-break-before: avoid; counter-reset: page; } #print-head { display: block; position: fixed; top: 0pt; left:0pt; right: 0pt; font-size: 200%; text-align: center; } #print-foot { display: block; position: fixed; bottom: 0pt; right: 0pt; font-size: 200%; } #print-foot:after { content: counter(page); counter-increment: page; } } /* end print only */
- View this document in your browser; it uses your browser's default style.
- Print (or print preview) the document; the stylesheet places each section on a separate page, and it adds a header and footer to each page. If your browser supports counters, it adds a page number in the footer.
Heading for paged mediaSection AThis is the first section...Page: 1Heading for paged mediaSection BThis is the second section...Page: 2
Move the print-specific style rules to a separate CSS file.
Read the Make the headings turn blue when the mouse pointer is over them. |
See solutions to these challenges.
What next?
If you had difficulty understanding this page, or if you have other comments about it, please contribute to its Discussion page.
So far, all the style rules in this tutorial have been specified in files. The rules and their values are fixed. The next page describes how you can change rules dynamically by using a programming language: JavaScript