HTML Layout Elements and Techniques

There are several types of HTML table layouts that you can use, depending on your needs:

  1. Basic table layout: This is the simplest type of table layout, where data is arranged in rows and columns. This layout is appropriate for presenting simple data without any need for additional styling or formatting.
  2. Grid table layout: In this layout, tables are divided into a grid of cells, allowing you to create complex layouts and designs. This layout is particularly useful for creating responsive designs that adjust to different screen sizes.
  3. Nested table layout: In this layout, tables are nested within other tables, allowing you to create more complex and hierarchical data structures. This layout can be useful for presenting data that has multiple levels of detail or categorization.
  4. Div-based table layout: This layout uses CSS and div elements to create a table-like structure. This layout is particularly useful for creating responsive designs that can adjust to different screen sizes, as it allows you to easily change the size and position of individual elements.
  5. Fixed table layout: In this layout, the width of the table and its columns is set to a fixed value. This can be useful when you need to ensure that the layout remains consistent across different devices and screen sizes.
  6. Responsive table layout: In this layout, the table adjusts to the screen size of the device, allowing you to create a design that is optimized for mobile and desktop devices. This layout is particularly useful for presenting data that needs to be easily readable and accessible on smaller screens.

Basic table Layout

A basic table layout is the simplest and most common type of table layout in HTML. It involves arranging data in rows and columns, with no special styling or formatting. Each row in the table represents a record, and each column represents a field or attribute of that record.

<table border=1> <tr> <th>Name</th> <th>Age</th> <th>Gender</th> </tr> <tr> <td>Kris james</td> <td>32</td> <td>Male</td> </tr> <tr> <td>Cathy Smith</td> <td>27</td> <td>Female</td> </tr> </table>
run this source code Browser View
Name Age Gender
Kris james 32 Male
Cathy Smith 27 Female

In the above example, a table with three columns: Name, Age, and Gender. The first row of the table contains the column headers, which are marked up with the <th> element. The subsequent rows contain the data for each record, with each field marked up with the <td> element.

When this code is rendered in a web browser, it will display a table with two records, one for Kris James and one for Cathy Smith. Each record is displayed in a row, with each field displayed in a column.

You can further customize the appearance of the table using CSS styles. For example, you can add borders, change the font, and adjust the spacing between rows and columns. However, the basic structure of the table will remain the same, with data arranged in rows and columns.

Grid table Layout

A grid table layout is a more complex type of HTML table layout that involves dividing the table into a grid of cells. This allows for more advanced and customizable layouts, as you can position elements anywhere within the grid.

<table class="grid-table"> <tr> <td colspan="2">Header</td> <td rowspan="2">Logo</td> </tr> <tr> <td>Navigation</td> <td>Content</td> </tr> <tr> <td colspan="3">Footer</td> </tr> </table>
run this source code Browser View
Header Logo
Navigation Content
Footer

In the above example, a table with three rows and three columns. The first row spans two columns using the colspan attribute, which allows the Header element to span across the Navigation and Content cells. The Logo element spans two rows using the rowspan attribute, which allows it to occupy the space of two cells.

The second row contains the Navigation and Content cells, which are each contained within their own column. The third row contains a single cell that spans all three columns, which serves as the footer of the table.

By using the colspan and rowspan attributes, we can create a more complex layout that goes beyond the basic rows and columns of a traditional HTML table. This can be useful for creating advanced designs and layouts, such as responsive designs that adjust to different screen sizes.

Nested table Layout

A nested table layout is an HTML table layout that involves tables being nested within other tables. This allows for more complex and hierarchical data structures to be presented.

<table border=2 bordercolor=green> <tr> <th>Customer</th> <th>Order Date</th> <th>Total</th> </tr> <tr> <td>Cris den</td> <td>01/01/2022</td> <td>$100</td> </tr> <tr> <td colspan="3"> <table border=4 bordercolor=red> <tr> <th>Product</th> <th>Quantity</th> <th>Price</th> </tr> <tr> <td>Product A</td> <td>2</td> <td>$50</td> </tr> <tr> <td>Product B</td> <td>1</td> <td>$50</td> </tr> </table> </td> </tr> </table>
run this source code Browser View
Customer Order Date Total
Cris den 01/01/2022 $100
Product Quantity Price
Product A 2 $50
Product B 1 $50

In the above example, a table that displays order information for a customer. The first row contains the column headers for the customer, order date, and total. The subsequent rows contain data for each order.

However, notice that the third row contains a nested table within it. This is a table that displays the individual products within the order. By nesting this table within the main table, we can create a more complex and hierarchical data structure that displays information about each product within the context of the order.

When this code is rendered in a web browser, it will display a table with two orders, one for John Doe and one for Jane Smith. Each order is displayed in a row, with the order details and a nested table containing the product information.

Div-based table Layout

A div-based table layout is an alternative to using HTML tables for creating a layout with rows and columns. Instead of using the <table>, <tr>, and <td> tags, this approach uses <div> tags with CSS styling to achieve a similar effect.

<div class="table"> <div class="table-row"> <div class="table-cell">Header 1</div> <div class="table-cell">Header 2</div> <div class="table-cell">Header 3</div> </div> <div class="table-row"> <div class="table-cell">Row 1, Cell 1</div> <div class="table-cell">Row 1, Cell 2</div> <div class="table-cell">Row 1, Cell 3</div> </div> <div class="table-row"> <div class="table-cell">Row 2, Cell 1</div> <div class="table-cell">Row 2, Cell 2</div> <div class="table-cell">Row 2, Cell 3</div> </div> </div>

In the above example, a div-based table with three rows and three columns. Each row is created using a <div> with the class table-row. Within each row, create the cells using <div> tags with the class table-cell.

By using CSS styles, you can control the layout and appearance of the div-based table. Here's an example of some CSS styles that could be used with the above HTML:

.table { display: table; width: 100%; } .table-row { display: table-row; } .table-cell { display: table-cell; border: 1px solid black; padding: 10px; }
run this source code Browser View
Header 1
Header 2
Header 3
Row 1, Cell 1
Row 1, Cell 2
Row 1, Cell 3
Row 2, Cell 1
Row 2, Cell 2
Row 2, Cell 3

These styles set the display property of the table to table, which makes it behave like a table. The width: 100% ensures that the table spans the entire width of its container. The .table-row and .table-cell classes are used to style the rows and cells respectively, with a border and padding added to create a similar appearance to a traditional HTML table.

One advantage of a div-based table layout is that it can be more flexible and responsive than using an HTML table. For example, you can easily adjust the width of columns or add and remove rows without affecting the overall layout. However, it can be more difficult to create complex layouts and data structures compared to using HTML tables.

Fixed table Layout

A fixed table layout is an approach to creating tables in HTML that allows for a fixed width to be set for the entire table, as well as for individual columns. This is in contrast to an automatic table layout, where the width of the table and columns are determined based on the content.

<table class="table" style="width: 100%;"> <colgroup> <col style="width: 20%;"> <col style="width: 50%;"> <col style="width: 30%;"> </colgroup> <thead> <tr> <th>Column 1</th> <th>Column 2</th> <th>Column 3</th> </tr> </thead> <tbody> <tr> <td>Row 1, Cell 1</td> <td>Row 1, Cell 2</td> <td>Row 1, Cell 3</td> </tr> <tr> <td>Row 2, Cell 1</td> <td>Row 2, Cell 2</td> <td>Row 2, Cell 3</td> </tr> </tbody> </table>
run this source code Browser View
Column 1 Column 2 Column 3
Row 1, Cell 1 Row 1, Cell 2 Row 1, Cell 3
Row 2, Cell 1 Row 2, Cell 2 Row 2, Cell 3

In the above example, a table with three columns, and each column has a fixed width set using the <col> element within a <colgroup> element. The <thead> and <tbody> elements are used to separate the table header from the table content.

By setting the width of the table to 100%, the table will automatically adjust to the width of its container. However, the columns will remain fixed in width regardless of the amount of content they contain.

One benefit of using a fixed table layout is that it can help improve the performance of the page, as the browser doesn't have to recalculate the width of the table and its columns every time the content changes. It also provides more control over the layout, which can be especially useful when creating complex tables.

However, a downside of using a fixed table layout is that it may not be suitable for all types of content, as the width of the columns is fixed and cannot adjust to accommodate different types of content. In addition, if the width of the content in a cell exceeds the width of the column, the content may overflow and be truncated or hidden.

Responsive table Layout

A responsive table layout is a design technique that allows tables to adapt and adjust to different screen sizes and resolutions without losing their functionality or readability. This means that the table can be viewed and used effectively on various devices, including desktops, laptops, tablets, and smartphones.

One common approach to creating a responsive table layout is through the use of CSS.

<table> <thead> <tr> <th>Product</th> <th>Price</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td data-label="Product">iPhone 13</td> <td data-label="Price">$999</td> <td data-label="Description">The latest iPhone model with advanced features.</td> </tr> <tr> <td data-label="Product">iPad Pro</td> <td data-label="Price">$799</td> <td data-label="Description">A powerful tablet with a large screen and versatile capabilities.</td> </tr> <tr> <td data-label="Product">MacBook Pro</td> <td data-label="Price">$1499</td> <td data-label="Description">A high-performance laptop designed for professionals.</td> </tr> </tbody> </table>
run this source code Browser View
Product Price Description
iPhone 13 $999 The latest iPhone model with advanced features.
iPad Pro $799 A powerful tablet with a large screen and versatile capabilities.
MacBook Pro $1499 A high-performance laptop designed for professionals.

In the above example, the HTML table is structured with the standard thead and tbody elements, and each row is represented by a tr element. The first row contains the table header (th) elements, which provide context for the data that follows.

The important part of this code is in the data-label attribute added to the td elements in each row. This attribute provides a custom label for each column that is used to identify the content of that column on smaller screens where the table might be reformatted into a vertical layout.

The CSS code for this responsive table layout might look something like this:

table { border-collapse: collapse; width: 100%; max-width: 800px; margin: auto; } th, td { text-align: left; padding: 8px; border: 1px solid #ddd; } @media only screen and (max-width: 600px) { table, thead, tbody, th, td, tr { display: block; } th { position: absolute; top: -9999px; left: -9999px; } td { border: none; position: relative; padding-left: 50%; } td:before { position: absolute; top: 6px; left: 6px; width: 45%; padding-right: 10px; white-space: nowrap; content: attr(data-label); font-weight: bold; } }

This CSS code applies a responsive design to the table. When the screen width is less than 600 pixels, the table and its elements are displayed as blocks, which allows them to be stacked vertically. The table header (th) elements are hidden from view, while the table data (td) elements are given a custom label using the data-label attribute. This custom label is displayed using the :before pseudo-element and positioned to the left of the data cell.

With this responsive table layout, the table is still functional and readable on smaller screens, and users can easily access the information they need without having to zoom or scroll horizontally.

Conclusion:

These are just a few examples of the different types of HTML table layouts that you can use. The best layout for your needs will depend on the type of data you are presenting, the design goals you have, and the devices and screen sizes you need to support.