Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Tabelle: Abwechselne Zeilenfarbe mit CSS

Empfohlene Antworten

Veröffentlicht

Hallo habe folgdendes Problem:

Möchte in einer Tabelle die 1 3 5 7 ... Zeile z.b. in hellgrau haben

und die Zeile 2 4 6 8 ... in dunkel grau

zu zeit mache ich das so:

<table>

<tr>

<td class="hell"></td>

</tr>

<tr>

<td class="dunkel"></td>

</tr>

<tr>

<td class="hell"></td>

</tr>

<tr>

<td class="dunkel"></td>

</tr>

<tr>

<td class="hell"></td>

</tr>

<tr>

<td class="dunkel"></td>

</tr>

(Die Klassen sind dann halt irgendwo noch definert)

Das bringt natürlich eine sehr hoch redundanz.

Gibt es eine Möglichkeit mit CSS sowas nochmehr zu vereinfachen???

Gibt es eine Möglichkeit mit CSS sowas nochmehr zu vereinfachen???

AFAIK: Nein, gibt es leider nicht.

Deshalb wird sowas in der Regel serverseitig bei der Generierung der Seite geregelt, beispielsweise:


<?php

  for($i=0; $i < 10; $i++) {

    echo "<tr>";

    echo "<td style=\"background-color: ";

    echo ($i % 2 == 0) ? "green" : "blue";

    echo "\">";

    echo "Text";

    echo "</td>";

    echo "</tr>";

  }

?>

danke!

ok, das habe ich mir schon gedacht..

Leider ist das nicht möglich, da es sich bei den Tabellen um statische HTML Seiten handelt.

naja wenn du z.b. öfters solche listen serverseitig zusammenbaust, könntest du auch eine einfache klasse schreiben:


class Table {

 var $rows;


 //...


 function addRow($row_arr) {

   $this->rows[] = $row_arr;

 }


 function getHtmlCode() {

   $table = "<table>";

   //html code der tabelle zusammenbauen

   //rows durchloopen und die arrays in rows auch durchloopen

   $table .= "</table>";

   return $table;

 }


 function show() {

   echo $this->getHtmlCode();

 }


}

Lässt sich natürlich noch beliebig erweitern ;)

Naja wenn ich da lese "introduced in November 2001" dann wird es wohl noch locker bis 2010 dauern, bis die meisten Browser soweit sind und damit umgehen können :(

Naja ich schätze mal der FF kann das in der nächsten Version die im März kommt und der IE 10 wirds den CSS Std. 1.0 immer noch nicht richtig beherrschen...

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.