` tags and wrapped in a code block with `` tags: ```html Using Multiple Elements in HTML Tables

2024-07-27

Purpose of <tbody>:

The <tbody> tag signifies the main content area of an HTML table. It groups table rows containing the data you want to display.

Using Multiple <tbody> elements:

While HTML allows for just one <thead> (table header) per table, you can have multiple <tbody> elements. These elements need to appear consecutively within the <table> structure.

  • Organization in Large Tables: Splitting a large table into multiple <tbody> sections enhances readability and maintainability.
  • Independent Formatting: You can apply different styles (like fonts or backgrounds) to each <tbody> element, creating visual distinction within the table.
  • Selective Manipulation: With scripting languages like JavaScript, you can target and manipulate specific <tbody> sections for features like hiding or showing content.

Example:

Imagine a table displaying product information with separate sections for electronics and clothing. You could structure it like this:

<table>
  <thead>
    <tr>
      <th>Product Name</th>
      <th>Description</th>
      <th>Price</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>Phone</td>
      <td>Latest smartphone with amazing features</td>
      <td>$500</td>
    </tr>
    </tbody>

  <tbody>
    <tr>
      <td>T-Shirt</td>
      <td>Comfortable cotton T-Shirt</td>
      <td>$15</td>
    </tr>
    </tbody>
</table>

This approach keeps the table organized and allows for potential future styling differences between the electronics and clothing sections.




Example 1: Splitting a Table by Category

This example shows a table with product information divided into "Electronics" and "Clothing" categories:

<table>
  <thead>
    <tr>
      <th>Product Name</th>
      <th>Description</th>
      <th>Price</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <th>Category</th>
      <th colspan="2">Electronics</th> </tr>
    <tr>
      <td>Phone</td>
      <td>Latest smartphone with amazing features</td>
      <td>$500</td>
    </tr>
    <tr>
      <td>Laptop</td>
      <td>Powerful laptop for work and play</td>
      <td>$800</td>
    </tr>
  </tbody>

  <tbody>
    <tr>
      <th>Category</th>
      <th colspan="2">Clothing</th>
    </tr>
    <tr>
      <td>T-Shirt</td>
      <td>Comfortable cotton T-Shirt</td>
      <td>$15</td>
    </tr>
    <tr>
      <td>Jeans</td>
      <td>Durable and stylish jeans</td>
      <td>$40</td>
    </tr>
  </tbody>
</table>

Example 2: Highlighting Rows with Different Background Colors

This example demonstrates using separate <tbody> elements to style rows with different background colors:

<style>
  .electronics tbody tr { background-color: #f5f5f5; }
  .clothing tbody tr { background-color: #e0e0e0; }
</style>

<table>
  <thead>
    <tr>
      <th>Product Name</th>
      <th>Description</th>
      <th>Price</th>
    </tr>
  </thead>

  <tbody class="electronics">
    <tr>
      <td>Phone</td>
      <td>Latest smartphone with amazing features</td>
      <td>$500</td>
    </tr>
    <tr>
      <td>Laptop</td>
      <td>Powerful laptop for work and play</td>
      <td>$800</td>
    </tr>
  </tbody>

  <tbody class="clothing">
    <tr>
      <td>T-Shirt</td>
      <td>Comfortable cotton T-Shirt</td>
      <td>$15</td>
    </tr>
    <tr>
      <td>Jeans</td>
      <td>Durable and stylish jeans</td>
      <td>$40</td>
    </tr>
  </tbody>
</table>



  1. CSS Selectors:
  • You can leverage CSS selectors with various table-related attributes to target and style specific rows or groups of rows within the table. This approach can be more concise for simple styling needs.

Here's an example:

<table>
  <thead>
    <tr>
      <th>Product Name</th>
      <th>Description</th>
      <th>Price</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>Phone</td>
      <td>Latest smartphone with amazing features</td>
      <td>$500</td>
    </tr>
    <tr>
      <td>Laptop</td>
      <td>Powerful laptop for work and play</td>
      <td>$800</td>
    </tr>
    <tr>
      <td>T-Shirt</td>
      <td>Comfortable cotton T-Shirt</td>
      <td>$15</td>
    </tr>
    <tr>
      <td>Jeans</td>
      <td>Durable and stylish jeans</td>
      <td>$40</td>
    </tr>
  </tbody>
</table>

<style>
  /* Style all rows with even index (starting from 0) */
  table tbody tr:nth-child(even) {
    background-color: #f5f5f5;
  }
  
  /* Style rows containing "Phone" in the Product Name */
  table tbody tr:first-child,  /* Target the first row */
  table tbody tr td:nth-child(1):contains("Phone") {  /* Target cells containing "Phone" in the first column */
    background-color: #e0e0e0;
  }
</style>
  1. JavaScript Manipulation:
  • If you require dynamic control over the table content or styling based on user interaction or data, you can utilize JavaScript. This method offers more flexibility but requires scripting knowledge.

Here's a basic example (note: this is a simplified demonstration, error handling may be needed in a real-world scenario):

<script>
function changeBackgroundColor(category) {
  const tableBody = document.querySelector(`tbody[data-category="${category}"]`);
  if (tableBody) {
    tableBody.style.backgroundColor = "#f0f0f0";
  }
}
</script>

<table>
  <thead>
    <tr>
      <th>Product Name</th>
      <th>Description</th>
      <th>Price</th>
    </tr>
  </thead>

  <tbody data-category="electronics">
    <tr>
      <td>Phone</td>
      <td>Latest smartphone with amazing features</td>
      <td>$500</td>
    </tr>
    <tr>
      <td>Laptop</td>
      <td>Powerful laptop for work and play</td>
      <td>$800</td>
    </tr>
  </tbody>

  <tbody data-category="clothing">
    <tr>
      <td>T-Shirt</td>
      <td>Comfortable cotton T-Shirt</td>
      <td>$15</td>
    </tr>
    <tr>
      <td>Jeans</td>
      <td>Durable and stylish jeans</td>
      <td>$40</td>
    </tr>
  </tbody>

  <button onclick="changeBackgroundColor('electronics')">Highlight Electronics</button>
</table>

html html-table xhtml



Ensuring a Smooth User Experience: Best Practices for Popups in JavaScript

Browsers have built-in popup blockers to prevent annoying ads or malicious windows from automatically opening.This can conflict with legitimate popups your website might use...


Why You Should Use the HTML5 Doctype in Your HTML

Standards Mode: The doctype helps the browser render the page in "standards mode" which ensures it follows the latest HTML specifications...


Enhancing Textarea Usability: The Art of Auto-sizing

We'll create a container element, typically a <div>, to hold the actual <textarea> element and another hidden <div>. This hidden element will be used to mirror the content of the textarea...


Example Codes for Customizing Numbering in HTML Ordered Lists

In HTML, ordered lists are created using the <ol> tag.Each item within the list is defined using the <li> tag.By default...


Understanding HTML, CSS, and XHTML for 100% Min-Height Layouts

HTML (HyperText Markup Language) is the building block of web pages. It defines the structure and content of a webpage using elements like headings...



html table xhtml

Fixing Width Collapse in Percentage-Width Child Elements with Absolutely Positioned Parents in Internet Explorer 7

In IE7, when you set a child element's width as a percentage (%) within an absolutely positioned parent that doesn't have an explicitly defined width


Unveiling the Mystery: How Websites Determine Your Timezone (HTML, Javascript, Timezone)

JavaScript Takes Over: Javascript running in the browser can access this information. There are two main methods:JavaScript Takes Over: Javascript running in the browser can access this information


Unleash the Power of Choice: Multiple Submit Button Techniques for HTML Forms

An HTML form is a section of a webpage that lets users enter information. It consists of various elements like text boxes


Unveiling Website Fonts: Techniques for Developers and Designers

The most reliable method is using your browser's developer tools. Here's a general process (specific keys might differ slightly):


Alternative Methods for Disabling Browser Autocomplete

Understanding AutocompleteBrowser autocomplete is a feature that helps users quickly fill out forms by suggesting previously entered values