Osama Mohammed
Osama Mohammed

Reputation: 2861

how to make a table scrollable with html + Tailwind CSS

i have this table like in the image below, it is overflow from the right side, how can I add scrolling, I am using Tailwind CSS like in this code:

  <table class="table-auto overflow-scroll">
                    <thead>
                    <tr class="bg-gray-100">
                        <th class="w-20 px-4 py-2">No.</th>
                        <th class="px-4 py-2">First Name</th>
                        <th class="px-4 py-2">Second Name</th>
                        <th class="px-4 py-2">Third Name</th>
                        <th class="px-4 py-2">Department</th>
                        <th class="px-4 py-2">Stage</th>
                        <th class="px-4 py-2">Email</th>
                        <th class="px-4 py-2">Roles</th>
                        <th class="px-4 py-2">status</th>
                        <th class="px-4 py-2">University Email</th>
                        <th class="px-4 py-2">University Password</th>
                        <th class="px-4 py-2">Students Files</th>
                        <th class="px-4 py-2">Actions</th>
                    </tr>
                    </thead>
                    <tbody>

                        @if(isset($users)) @include('dashboard.users.partials.users_details') @endif
                        @if(isset($searches)) @include('dashboard.users.partials.search') @endif
                        @if(isset($statusSearch)) @include('dashboard.users.partials.status_search') @endif

                    </tbody>
                </table>

enter image description here

Upvotes: 8

Views: 39850

Answers (4)

James Eric
James Eric

Reputation: 21

You can add these classes to your table to make your table scrollable on the x axis

 <table class="table-auto overflow-x-scroll w-full">
                    <thead>
                    <tr class="bg-gray-100">
                        <th class="w-20 px-4 py-2">No.</th>
                        <th class="px-4 py-2">First Name</th>
                        <th class="px-4 py-2">Second Name</th>
                        <th class="px-4 py-2">Third Name</th>
                        <th class="px-4 py-2">Department</th>
                        <th class="px-4 py-2">Stage</th>
                        <th class="px-4 py-2">Email</th>
                        <th class="px-4 py-2">Roles</th>
                        <th class="px-4 py-2">status</th>
                        <th class="px-4 py-2">University Email</th>
                        <th class="px-4 py-2">University Password</th>
                        <th class="px-4 py-2">Students Files</th>
                        <th class="px-4 py-2">Actions</th>
                    </tr>
                    </thead>
                    <tbody>

                        @if(isset($users)) @include('dashboard.users.partials.users_details') @endif
                        @if(isset($searches)) @include('dashboard.users.partials.search') @endif
                        @if(isset($statusSearch)) @include('dashboard.users.partials.status_search') @endif

                    </tbody>
                </table>

Upvotes: 1

shawonwebdev
shawonwebdev

Reputation: 31

<div class="overflow-auto ..."></div> you can try this 

tailewindcss link

Upvotes: 3

Ali Zeinalzadeh
Ali Zeinalzadeh

Reputation: 342

Wrap your table with a div with overflow-x class and your required width, and add w-full class to your table class.

<div class='overflow-x'>
        <table class='table-auto overflow-scroll w-full'>
            <thead>
                <tr class='bg-gray-100'>
                    <th class='w-20 px-4 py-2'>No.</th>
                    <th class='px-4 py-2'>First Name</th>
                    <th class='px-4 py-2'>Second Name</th>
                    <th class='px-4 py-2'>Third Name</th>
                    <th class='px-4 py-2'>Department</th>
                    <th class='px-4 py-2'>Stage</th>
                    <th class='px-4 py-2'>Email</th>
                    <th class='px-4 py-2'>Roles</th>
                    <th class='px-4 py-2'>status</th>
                    <th class='px-4 py-2'>University Email</th>
                    <th class='px-4 py-2'>University Password</th>
                    <th class='px-4 py-2'>Students Files</th>
                    <th class='px-4 py-2'>Actions</th>
                </tr>
            </thead>
            <tbody>
                @if(isset($users))
                @include('dashboard.users.partials.users_details') @endif
                @if(isset($searches))
                @include('dashboard.users.partials.search') @endif
                @if(isset($statusSearch))
                @include('dashboard.users.partials.status_search') @endif
            </tbody>
        </table>
    </div>

Upvotes: 12

Tyler Spruill
Tyler Spruill

Reputation: 133

You can put it all into a div with a fixed width and height, then use

overflow: scroll;

like this

 <style>
 #table {
    width: 50%;
    height: 100%;
    overflow: scroll;
}
</style>
 <div id="table">
 <table>
                    <thead>
                    <tr class="bg-gray-100">
                        <th class="w-20 px-4 py-2">No.</th>
                        <th class="px-4 py-2">First Name</th>
                        <th class="px-4 py-2">Second Name</th>
                        <th class="px-4 py-2">Third Name</th>
                        <th class="px-4 py-2">Department</th>
                        <th class="px-4 py-2">Stage</th>
                        <th class="px-4 py-2">Email</th>
                        <th class="px-4 py-2">Roles</th>
                        <th class="px-4 py-2">status</th>
                        <th class="px-4 py-2">University Email</th>
                        <th class="px-4 py-2">University Password</th>
                        <th class="px-4 py-2">Students Files</th>
                        <th class="px-4 py-2">Actions</th>
                    </tr>
                    </thead>
                    <tbody>

                        @if(isset($users)) @include('dashboard.users.partials.users_details') @endif
                        @if(isset($searches)) @include('dashboard.users.partials.search') @endif
                        @if(isset($statusSearch)) @include('dashboard.users.partials.status_search') @endif

                    </tbody>
                </table>
</div>

of course set the width and height to whatever you need.

Upvotes: 0

Related Questions