How to useTailwind CSS Utility Classes in Javascript

In this approach, the Tailwind CSS framework and JavaScript dynamically display the active breakpoint and screen size by listening to window resize events and applying specific styles based on the viewport width.

Syntax:

<div id="app" class="bg-red-200 
sm:bg-blue-200
md:bg-green-200
lg:bg-yellow-200
xl:bg-pink-200
p-4">
</div>

Example: In this example, we are using the Tailwind CSS utility classes for screen sizes or breakpoints. Using the JavaScript, we are dynamically updating and displaying the active breakpoint and screen size. JavaScript listens for window resize events and also updates the displayed breakpoint and screen which is based on media query breakpoints.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <link href=
"https://cdn.jsdelivr.net/npm/tailwindcss@2.2.16/dist/tailwind.min.css"
        rel="stylesheet">
    <title>Tailwind CSS Breakpoint</title>
</head>
 
<body class="flex justify-center
             items-center
             min-h-screen
             m-0">
    <div id="app" class="bg-red-200
                sm:bg-blue-200
                md:bg-green-200
                lg:bg-yellow-200
                xl:bg-pink-200
                p-4">
        <p class="text-2xl">
            Active Breakpoint:
            <span id="breakpoint"
                class="font-semibold">
            </span>
        </p>
        <p class="text-lg">
            Screen Size:
            <span id="screenSize"
                class="font-semibold">
            </span>
        </p>
    </div>
 
    <script>
        function updatingActiveBreakpoint() {
            const breakpoints = {
                'xs': '(max-width: 639px)',
                'sm': '(min-width: 640px) and (max-width: 767px)',
                'md': '(min-width: 768px) and (max-width: 1023px)',
                'lg': '(min-width: 1024px) and (max-width: 1279px)',
                'xl': '(min-width: 1280px)',
            };
            let appElement = document.getElementById("app");
 
            for (const breakpoint in breakpoints) {
                if (window.matchMedia(breakpoints[breakpoint])
                .matches) {
                    document.getElementById("breakpoint"
                    ).textContent = breakpoint;
                    break;
                }
            }
 
            let screenSize = window.innerWidth + "px";
            document.getElementById("screenSize"
            ).textContent = screenSize;
        }
 
        window.addEventListener('resize',
            updatingActiveBreakpoint);
        updatingActiveBreakpoint();
    </script>
</body>
 
</html>


Output:

How to Get Tailwind CSS Active Breakpoint in JavaScript ?

In JavaScript, a Tailwind CSS active breakpoint refers to a specific breakpoint configuration in the Tailwind CSS framework that is currently active, which allows web designers to target specific screen sizes and also consider their styles and functionalities efficiently. In this article, we will see how we can get Tailwinds active breakpoints in JavaScript.

There are two common methods that can be used to get tailwinds active breakpoint in JavaScript, which are listed below:

Table of Content

  • Using Tailwind CSS Utility Classes
  • Using Javascript and Media Queries

We will explore all the above methods along with their basic implementation with the help of examples.

Similar Reads

Approach 1: Using Tailwind CSS Utility Classes

...

Approach 2: Using JavaScript and Media Queries

In this approach, the Tailwind CSS framework and JavaScript dynamically display the active breakpoint and screen size by listening to window resize events and applying specific styles based on the viewport width....