Zur Navigation

Tabelle: Kopfzeile automatisch vorne

1 webuser

Hallo,
bei nachfolgendem Code muss man die Kopfdaten (bei kleinem Display) manuell eingeben.
table tbody tr td:nth-of-type(1):before {
    content: "id";
  }
  table tbody tr td:nth-of-type(2):before {
    content: "Name";
  }
  table tbody tr td:nth-of-type(3):before {
    content: "Adesse";
  }
table tbody tr td:nth-of-type(3):before {
    content: "Telefon";

Geht das auch dynamisch?

  table {
    border: 1px solid #ccc;
    border-collapse: collapse;
    margin: 0;
    padding: 0;
    width: 100%;
    table-layout: fixed;
  }

  table caption {
    font-size: 1.5em;
    margin: .5em;
  }

  table tr {
    background-color: #f8f8f8;
    border: 1px solid #ddd;
    padding: .35em;
  }

  table th,
  table td {
    padding: .625em;
    text-align: center;
  }

  table th {
    font-size: .85em;
    letter-spacing: .1em;
                background-color: #2a2a2a;
                color:#ffffff;
  }

  @media screen and (max-width: 600px) {
    table {
      border: 0;
    }

    table caption {
      font-size: 1.3em;
    }

    table thead {
      border: none;
      clip: rect(0 0 0 0);
      height: 1px;
      margin: -1px;
      overflow: hidden;
      padding: 0;
      position: absolute;
      width: 1px;
    }

    table tr {
      border-bottom: 3px solid #ddd;
      display: block;
      margin-bottom: .5em;
    }

    table td {
      border-bottom: 1px solid #ddd;
      display: block;
      font-size: .8em;
      text-align: right;
    }

    table td::before {
      content: attr(data-label);
      float: left;
      font-weight: bold;
    }

    table td:last-child {
      border-bottom: 0;
    }

table tbody tr td:nth-of-type(1):before {
    content: "id";
  }
  table tbody tr td:nth-of-type(2):before {
    content: "Name";
  }
  table tbody tr td:nth-of-type(3):before {
    content: "Adesse";
  }
table tbody tr td:nth-of-type(3):before {
    content: "Telefon";
  }
  }

04.04.2025 13:39

2 Jörg Kruse

Was meinst du mit "dynamisch" - dass in CSS der Inhalt von content ausgelesen wird? du könntest die Kopfdaten auch im HTML in einem data Attribut unterbringen und im CSS mit der attr() Funktion auslesen. Auf dieser Seite wird beispielsweise das Attribut data-label verwendet:

https://allthingssmitty.com/2016/10/03/responsive-table-layout/

Auf dein Beispiel bezogen könnte das im HTML so ausschauen:

<tr>
  <td data-label="Name">...</td>
  <td data-label="Adresse">...</td>
  <td data-label="Telefon">...</td>
</tr>

... und im CSS einfach nur noch:

td::before {
  content: attr(data-label);
}

04.04.2025 14:12

1 Forenmitglied fand diesen Beitrag gut

3 webuser

Danke,
wie in dem Bericht steht:
That’s it! �

04.04.2025 15:20

Beitrag schreiben (als Gast)

Die Antwort wird nach der Überprüfung durch einen Moderator freigeschaltet.





[BBCode-Hilfe]