← Level 1 – Baumeister

Lektion 10 von 12 ⏱ 35 Minuten

Karten und Fotogalerie

Baue die zwei beliebtesten Bausteine moderner Websites: Inhaltskarten und ein Foto-Raster.

Zeig mir das: eine Karte

Eine Karte ist ein weißes Rechteck mit Schatten, das Bild und Text zusammenhält. Du kennst sie von YouTube, Netflix und Online-Shops. Eine Karte ist nur eine Box mit ein paar Eigenschaften:

<div class="card">
  <img src="images/photo.jpg" alt="Photo description">
  <h3>Card heading</h3>
  <p>Short text for the card.</p>
</div>
.card {
  background-color: white;
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

Zwei neue Dinge:

Zeig mir das: Karten nebeneinander

Jetzt ein Trick, den du schon kennst – Flexbox. Packe die Karten in eine Box und setze sie auf flex:

<div class="cards">
  <div class="card">…</div>
  <div class="card">…</div>
  <div class="card">…</div>
</div>
.cards {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

.cards .card {
  flex: 1;             /* cards fairly share the available space */
  min-width: 200px;    /* but none will ever be narrower than 200px */
}

flex-wrap: wrap + min-width = am Computer sitzen die Karten nebeneinander, am Handy stapeln sie sich. Du hast gerade eine responsive Website gebaut – eine, die sich jedem Bildschirm anpasst.

Jetzt du 💪

Füge deiner Website einen Abschnitt mit Karten hinzu – mindestens drei nebeneinander. Was draufkommt, hängt von deinem Projekt ab:

🏫 Schulwebsite – Lösung (Lehrer-Karten)
<h2>Our Teachers</h2>
<div class="cards">
  <div class="card">
    <h3>Ms. Turner</h3>
    <p>4th grade homeroom. Teaches English and art. Loves cats.</p>
  </div>
  <div class="card">
    <h3>Mr. Bennett</h3>
    <p>Math and P.E. Runs the floorball club.</p>
  </div>
  <div class="card">
    <h3>Principal Adams</h3>
    <p>Running the school for 10 years. Plays the piano.</p>
  </div>
</div>
🙋 Persönliche Website – Lösung (Hobby-Karten)
<h2>My Hobbies</h2>
<div class="cards">
  <div class="card">
    <img src="images/soccer.jpg" alt="Me during a match">
    <h3>Soccer</h3>
    <p>I play forward for the youth team. 12 goals this year!</p>
  </div>
  <div class="card">
    <img src="images/minecraft-build.jpg" alt="My castle in Minecraft">
    <h3>Minecraft</h3>
    <p>I build huge castles. This one took a month.</p>
  </div>
  <div class="card">
    <h3>Coding</h3>
    <p>I'm building this very website right now. And this is just the start.</p>
  </div>
</div>
🌍 Reisetagebuch – Lösung (Reise-Karten)
<h2>Our Trips</h2>
<div class="cards">
  <div class="card">
    <img src="images/mountains.jpg" alt="View from the summit">
    <h3>The Mountains</h3>
    <p>A three-day hike. Best part: wild berries and the lookout tower.</p>
  </div>
  <div class="card">
    <img src="images/sea.jpg" alt="A bay in Greece">
    <h3>Greece</h3>
    <p>A week at the beach. We saw dolphins!</p>
  </div>
  <div class="card">
    <img src="images/dc.jpg" alt="View from a monument in Washington, D.C.">
    <h3>Washington, D.C.</h3>
    <p>Museums, monuments, and way too much walking.</p>
  </div>
</div>
⚽ Vereinswebsite – Lösung (Spieler-Karten)
<h2>Roster</h2>
<div class="cards">
  <div class="card">
    <h3>Jake · goalkeeper 🧤</h3>
    <p>Team captain. 5 clean sheets this season.</p>
  </div>
  <div class="card">
    <h3>Owen · midfield ⚡</h3>
    <p>Fastest player on the team. 8 assists.</p>
  </div>
  <div class="card">
    <h3>Tom · forward 🎯</h3>
    <p>Top scorer. 14 goals this season.</p>
  </div>
</div>

Dasselbe CSS funktioniert für jedes Projekt:

.cards {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

.cards .card {
  flex: 1;
  min-width: 200px;
}

.card {
  background-color: white;
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

Bonus: eine Fotogalerie

Viele Fotos? Ein Foto-Raster ist noch einfacher – nutze aspect-ratio und object-fit, damit alle gleich groß sind:

<div class="gallery">
  <img src="images/p1.jpg" alt="…">
  <img src="images/p2.jpg" alt="…">
  <img src="images/p3.jpg" alt="…">
  <img src="images/p4.jpg" alt="…">
</div>
.gallery {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.gallery img {
  width: 180px;
  aspect-ratio: 1;      /* a square */
  object-fit: cover;    /* the photo fills the square and gets cropped, not squished */
}

Kontrolliere dich selbst: Deine Website hat Karten mit Schatten nebeneinander. Verkleinere das Browserfenster auf Handybreite – die Karten stapeln sich. Das heißt responsives Design, und du hast es gerade geschafft.

Das nimmst du aus dieser Lektion mit