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:

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.

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:

The property margin combines the following four properties, which can also be set individually for more precision:

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.

  

 

 

 

back to top