Skip to content

CSS


Was ist CSS?

  • CSS = Cascading Style Sheets
  • Verantwortlich f�r Design & Layout von Webseiten
  • Getrennt von HTML (Struktur vs. Darstellung)
  • HTML = Struktur, CSS = Aussehen
css
h1 {
    color: blue;
    font-size: 32px;
}

CSS einbinden

Externe Datei (empfohlen):

html
<link rel="stylesheet" href="styles.css">

Internes CSS:

html
<style>
    p { color: blue; }
</style>

Inline CSS:

html
<p style="color: blue;">Text</p>

CSS Syntax

css
selektor {
    eigenschaft: wert;
}

Beispiel:

css
h1 {
    color: red;
    font-size: 32px;
    text-align: center;
}

Selektoren - Element

  • Spricht alle Elemente eines Typs (Tag) an
css
p {
    color: green;
}

h1 {
    font-size: 36px;
}

a {
    text-decoration: none;
}

Selektoren - Klassen

  • Mit . ansprechen
  • Wiederverwendbar
  • Element kann mehrere Klassen haben
html
<p class="wichtig highlight">Text</p>
css
.wichtig {
    color: red;
    font-weight: bold;
}

.highlight {
    background-color: yellow;
}

Selektoren - IDs

  • Mit # ansprechen
  • Nur einmal pro Seite
  • H�here Priorit�t als Klassen
html
<div id="header">Header</div>
css
#header {
    background-color: navy;
    color: white;
    padding: 20px;
}

Kombinierte Selektoren

Mehrere Elemente:

css
h1, h2, h3 {
    font-family: Arial, sans-serif;
}

Nachfahren ("Kinder"):

css
.container p {
    margin: 10px;
}

Direkte Kinder:

css
.container > p {
    color: blue;
}

Pseudo-Klassen

css
a:hover {
    color: red;
}

input:focus {
    border: 2px solid blue;
}

li:first-child {
    font-weight: bold;
}

tr:nth-child(even) {
    background-color: #f0f0f0;
}

Farben

Farbnamen:

css
color: red;

Hexadezimal:

css
color: #FF0000;
color: #333;  /* Kurzform */

RGB:

css
color: rgb(255, 0, 0);

RGBA (mit Transparenz):

css
background-color: rgba(255, 0, 0, 0.5);

Schriftarten

Font-Family:

  • Fallback falls die Schrift auf dem System nicht vorhanden ist
css
font-family: Arial, Helvetica, sans-serif;

Font-Size:

css
font-size: 16px;
font-size: 1.5em;   /* Relativ */
font-size: 1.5rem;  /* Root relativ */

Font-Weight & Style:

css
font-weight: bold;
font-style: italic;
text-decoration: underline;

Text-Formatierung

css
text-align: center;
line-height: 1.5;
letter-spacing: 2px;
word-spacing: 5px;

text-transform: uppercase;
text-transform: lowercase;
text-transform: capitalize;

text-shadow: 2px 2px 4px rgba(0,0,0,0.5);

Box Model

�������������������������
      Margin             
  �������������������  
       Border          
    �������������    
       Padding       
      �������      
      Content      
      �������      
    �������������    
  �������������������  
�������������������������

Padding & Margin

css
/* Alle Seiten */
padding: 20px;
margin: 20px;

/* Vertikal | Horizontal */
padding: 10px 20px;

/* Oben | Rechts | Unten | Links */
padding: 10px 20px 15px 25px;

/* Einzelne Seiten */
padding-top: 10px;
margin-bottom: 15px;

/* Zentrieren */
margin: 0 auto;

Border

Grundform:

css
border: 2px solid black;

Eigenschaften:

css
border-width: 2px;
border-style: solid;  /* dashed, dotted, double */
border-color: black;

Abgerundete Ecken:

css
border-radius: 10px;
border-radius: 50%;  /* Kreis */

Width & Height

css
width: 300px;
height: 200px;
width: 50%;

min-width: 200px;
max-width: 800px;
min-height: 100px;
max-height: 500px;

Box-Sizing (Best Practice):

css
* {
    box-sizing: border-box;
}

Background Color & Image

css
background-color: #f0f0f0;

background-image: url('bild.jpg');
background-repeat: no-repeat;
background-position: center;
background-size: cover;
background-attachment: fixed;

Kurzform:

css
background: #f0f0f0 url('bild.jpg') no-repeat center/cover;

Gradients

Linear:

css
background: linear-gradient(to right, red, blue);
background: linear-gradient(45deg, red, yellow, green);

Radial:

css
background: radial-gradient(circle, red, blue);

Bilder stylen

css
img {
    width: 100%;
    max-width: 600px;
    height: auto;

    display: block;
    margin: 0 auto;

    border-radius: 10px;
    box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}

Object-Fit:

css
img {
    width: 300px;
    height: 200px;
    object-fit: cover;    /* F�llt, schneidet ab */
    object-fit: contain;  /* Ganzes Bild sichtbar */
}

Flexbox Grundlagen

  • Modernes Layout-System
  • Flexible, responsive Designs
css
.container {
    display: flex;
}

Flex-Direction:

css
flex-direction: row;            /* Default */
flex-direction: row-reverse;
flex-direction: column;
flex-direction: column-reverse;

Justify-Content

  • Ausrichtung auf Hauptachse (horizontal bei row)
css
.container {
    display: flex;
    justify-content: flex-start;    /* Default */
    justify-content: flex-end;
    justify-content: center;
    justify-content: space-between;
    justify-content: space-around;
    justify-content: space-evenly;
}

Align-Items

  • Ausrichtung auf Querachse (vertikal bei row)
css
.container {
    display: flex;
    align-items: stretch;     /* Default */
    align-items: flex-start;
    align-items: flex-end;
    align-items: center;
}

Zentrieren:

css
.container {
    display: flex;
    justify-content: center;
    align-items: center;
}

Flex Wrap & Gap

css
.container {
    display: flex;
    flex-wrap: wrap;       /* Umbricht auf neue Zeile */
    gap: 20px;             /* Abstand zwischen Items */
    row-gap: 20px;
    column-gap: 10px;
}

Flex Items

css
.item {
    flex-grow: 1;       /* Nimmt verf�gbaren Platz */
    flex-shrink: 0;     /* Schrumpft nicht */
    flex-basis: 200px;  /* Ausgangsgr��e */
}

Kurzform:

css
.item {
    flex: 1;  /* grow shrink basis */
}

Flexbox Beispiel - 3 Spalten

html
<div class="container">
    <div class="column">Spalte 1</div>
    <div class="column">Spalte 2</div>
    <div class="column">Spalte 3</div>
</div>
css
.container {
    display: flex;
    gap: 20px;
}

.column {
    flex: 1;
    padding: 20px;
    background-color: #f0f0f0;
}

Flexbox Beispiel - Navigation

html
<nav class="navbar">
    <div class="logo">Logo</div>
    <div class="nav-links">
        <a href="#">Home</a>
        <a href="#">�ber</a>
    </div>
</nav>
css
.navbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.nav-links {
    display: flex;
    gap: 20px;
}

Transitions

  • Sanfte �berg�nge bei Zustands�nderungen

Grundform:

css
transition: property duration timing-function delay;

Beispiel:

css
.button {
    background-color: blue;
    transition: background-color 0.3s ease;
}

.button:hover {
    background-color: red;
}

Transition Timing

css
transition: all 0.3s linear;      /* Gleichm��ig */
transition: all 0.3s ease;        /* Default */
transition: all 0.3s ease-in;     /* Langsamer Start */
transition: all 0.3s ease-out;    /* Langsames Ende */
transition: all 0.3s ease-in-out; /* Beides */

Transition Beispiel - Button

css
.button {
    background-color: #007bff;
    padding: 10px 20px;
    transition: all 0.3s ease;
}

.button:hover {
    background-color: #0056b3;
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}

Animations

  • Komplexere Bewegungen mit Keyframes

Keyframes definieren:

css
@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

Anwenden:

css
.box {
    animation: fadeIn 1s ease-in;
}

Keyframes mit Prozenten

css
@keyframes slideIn {
    0% {
        transform: translateX(-100%);
    }
    50% {
        transform: translateX(10%);
    }
    100% {
        transform: translateX(0);
    }
}

Animation Eigenschaften

css
.element {
    animation-name: fadeIn;
    animation-duration: 2s;
    animation-timing-function: ease;
    animation-delay: 0.5s;
    animation-iteration-count: infinite;
    animation-direction: alternate;
    animation-fill-mode: forwards;
}

Animation Beispiel - Pulse

css
@keyframes pulse {
    0% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.05);
    }
    100% {
        transform: scale(1);
    }
}

.pulse-button {
    animation: pulse 2s ease infinite;
}

Animation Beispiel - Spinner

css
@keyframes rotate {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

.spinner {
    width: 50px;
    height: 50px;
    border: 4px solid #f3f3f3;
    border-top: 4px solid #3498db;
    border-radius: 50%;
    animation: rotate 1s linear infinite;
}

Box-Shadow

css
box-shadow: x y blur spread color;

Beispiele:

css
.card {
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.card:hover {
    box-shadow: 0 8px 16px rgba(0,0,0,0.2);
}

/* Innerer Schatten */
box-shadow: inset 0 2px 4px rgba(0,0,0,0.2);

/* Mehrere Schatten */
box-shadow:
    0 2px 4px rgba(0,0,0,0.1),
    0 4px 8px rgba(0,0,0,0.1);

Text-Shadow

css
text-shadow: x y blur color;

Beispiele:

css
h1 {
    text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}

/* 3D-Effekt */
h1 {
    text-shadow:
        1px 1px 0 #ccc,
        2px 2px 0 #bbb,
        3px 3px 0 #aaa;
}

/* Glow */
h1 {
    text-shadow: 0 0 10px #fff, 0 0 20px #0073e6;
}

Display Property

css
display: block;        /* Ganze Breite, neue Zeile */
display: inline;       /* Nur Inhalt-Breite */
display: inline-block; /* Kombiniert beides */
display: flex;         /* Flexbox */
display: grid;         /* Grid */
display: none;         /* Versteckt */

Position

css
position: static;     /* Default */
position: relative;   /* Relativ zur normalen Position */
position: absolute;   /* Relativ zum Elternelement */
position: fixed;      /* Relativ zum Viewport */
position: sticky;     /* Wechselt zwischen relative/fixed */

Beispiel:

css
.element {
    position: absolute;
    top: 20px;
    right: 30px;
}

Fixed Header Beispiel

css
header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    background-color: white;
    z-index: 1000;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

Responsive Design - Media Queries

css
/* Mobile First */
.container {
    width: 100%;
}

/* Tablets */
@media (min-width: 768px) {
    .container {
        width: 750px;
    }
}

/* Desktop */
@media (min-width: 1024px) {
    .container {
        width: 1000px;
    }
}

Media Query Beispiel

css
/* Mobile */
@media (max-width: 767px) {
    .menu {
        display: none;
    }

    .hero h2 {
        font-size: 24px;
    }
}

/* Desktop */
@media (min-width: 768px) {
    .menu {
        display: flex;
    }
}

CSS Variablen

css
:root {
    --primary-color: #007bff;
    --spacing: 20px;
    --border-radius: 5px;
}

.button {
    background-color: var(--primary-color);
    padding: var(--spacing);
    border-radius: var(--border-radius);
}

CSS Reset

css
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: Arial, sans-serif;
    line-height: 1.6;
}

Best Practices

  • Externe CSS-Dateien verwenden
  • Klassen statt IDs f�r Styling
  • box-sizing: border-box f�r alle Elemente
  • Mobile First Ansatz
  • CSS-Variablen f�r wiederholte Werte
  • Kommentare f�r Struktur
  • Vermeide !important

Vollst�ndiges Beispiel - HTML

html
<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Meine Website</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <header class="header">
        <div class="container">
            <h1>Logo</h1>
            <nav class="nav">
                <a href="#" class="nav-link">Home</a>
                <a href="#" class="nav-link">�ber</a>
            </nav>
        </div>
    </header>

    <main>
        <section class="hero">
            <h2>Willkommen!</h2>
            <button class="button">Start</button>
        </section>
    </main>
</body>
</html>

Vollst�ndiges Beispiel - CSS

css
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary-color: #007bff;
    --spacing: 20px;
}

body {
    font-family: Arial, sans-serif;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 var(--spacing);
}

.header {
    background-color: var(--primary-color);
    color: white;
    padding: var(--spacing) 0;
}

.header .container {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.nav {
    display: flex;
    gap: 20px;
}

.nav-link {
    color: white;
    text-decoration: none;
    transition: opacity 0.3s;
}

.nav-link:hover {
    opacity: 0.8;
}

.hero {
    text-align: center;
    padding: 80px var(--spacing);
    background: linear-gradient(135deg, #667eea, #764ba2);
    color: white;
}

.button {
    background-color: white;
    color: var(--primary-color);
    padding: 12px 30px;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    transition: all 0.3s;
}

.button:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}

Resources


Wichtigste Eigenschaften

KategorieEigenschaften
Textcolor, font-family, font-size, text-align
Boxwidth, height, padding, margin, border
Backgroundbackground-color, background-image
Flexboxdisplay: flex, justify-content, align-items
Positionposition, top, left
Effectsbox-shadow, border-radius, opacity

Informatik & ICT Unterricht Neufeld