Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS table-layout Property

Posted in CSS Tutorial
Updated on Jul 19, 2024
By Mari Selvan
👁️ 17 - Views
⏳ 4 mins
💬 1 Comment
CSS table-layout Property

Photo Credit to CodeToFun

🙋 Introduction

The table-layout property in CSS is used to control the layout algorithm used for a table. It can be used to speed up the rendering of large tables by allowing the browser to quickly determine the layout of the table once the widths of the columns are set.

This property is especially useful when working with tables that have complex content or require consistent column widths.

💡 Syntax

The syntax for the table-layout property is straightforward. It can be applied to any table element.

Syntax
Copied
Copy To Clipboard
table {
  table-layout: value;
}

🎛️ Default Value

The default value of the table-layout property is auto.

🏠 Property Values

ValueDescription
autoThe table layout is automatically determined by the browser based on the content of the cells. This can result in a slower rendering time for large tables as the browser needs to calculate the widths of all columns based on their content.
fixedThe table layout is fixed, and the widths of the columns are determined by the width property of the table cells. The browser can render the table more quickly because it doesn't need to wait for all the content to be loaded to calculate the column widths.
inheritThe table layout value is inherited from its parent element.

📄 Example

In this example, we'll demonstrate the difference between auto and fixed table layouts.

index.html
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS table-layout Example</title>
  <style>
    table {
      width: 100%;
      border-collapse: collapse;
      margin-bottom: 20px;
    }
    th, td {
      border: 1px solid #000;
      padding: 8px;
      text-align: left;
    }
    .auto-layout {
      table-layout: auto;
    }
    .fixed-layout {
      table-layout: fixed;
    }
    .fixed-layout th, .fixed-layout td {
      width: 100px; /* Fixed width for demonstration */
    }
  </style>
</head>
<body>
  <h1>Table with Auto Layout</h1>
  <table class="auto-layout">
    <tr>
      <th>Header 1</th>
      <th>Header 2</th>
      <th>Header 3</th>
    </tr>
    <tr>
      <td>Content 1</td>
      <td>Content 2 with longer text</td>
      <td>Content 3</td>
    </tr>
    <tr>
      <td>Content 4</td>
      <td>Content 5</td>
      <td>Content 6 with even longer text to show difference</td>
    </tr>
  </table>

  <h1>Table with Fixed Layout</h1>
  <table class="fixed-layout">
    <tr>
      <th>Header 1</th>
      <th>Header 2</th>
      <th>Header 3</th>
    </tr>
    <tr>
      <td>Content 1</td>
      <td>Content 2 with longer text</td>
      <td>Content 3</td>
    </tr>
    <tr>
      <td>Content 4</td>
      <td>Content 5</td>
      <td>Content 6 with even longer text to show difference</td>
    </tr>
  </table>
</body>
</html>

🖥️ Browser Compatibility

The table-layout property is well-supported in all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. It is also supported in older browsers, making it a reliable property for cross-browser compatibility.

🎉 Conclusion

The table-layout property is a valuable tool for web developers when working with tables, allowing for more control over how tables are rendered.

By using the fixed value, you can improve the rendering performance of large tables and create a more predictable layout. Experiment with different values to see how they affect your tables and choose the one that best suits your needs.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy