DeveloperBreeze

Automatically add Tailwind CSS and jQuery classes to any table

To automatically add Tailwind CSS and jQuery classes to any table on a page, use the following approach.


1. Include jQuery and Tailwind CSS

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Table Styling with Tailwind CSS</title>
    <!-- Include Tailwind CSS -->
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
    <!-- Include jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>

<!-- Example table -->
<table>
    <thead>
        <tr>
            <th>Header 1</th>
            <th>Header 2</th>
            <th>Header 3</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Data 1</td>
            <td>Data 2</td>
            <td>Data 3</td>
        </tr>
        <tr>
            <td>Data 4</td>
            <td>Data 5</td>
            <td>Data 6</td>
        </tr>
    </tbody>
</table>

<!-- jQuery script to apply classes -->
<script>
    $(document).ready(function() {
        $('table').each(function() {
            $(this).addClass('min-w-full bg-white border border-gray-200');
            $(this).find('thead').addClass('bg-gray-50');
            $(this).find('th').addClass('px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider border-b border-gray-200');
            $(this).find('tbody').addClass('bg-white divide-y divide-gray-200');
            $(this).find('tr').addClass('hover:bg-gray-100');
            $(this).find('td').addClass('px-6 py-4 whitespace-nowrap text-sm text-gray-900');
        });
    });
</script>

</body>
</html>

Explanation

Tailwind CSS:

The <link> tag includes Tailwind CSS via CDN, enabling use of utility classes.

jQuery:

The <script> tag includes jQuery, which is used to apply classes dynamically to the table elements.

jQuery Script Behavior:

  • Runs when the document is ready.
  • Selects all <table> elements.
  • Applies Tailwind CSS classes to each table and its child elements.

Tailwind Classes Applied

table:

  • min-w-full: Full-width table.
  • bg-white: White background.
  • border border-gray-200: Adds border.

thead:

  • bg-gray-50: Light gray header background.

th:

  • px-6 py-3: Padding.
  • text-left text-xs font-medium text-gray-500 uppercase tracking-wider: Header text styling.
  • border-b border-gray-200: Bottom border.

tbody:

  • bg-white divide-y divide-gray-200: White background and row separators.

tr:

  • hover:bg-gray-100: Hover effect.

td:

  • px-6 py-4 whitespace-nowrap text-sm text-gray-900: Data cell styling.

Feel free to adjust the class names to fit your design requirements.

Related Posts

More content you might like

Article

10 Insanely Game-Changing Hacks for Web Developers in 2025: Code Smarter, Not Harder

Pro Move: Migrate an old JavaScript project to TypeScript and see how quickly your code quality improves.

Manual deployments? That’s so last decade.

Feb 11, 2025
Read More
Article

Mastering Modern Web Development: Trends, Tools, and Tutorials for 2025 and Beyond

Web development in 2025 is all about creating fast, scalable, and user-friendly applications. Embracing trends like Jamstack, micro frontends, and AI integration will keep you ahead of the curve. By mastering modern tools like Next.js, Tailwind CSS, Docker, and Kubernetes, you’re not just building websites—you’re shaping the future of digital experiences.

Whether you’re following our step-by-step tutorials or exploring the latest tech trends, remember that the world of web development is vast and full of opportunities. Keep learning, keep experimenting, and most importantly, have fun creating amazing digital experiences.

Feb 11, 2025
Read More
Tutorial
php

Building a Laravel Application with Vue.js for Dynamic Interfaces

Visit your application in the browser (e.g., http://127.0.0.1:8000) to see your Vue component in action.

Add a new route in routes/api.php:

Nov 16, 2024
Read More
Code
javascript

Dynamic and Responsive DataTable with Server-Side Processing and Custom Styling

  • serverSide: true enables server-side pagination, sorting, and filtering.
  • processing: true displays a processing indicator while fetching data.
  • The ajax object defines how data is fetched from the server.
  • url: Endpoint for fetching data.
  • type: HTTP method (GET).
  • error: Logs errors that occur during the AJAX request.
  • dataSrc: Processes the server's response. It logs the data and returns records for display.

Oct 24, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!