CSS media and alternative style sheets tutorial: Difference between revisions

The educational technology and digital learning wiki
Jump to navigation Jump to search
m (Created page with "{{under construction}} {{Web technology tutorial|Intermediate}} <pageby nominor="false" comments="false"/> Category: CSS <div class="tut_goals"> ; Learning goals * Understan...")
 
Line 40: Line 40:
== Introduction ==
== Introduction ==


CSS allows to to create
You can define alternate stylesheets to meet various user preferences. Often, designers define different stylesheets for different media types.
 
CSS allows to to create:
* Stylesheets for different media types (e.g. your screen, a printer or a hand-held device)
* Stylesheets for different media types (e.g. your screen, a printer or a hand-held device)
* Stylesheets for different people (e.g. allow people with poor eyesight to enjoy larger letter)
* Stylesheets for different people (e.g. allow people with poor eyesight to enjoy larger letter)


== Media types ==
== Media types ==
The CSS 2.1 specification distinguishes the following media types
# all - suitable for all devices.
# braille - for braille tactile feedback devices.
# embossed - for paged braille printers.
# handheld - for handheld devices (typically small screen, limited bandwidth).
# print - for paged material and for documents viewed on screen in print preview mode.
# projection - for projected presentations, for example projectors.
# screen - primarily for color computer screens.
# speech - for speech synthesizers. Note: CSS2 had a similar media type called 'aural' # for this purpose.
# tty - for media using a fixed-pitch character grid (such as teletypes, terminals, or portable devices with limited display capabilities).
# tv - Intended for television-type devices (low resolution, color, limited-scrollability screens, sound available).


== Linking several style sheets ==
== Linking several style sheets ==
Firstly, you should remember that you can load several stylesheets in a row. E.g
<source lang="XML">
<link rel="stylesheet" type="text/css" href="default.css" />
<link rel="stylesheet" type="text/css" href="articles.css" />
</source>
There exist three methods for defining alternative styles
* Use link and "alternative stylesheet" to load alternative style sheets
* Use the CSS @import rule to load alternative style sheets
* Use the @media rule within a single stylesheet
=== Alternative stylesheets with the link HTML element ===
(1) You may define alternative stylesheets, e.g. one that uses a bigger font for people with bad eyesight. Most browsers allow users to select from alternative stylesheets. E.g. in Firefox through the ''View->Page Style'' menu.
Now, these stylesheets have to be linked in a special way unless they are media specific. In the example below we define two stylesheets for the screen medium. The 2nd and 3rd are alternate stylesheets that the user can choose.
<source lang="XML">
<link rel="stylesheet" type="text/css" media="screen"
        title="Default style" href="default.css" />
<link rel="alternate stylesheet" type="text/css" media="screen"
        title="Friendly fonts" href="friendly.css" />
<link rel="alternate stylesheet" type="text/css" media="screen"
        title="bigtype" href="big.css" />
</source>
In addition, you should provide JavaScript code that will allow the user to switch style (a typical user may not know how to do this manually). A older very popular and free example is available from [http://www.alistapart.com/articles/alternate/ AlistAPart]. A simpler, more elegant [http://www.alistapart.com/articles/bodyswitchers/ more sophisticated] 2004 version also exists.
=== Alternative stylesheets with the CSS @import rule ===
The @import at-rule allows to specify a media type, i.e. you may use this strategy to load various CSS variants from a single CSS file.
<source lang="CSS">
@import url(print-style.css) print;
</source>
Also, you often will find the ''@import'' CSS at-rule as replacement of the HTML ''link'' tag. E.g. the two following expressions are identical:
(a) Import a style-sheet with @import
<source lang="XML">
<style type="text/css">
@import url(css-intro.css)
</style>
</source>
(b) Import the normal way
<source lang="XML">
<link rel="stylesheet" href="css-intro.css" type="text/css">
</source>
=== Alternative styles with the CSS @media rule ===
Media-specific alternatives also can be defined within a single style sheet using the ''@media'' at-rule.
<source lang="CSS">
@media print {
  body {
    padding: 3cm;
  }
@media screen, projection {
  body {
    padding: 1cm;
  }
}
</source>
== Testing ==
....


== Style sheet switching ==
== Style sheet switching ==
.....

Revision as of 17:44, 19 November 2011

This article or section is currently under construction

In principle, someone is working on it and there should be a better version in a not so distant future.
If you want to modify this page, please discuss it with the person working on it (see the "history")

<pageby nominor="false" comments="false"/>

Learning goals
  • Understand CSS float
  • Be able to create simple layouts using floats
Prerequisite
Concurrent
Moving on
Level and target population
  • Beginners
Teaching materials (Examples)
Remarks
  • This tutorial is intended for students in a field that is technology intensive (e.g. computer applications or educational technology). For people who need less, there exist many easy CSS tutorials on the web. This text is intended for students who also must learn principles and who are willing to learn CSS by doing a project, looking at CSS code and online reference manuals.
  • Ideally, a teacher also should assign a text formatting task, during or before assigning this tutorial for reading).
  • There are some inline examples. However, they had to be created in a away that makes them fit into a wiki page. I.e. we use inline style (something we usually don't advocate) and we also had to add extra styling properties.


Introduction

You can define alternate stylesheets to meet various user preferences. Often, designers define different stylesheets for different media types.

CSS allows to to create:

  • Stylesheets for different media types (e.g. your screen, a printer or a hand-held device)
  • Stylesheets for different people (e.g. allow people with poor eyesight to enjoy larger letter)

Media types

The CSS 2.1 specification distinguishes the following media types

  1. all - suitable for all devices.
  2. braille - for braille tactile feedback devices.
  3. embossed - for paged braille printers.
  4. handheld - for handheld devices (typically small screen, limited bandwidth).
  5. print - for paged material and for documents viewed on screen in print preview mode.
  6. projection - for projected presentations, for example projectors.
  7. screen - primarily for color computer screens.
  8. speech - for speech synthesizers. Note: CSS2 had a similar media type called 'aural' # for this purpose.
  9. tty - for media using a fixed-pitch character grid (such as teletypes, terminals, or portable devices with limited display capabilities).
  10. tv - Intended for television-type devices (low resolution, color, limited-scrollability screens, sound available).

Linking several style sheets

Firstly, you should remember that you can load several stylesheets in a row. E.g

 <link rel="stylesheet" type="text/css" href="default.css" />
 <link rel="stylesheet" type="text/css" href="articles.css" />

There exist three methods for defining alternative styles

  • Use link and "alternative stylesheet" to load alternative style sheets
  • Use the CSS @import rule to load alternative style sheets
  • Use the @media rule within a single stylesheet

Alternative stylesheets with the link HTML element

(1) You may define alternative stylesheets, e.g. one that uses a bigger font for people with bad eyesight. Most browsers allow users to select from alternative stylesheets. E.g. in Firefox through the View->Page Style menu.

Now, these stylesheets have to be linked in a special way unless they are media specific. In the example below we define two stylesheets for the screen medium. The 2nd and 3rd are alternate stylesheets that the user can choose.

 <link rel="stylesheet" type="text/css" media="screen"
        title="Default style" href="default.css" />
 <link rel="alternate stylesheet" type="text/css" media="screen"
        title="Friendly fonts" href="friendly.css" />
 <link rel="alternate stylesheet" type="text/css" media="screen"
        title="bigtype" href="big.css" />

In addition, you should provide JavaScript code that will allow the user to switch style (a typical user may not know how to do this manually). A older very popular and free example is available from AlistAPart. A simpler, more elegant more sophisticated 2004 version also exists.

Alternative stylesheets with the CSS @import rule

The @import at-rule allows to specify a media type, i.e. you may use this strategy to load various CSS variants from a single CSS file.

 @import url(print-style.css) print;

Also, you often will find the @import CSS at-rule as replacement of the HTML link tag. E.g. the two following expressions are identical:

(a) Import a style-sheet with @import

<style type="text/css">
 @import url(css-intro.css)
</style>

(b) Import the normal way

 <link rel="stylesheet" href="css-intro.css" type="text/css">

Alternative styles with the CSS @media rule

Media-specific alternatives also can be defined within a single style sheet using the @media at-rule.

@media print {
  body {
    padding: 3cm;
  }

@media screen, projection {
  body {
    padding: 1cm;
  }
}


Testing

....

Style sheet switching

.....