XML technologies / CSS / Advanced websites with CSS / Miscellaneous CSS topics / At-rules
(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)
W3C’s CSS2 recommendation defines this new rule type, named at-rules because they all begin with @. Their purpose is extensibility: W3C, browser developers or anyone using CSS can define new rule groups with @.
Four groups currently exist:
@import@media@page@font-face
The rule @media defines output options for different media. Browser support remains inconsistent but improves with updates. The rule @media works in most modern browsers, except notably Internet Explorer 5.2 for Macintosh.
W3C defines these media types for the rule @media. The list is not exhaustive: future technologies and output devices will add more.
all(all)aural(speech output)braille(Braille display)embossed(Braille printer)handheld(PDAs and similar devices)print(print output)projection(projector)screen(screen)TTY(text browser)TV(television)
Most directives are self-explanatory except embossed, which targets Braille printers. This HTML example produces clearly different screen and print appearances and demonstrates the syntax of the rule @media.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Anwendung der @media-Regel</title>
<style type="text/css">
@media print {
body {
font-size: 12pt;
font-family: Courier;
}
}
@media screen {
body {
font-size: 36px;
font-family: Arial;
}
}
@media screen, print {
body {
line-height: 1.2;
}
}
</style>
</head>
<body>Probieren Sie aus, ob dieses Beispiel funktioniert und in welchen Browsern es klappt. Ich habe eine @media-Regel für den Ausdruck in Courier in 12 Punkt definiert, eine andere @media-Regel legt die Anzeige auf dem Bildschirm in Arial mit der Höhe von 36 Pixel fest. Auf beiden Geräten wird die Ausgabe mit einem Zeilenabstand von 120 % des Standardwerts erfolgen.</body>
</html>One rule @media can cover multiple media, separated by commas. Two other methods specify media. First, the rule @import accepts media names as parameters:
@import url(bossvoice.css) aural;This imports the stylesheet bossvoice.css and applies it to all aural output media.
The second method is the attribute media of the tag <style>:
<style type="text/css2" media="projection">
body {
color: blue;
background-color: white;
}
</style>For media understanding printed pages, use the at-rule @page to specify dimensions, borders, page breaks and crop marks. (Note: extensive use of the rule @page can be complex. More information appears on W3C’s official page with the rule definition.)
For an 8.5 by 11 centimeter page with a half-centimeter margin, the rule @page should look like this:
@page {
size: 8.5cm 11cm;
margin: 0.5cm;
}The property size accepts explicit sizes or these three keywords:
auto: use the browser’s default page sizelandscape: landscape pageportrait: portrait page
The property margin combines the following four properties, which can also be set individually for more precision:
margin-topmargin-rightmargin-bottommargin-left
The pseudo-classes :first, :left and :right define sizes and margins for cover, left and right pages. This example uses a rule set @page for a two-sided document with a different cover layout:
@page {
margin: 2cm; /* Alle Seitenränder 2cm */
}
@page:first {
margin-top: 10cm; /* Oberer Rand auf der Titelseite 10cm */
}
/* 1cm breitere Seitenränder hin zum Einband */
@page:left {
margin-left: 3cm;
margin-right: 4cm;
}
@page:right
margin-left: 4cm;
margin-right: 3cm;
}CSS2 even controls page breaks, but printed-page control is complex, beyond this book’s scope and most web designers’ focus. We will not explore it further. CSS can nevertheless design printed versions; the required properties are detailed in CSS property reference.
Copyright © 2006 of the German translation, dpunkt.verlag GmbH
You may print the online version for your private use.
Otherwise, this chapter from “CSS − Anspruchsvolle Websites mit Cascading Stylesheets” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
dpunkt.verlag GmbH, Ringstraße 19, 69115 Heidelberg
back to top