Cascading Style Sheets (CSS)


Einordnung

Layouts für Web-Publishing

Trennung von Struktur und Präsentation wird möglich


Grundkonzepte

Elemente für die kein Stil definiert wird, werden mit dem Browser Default-Stil dargestellt.

Verknüpfung mit HTML

Element-Typen

Block-Elemente
BLOCKQUOTE, BR, DD, DL, DIV, DT, Hi, HR, HTML, LI, OBJECT, OL, P, PRE, UL
Inline-Elemente
A, EM, I, IMG, SPAN, STRONG, TT
Unsichtbare Elemente
LINK, META, STYLE, TITLE

Auswahl mit Bezeichnern

Algorithmus der Cascadierung

  1. entsprechend dem Selektor oder Vererbung

  2. entsprechend dem Gewicht: !important

  3. entsprechend dem Ursprung: Autor, Benutzer, Browser

  4. entsprechend der Detail-Level (specificity)

  5. entsprechend der Reihenfolge


Layout

Box-Eigenschaften: Füllung (padding), Rahmen (border), Rand (margin)

Box mit was drin

Schriften

    P { font-family: monospace; 
        font-size: x-large; }

So siehts aus.

Farben

    P { color: blue }

Schwarz, Hellgrau, Grau, Weiß, Dunkelrot, Rot, Purpur, helles Purpur, Grün, Hellgrün, dunkles Grün, Gelb, Dunkelblau, Blau, Blaugrün, helles Grünblau (Cyan).

weitere Eigenschaften

Beispiele vom W3C

HTML 2.0 in CSS1:

  /* Copyright (c) 1998 W3C */
  BODY { 
    margin: 1em;
    font-family: serif;
    line-height: 1.1;
    background: white;
    color: black; 
  }

  H1, H2, H3, H4, H5, H6, P, UL, OL, DIR, 
  MENU, DIV, DT, DD, ADDRESS, BLOCKQUOTE, 
  PRE, BR, HR { display: block }

  B, STRONG, I, EM, CITE, VAR, TT, CODE, KBD, SAMP, 
  IMG, SPAN { display: inline }

  LI { display: list-item }

  H1, H2, H3, H4 { margin-top: 1em; margin-bottom: 1em }
  H5, H6 { margin-top: 1em }
  H1 { text-align: center }
  H1, H2, H4, H6 { font-weight: bold }
  H3, H5 { font-style: italic }

  H1 { font-size: xx-large }
  H2 { font-size: x-large }
  H3 { font-size: large }

  B, STRONG { font-weight: bolder } /*relative to the parent*/
  I, CITE, EM, VAR, ADDRESS, BLOCKQUOTE { font-style: italic }
  PRE, TT, CODE, KBD, SAMP { font-family: monospace }

  PRE { white-space: pre }

  ADDRESS { margin-left: 3em }
  BLOCKQUOTE { margin-left: 3em; margin-right: 3em }

  UL, DIR { list-style: disc }
  OL { list-style: decimal }
  MENU { margin: 0 }              /* tight formatting */
  LI { margin-left: 3em }

  DT { margin-bottom: 0 }
  DD { margin-top: 0; margin-left: 3em }

  HR { border-top: solid } 

  A:link { color: blue }          /* unvisited link */
  A:visited { color: red }        /* visited links */
  A:active { color: lime }        /* active links */

  /* setting the anchor border around IMG elements
     requires contextual selectors */
  A:link IMG { border: 2px solid blue }
  A:visited IMG { border: 2px solid red }
  A:active IMG { border: 2px solid lime }

Moderner Stil: Seite, Style Sheet Inhalt:

    /* The W3C Core Styles Copyright (c) 1998 W3C */
    H1, H2, H3, H4, H5, H6, TH, DT { 
        font-family: Tahoma, Verdana, 
                     'Myriad Web', Syntax, sans-serif; 
        }

    P, DIV, UL, DL, DT, DD, PRE, OL, LI, 
    BLOCKQUOTE, ADDRESS {
        color: black;
        }

    A, ADDRESS, BLOCKQUOTE, BODY, CITE, CODE, DD, 
    DEL, DFN, DIV, DL, DT, EM, FORM, H1, H2, H3, 
    H4, H5, H6, IFRAME, IMG, KBD, LI, OBJECT, OL, 
    P, Q, SAMP, SMALL, SPAN, STRONG, SUB, SUP, UL, 
    VAR, APPLET, BIG, CENTER, DIR, FONT, HR, MENU, 
    PRE, ABBR, ACRONYM, BDO, BUTTON, FIELDSET, 
    INS, LABEL, LEGEND {       
        word-spacing:    normal;
        letter-spacing:  normal;
        text-transform:  none;
        text-decoration: none;
        border-color:    #036;
        border-style:    none;
        }       

    BODY {       
        color:      black;
        background: white;
        }       

    EM  {       
        font-style:  normal;
        font-weight: bold;
        color:       black;
        }       

    STRONG {       
        font-style:  italic;
        background:  white;
        font-weight: bold;
        color:       black;
        }       

    EM STRONG, STRONG EM    {       
        text-transform: uppercase;
        font-style:     normal;
        font-weight:    bolder;
        background:     white;
        color:          black;
        }       

    B {
        font-weight: bold;
        }

    I {
        font-style: italic;
        }

    .warning {       
        text-transform: none;
        font-style:     normal;
        font-weight:    bolder;
        background:     yellow;
        color:          black;
        }       

    DEL {       
        text-decoration: line-through;
        background:      #F66;
        }       

    INS {       
        text-decoration: none;
        background:      yellow;
        }       

    VAR, CITE, DFN, .note {       
        font-style: italic;
        }       

    ADDRESS {       
        font-style:     normal;
        letter-spacing: .1em;
        }       

    ACRONYM {       
        font-variant:   small-caps;
        letter-spacing: 0.1em;
        }       

    H1, H2, H3, H4, H5, H6, DT, LEGEND, 
    CAPTION, TH, THEAD, TFOOT {       
        color:      #036;
        background: white;
        }       

    HR {       
        color:  #036;
        }       

    #colophon {       
        display: none;
        }       

    CAPTION, COL, COLGROUP, TABLE, TBODY, TD, TR {       
        color:           black;
        text-decoration: none;
        border-color:    black;
        border-style:    none;
        background:      white;
        }       

    A:link {       
        text-decoration: none;
        font-weight:     bold;
        color:           #F30;
        background:      white;
        }       

    A:visited {       
        text-decoration: none;
        font-weight:     bold;
        color:           #996;
        background:      white;
        }       

    A:active {       
        text-decoration: none;
        font-weight:     bold;
        color:           #F30;
        background:      #FF0;
        }       

    A:hover {       
        text-decoration: none;
        color:           #F30;
        background:      white;
        }       

    A.offsite {       
        text-decoration: none;
        font-weight:     normal;
        color:           #C30;
        background:      white;
        }       

    BODY {
        margin-top:     1.58em;
        margin-left:    1.58em;
        margin-right:   1ex;
        margin-bottom:  1.58em;
        padding-top:    0;
        padding-left:   0;
        padding-right:  0;
        padding-bottom: 0;
        border-top:     0;
        border-left:    0;
        border-bottom:  0;
        border-right:   0;
        width:          auto;
        }

Style Sheet für HTML 4.0 in CSS2

Positionierung (CSS2)

Implementiert in Netscape 4.x, IE 4.0

Animation mit HTML, CSS, JavaScript und DOM

Dateien: HTML, CSS, JavaScript.

weitere Beispiele


Ausblick

Schwachstellen von CSS1

Alles was mit CSS1 machbar ist, ist in HTML 4.0 "deprecated", d.h. nicht mehr empfohlen, zur Nicht-Benutzung empfohlen.


© Universität Mannheim, Rechenzentrum, 1998/1999.