<table class="w-full text-sm text-left rtl:text-right text-gray-500 dark:text-gray-400">…
- User
Some column only contains short lenght character, some others could be way longer, and need more space. I want to set column width to be dynamic, it has to fit itself to content, expand or contract width, and max width should only be 400px
- Assistant
To make the table columns dynamically size according to their content, but also have a maximum width of 400px, you can achieve this with a mix of CSS styles and some tweaks to the table structure.
- User
no, make inline
- Assistant
Sure! Here's how you can apply the styles inline directly on your
<th>and<td>elements, so that each cell dynamically fits content but doesn't get wider than 400px, wrapping text as needed:<table class="w-full text-sm text-left rtl:text-right text-gray-500 dark:text-gray-400" style="table-layout: auto; width: 100%;"> <thead class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400 sticky top-0 z-20"> <tr> @foreach($attrs as $key => $value) <th scope="col" class="px-6 py-3" style="max-width: 400px; white-space: normal; word-break: break-word; overflow-wrap: break-word;"> {{ $key }} </th> @endforeach </tr> </thead> <tbody> @foreach($results as $item) <tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700 border-gray-200"> @foreach($item as $key => $val) <td class="px-6 py-4" style="max-width: 400px; white-space: normal; word-break: break-word; overflow-wrap: break-word;"> {{ $val }} </td> @endforeach </tr> @endforeach </tbody> </table>